Made SpriteParticle's action configurable
[supertux.git] / src / object / sprite_particle.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 #include <stdexcept>
22 #include "sprite_particle.hpp"
23 #include "sector.hpp"
24 #include "camera.hpp"
25 #include "main.hpp"
26 #include "log.hpp"
27
28 SpriteParticle::SpriteParticle(std::string sprite_name, std::string action, Vector position, AnchorPoint anchor, Vector velocity, Vector acceleration, int drawing_layer) 
29         : position(position), velocity(velocity), acceleration(acceleration), drawing_layer(drawing_layer)
30 {
31   sprite = sprite_manager->create(sprite_name);
32   if (!sprite) throw std::runtime_error("Could not load sprite "+sprite_name);
33   sprite->set_action(action, 1);
34
35   this->position -= get_anchor_pos(sprite->get_current_hitbox(), anchor);
36 }
37   
38 SpriteParticle::~SpriteParticle() 
39 {
40   remove_me();
41 }
42
43 void
44 SpriteParticle::hit(Player& )
45 {
46 }
47
48 void
49 SpriteParticle::update(float elapsed_time) 
50 {
51   // die when animation is complete
52   if (sprite->animation_done()) {
53     remove_me();
54     return;
55   }
56
57   // calculate new position and velocity
58   position.x += velocity.x * elapsed_time;
59   position.y += velocity.y * elapsed_time;
60   velocity.x += acceleration.x * elapsed_time;
61   velocity.y += acceleration.y * elapsed_time;
62
63   // die when too far offscreen
64   Vector camera = Sector::current()->camera->get_translation();
65   if ((position.x < camera.x - 128) || (position.x > SCREEN_WIDTH + camera.x + 128) || 
66       (position.y < camera.y - 128) || (position.y > SCREEN_HEIGHT + camera.y + 128)) {
67     remove_me();
68     return;
69   }
70 }
71
72 void
73 SpriteParticle::draw(DrawingContext& context) 
74 {
75    sprite->draw(context, position, drawing_layer);
76 }