Merge branch 'glow_effects'
[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 "sprite/sprite.hpp"
20 #include "sprite/sprite_manager.hpp"
21 #include "object/sprite_particle.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/sector.hpp"
24
25 #include <stdexcept>
26
27 SpriteParticle::SpriteParticle(std::string sprite_name, std::string action, 
28                                Vector position, AnchorPoint anchor, Vector velocity, Vector acceleration, 
29                                int drawing_layer) :
30   sprite(),
31   position(position), 
32   velocity(velocity), 
33   acceleration(acceleration), 
34   drawing_layer(drawing_layer),
35   light(0.0f,0.0f,0.0f),
36   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-tiny.sprite")),
37   glow(false)
38 {
39   sprite = sprite_manager->create(sprite_name);
40   if (!sprite.get()) throw std::runtime_error("Could not load sprite "+sprite_name);
41   sprite->set_action(action, 1);
42   sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
43
44   this->position -= get_anchor_pos(sprite->get_current_hitbox(), anchor);
45
46   if(sprite_name=="images/objects/particles/sparkle.sprite")
47     glow = true;
48 }
49
50 SpriteParticle::~SpriteParticle()
51 {
52   remove_me();
53 }
54
55 void
56 SpriteParticle::hit(Player& )
57 {
58 }
59
60 void
61 SpriteParticle::update(float elapsed_time)
62 {
63   // die when animation is complete
64   if (sprite->animation_done()) {
65     remove_me();
66     return;
67   }
68
69   // calculate new position and velocity
70   position.x += velocity.x * elapsed_time;
71   position.y += velocity.y * elapsed_time;
72   velocity.x += acceleration.x * elapsed_time;
73   velocity.y += acceleration.y * elapsed_time;
74
75   // die when too far offscreen
76   Vector camera = Sector::current()->camera->get_translation();
77   if ((position.x < camera.x - 128) || (position.x > SCREEN_WIDTH + camera.x + 128) ||
78       (position.y < camera.y - 128) || (position.y > SCREEN_HEIGHT + camera.y + 128)) {
79     remove_me();
80     return;
81   }
82 }
83
84 void
85 SpriteParticle::draw(DrawingContext& context)
86 {
87   sprite->draw(context, position, drawing_layer);
88
89   //Sparkles glow in the dark
90   if(glow){
91     context.get_light(position, &light );
92     if (light.red + light.green + light.blue < 3.0){
93       context.push_target();
94       context.set_target(DrawingContext::LIGHTMAP);
95       sprite->draw(context, position, drawing_layer);
96       lightsprite->draw(context, position + Vector(12,12), 0);
97       context.pop_target();
98     }
99   }
100
101 }
102
103 /* EOF */