00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "object/moving_sprite.hpp"
00018 #include "sprite/sprite.hpp"
00019 #include "sprite/sprite_manager.hpp"
00020 #include "util/reader.hpp"
00021
00022 #include <stdexcept>
00023
00024 MovingSprite::MovingSprite(const Vector& pos, const std::string& sprite_name,
00025 int layer, CollisionGroup collision_group) :
00026 sprite_name(sprite_name),
00027 sprite(),
00028 layer(layer)
00029 {
00030 bbox.set_pos(pos);
00031 sprite = sprite_manager->create(sprite_name);
00032 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00033 set_group(collision_group);
00034 }
00035
00036 MovingSprite::MovingSprite(const Reader& reader, const Vector& pos, int layer, CollisionGroup collision_group) :
00037 sprite_name(),
00038 sprite(),
00039 layer(layer)
00040 {
00041 bbox.set_pos(pos);
00042 if (!reader.get("sprite", sprite_name))
00043 throw std::runtime_error("no sprite name set");
00044
00045 sprite = sprite_manager->create(sprite_name);
00046 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00047 set_group(collision_group);
00048 }
00049
00050 MovingSprite::MovingSprite(const Reader& reader, const std::string& sprite_name, int layer, CollisionGroup collision_group) :
00051 sprite_name(sprite_name),
00052 sprite(),
00053 layer(layer)
00054 {
00055 reader.get("x", bbox.p1.x);
00056 reader.get("y", bbox.p1.y);
00057 reader.get("sprite", this->sprite_name);
00058
00059 sprite = sprite_manager->create(this->sprite_name);
00060 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00061 set_group(collision_group);
00062 }
00063
00064 MovingSprite::MovingSprite(const Reader& reader, int layer, CollisionGroup collision_group) :
00065 sprite_name(),
00066 sprite(),
00067 layer(layer)
00068 {
00069 reader.get("x", bbox.p1.x);
00070 reader.get("y", bbox.p1.y);
00071 if (!reader.get("sprite", sprite_name))
00072 throw std::runtime_error("no sprite name set");
00073
00074 sprite = sprite_manager->create(sprite_name);
00075 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00076 set_group(collision_group);
00077 }
00078
00079 MovingSprite::MovingSprite(const MovingSprite& other) :
00080 MovingObject(other),
00081 sprite_name(),
00082 sprite(),
00083 layer(other.layer)
00084 {
00085 sprite = other.sprite->clone();
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 MovingSprite::~MovingSprite()
00103 {
00104 }
00105
00106 void
00107 MovingSprite::draw(DrawingContext& context)
00108 {
00109 sprite->draw(context, get_pos(), layer);
00110 }
00111
00112 void
00113 MovingSprite::update(float )
00114 {
00115 }
00116
00117 void
00118 MovingSprite::set_action(const std::string& action, int loops)
00119 {
00120 sprite->set_action(action, loops);
00121 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00122 }
00123
00124 void
00125 MovingSprite::set_action_centered(const std::string& action, int loops)
00126 {
00127 Vector old_size = bbox.get_size();
00128 sprite->set_action(action, loops);
00129 set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
00130 set_pos(get_pos() - (bbox.get_size() - old_size) / 2);
00131 }
00132
00133 void
00134 MovingSprite::set_action(const std::string& action, int loops, AnchorPoint anchorPoint)
00135 {
00136 Rectf old_bbox = bbox;
00137 sprite->set_action(action, loops);
00138 float w = sprite->get_current_hitbox_width();
00139 float h = sprite->get_current_hitbox_height();
00140 set_size(w, h);
00141 set_pos(get_anchor_pos(old_bbox, w, h, anchorPoint));
00142 }
00143
00144