src/object/sprite_particle.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
00004 //
00005 //  This program is free software: you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation, either version 3 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #include "object/camera.hpp"
00019 #include "object/sprite_particle.hpp"
00020 #include "supertux/globals.hpp"
00021 #include "supertux/sector.hpp"
00022 
00023 #include <stdexcept>
00024 
00025 SpriteParticle::SpriteParticle(std::string sprite_name, std::string action, 
00026                                Vector position, AnchorPoint anchor, Vector velocity, Vector acceleration, 
00027                                int drawing_layer) :
00028   sprite(),
00029   position(position), 
00030   velocity(velocity), 
00031   acceleration(acceleration), 
00032   drawing_layer(drawing_layer)
00033 {
00034   sprite = sprite_manager->create(sprite_name);
00035   if (!sprite.get()) throw std::runtime_error("Could not load sprite "+sprite_name);
00036   sprite->set_action(action, 1);
00037   sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
00038 
00039   this->position -= get_anchor_pos(sprite->get_current_hitbox(), anchor);
00040 }
00041 
00042 SpriteParticle::~SpriteParticle()
00043 {
00044   remove_me();
00045 }
00046 
00047 void
00048 SpriteParticle::hit(Player& )
00049 {
00050 }
00051 
00052 void
00053 SpriteParticle::update(float elapsed_time)
00054 {
00055   // die when animation is complete
00056   if (sprite->animation_done()) {
00057     remove_me();
00058     return;
00059   }
00060 
00061   // calculate new position and velocity
00062   position.x += velocity.x * elapsed_time;
00063   position.y += velocity.y * elapsed_time;
00064   velocity.x += acceleration.x * elapsed_time;
00065   velocity.y += acceleration.y * elapsed_time;
00066 
00067   // die when too far offscreen
00068   Vector camera = Sector::current()->camera->get_translation();
00069   if ((position.x < camera.x - 128) || (position.x > SCREEN_WIDTH + camera.x + 128) ||
00070       (position.y < camera.y - 128) || (position.y > SCREEN_HEIGHT + camera.y + 128)) {
00071     remove_me();
00072     return;
00073   }
00074 }
00075 
00076 void
00077 SpriteParticle::draw(DrawingContext& context)
00078 {
00079   sprite->draw(context, position, drawing_layer);
00080 }
00081 
00082 /* EOF */

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