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