3 // SuperTux - MovingSprite Base Class
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "moving_sprite.hpp"
24 #include "video/drawing_context.hpp"
25 #include "sprite/sprite_manager.hpp"
28 #include "player_status.hpp"
29 #include "gameobjs.hpp"
30 #include "statistics.hpp"
31 #include "object_factory.hpp"
33 #include "random_generator.hpp"
34 #include "audio/sound_source.hpp"
35 #include "audio/sound_manager.hpp"
38 MovingSprite::MovingSprite(const Vector& pos, const std::string& sprite_name, int layer, CollisionGroup collision_group)
39 : sprite_name(sprite_name), layer(layer)
42 sprite = sprite_manager->create(sprite_name);
43 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
44 set_group(collision_group);
47 MovingSprite::MovingSprite(const lisp::Lisp& reader, const Vector& pos, int layer, CollisionGroup collision_group)
51 if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
52 sprite = sprite_manager->create(sprite_name);
53 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
54 set_group(collision_group);
57 MovingSprite::MovingSprite(const lisp::Lisp& reader, const std::string& sprite_name, int layer, CollisionGroup collision_group)
58 : sprite_name(sprite_name), layer(layer)
60 if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
61 if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
62 sprite = sprite_manager->create(sprite_name);
63 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
64 set_group(collision_group);
67 MovingSprite::MovingSprite(const lisp::Lisp& reader, int layer, CollisionGroup collision_group)
70 if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
71 if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
72 if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
73 sprite = sprite_manager->create(sprite_name);
74 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
75 set_group(collision_group);
78 MovingSprite::MovingSprite(const MovingSprite& other)
79 : MovingObject(other), layer(other.layer)
81 sprite = new Sprite(*other.sprite);
85 MovingSprite::operator=(const MovingSprite& other)
87 if (this == &other) return *this;
90 sprite = new Sprite(*other.sprite);
97 MovingSprite::~MovingSprite()
103 MovingSprite::draw(DrawingContext& context)
105 sprite->draw(context, get_pos(), layer);
109 MovingSprite::update(float )
114 MovingSprite::set_action(const std::string& action, int loops)
116 sprite->set_action(action, loops);
117 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
121 MovingSprite::set_action_centered(const std::string& action, int loops)
123 Vector old_size = bbox.get_size();
124 sprite->set_action(action, loops);
125 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
126 set_pos(get_pos() - (bbox.get_size() - old_size) / 2);