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