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))
52 throw std::runtime_error("no sprite name set");
54 sprite = sprite_manager->create(sprite_name);
55 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
56 set_group(collision_group);
59 MovingSprite::MovingSprite(const lisp::Lisp& reader, const std::string& sprite_name, int layer, CollisionGroup collision_group)
60 : sprite_name(sprite_name), layer(layer)
62 if (!reader.get("x", bbox.p1.x))
63 throw std::runtime_error("no x position set");
64 if (!reader.get("y", bbox.p1.y))
65 throw std::runtime_error("no y position set");
67 sprite = sprite_manager->create(sprite_name);
68 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
69 set_group(collision_group);
72 MovingSprite::MovingSprite(const lisp::Lisp& reader, int layer, CollisionGroup collision_group)
75 if (!reader.get("x", bbox.p1.x))
76 throw std::runtime_error("no x position set");
77 if (!reader.get("y", bbox.p1.y))
78 throw std::runtime_error("no y position set");
79 if (!reader.get("sprite", sprite_name))
80 throw std::runtime_error("no sprite name set");
82 sprite = sprite_manager->create(sprite_name);
83 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
84 set_group(collision_group);
87 MovingSprite::MovingSprite(const MovingSprite& other)
88 : MovingObject(other), layer(other.layer)
90 sprite = new Sprite(*other.sprite);
94 MovingSprite::operator=(const MovingSprite& other)
100 sprite = new Sprite(*other.sprite);
107 MovingSprite::~MovingSprite()
113 MovingSprite::draw(DrawingContext& context)
115 sprite->draw(context, get_pos(), layer);
119 MovingSprite::update(float )
124 MovingSprite::set_action(const std::string& action, int loops)
126 sprite->set_action(action, loops);
127 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
131 MovingSprite::set_action_centered(const std::string& action, int loops)
133 Vector old_size = bbox.get_size();
134 sprite->set_action(action, loops);
135 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
136 set_pos(get_pos() - (bbox.get_size() - old_size) / 2);