Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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/main.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   position(position), 
27   velocity(velocity), 
28   acceleration(acceleration), 
29   drawing_layer(drawing_layer)
30 {
31   sprite = sprite_manager->create(sprite_name);
32   if (!sprite.get()) throw std::runtime_error("Could not load sprite "+sprite_name);
33   sprite->set_action(action, 1);
34   sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
35
36   this->position -= get_anchor_pos(sprite->get_current_hitbox(), anchor);
37 }
38
39 SpriteParticle::~SpriteParticle()
40 {
41   remove_me();
42 }
43
44 void
45 SpriteParticle::hit(Player& )
46 {
47 }
48
49 void
50 SpriteParticle::update(float elapsed_time)
51 {
52   // die when animation is complete
53   if (sprite->animation_done()) {
54     remove_me();
55     return;
56   }
57
58   // calculate new position and velocity
59   position.x += velocity.x * elapsed_time;
60   position.y += velocity.y * elapsed_time;
61   velocity.x += acceleration.x * elapsed_time;
62   velocity.y += acceleration.y * elapsed_time;
63
64   // die when too far offscreen
65   Vector camera = Sector::current()->camera->get_translation();
66   if ((position.x < camera.x - 128) || (position.x > SCREEN_WIDTH + camera.x + 128) ||
67       (position.y < camera.y - 128) || (position.y > SCREEN_HEIGHT + camera.y + 128)) {
68     remove_me();
69     return;
70   }
71 }
72
73 void
74 SpriteParticle::draw(DrawingContext& context)
75 {
76   sprite->draw(context, position, drawing_layer);
77 }
78
79 /* EOF */