src/object/moving_sprite.cpp

Go to the documentation of this file.
00001 //  SuperTux - MovingSprite Base Class
00002 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   MovingSprite&
00089   MovingSprite::operator=(const MovingSprite& other)
00090   {
00091   if (this == &other)
00092   return *this;
00093 
00094   delete sprite;
00095   sprite = new Sprite(*other.sprite);
00096 
00097   layer = other.layer;
00098 
00099   return *this;
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 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1