0285207997f6a7e27e0a5e9ae1aefeb9da7e02a6
[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(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-tiny.sprite")),
37   glow(false)
38 {
39   sprite = SpriteManager::current()->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     if(action=="dark") {
49       lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
50       lightsprite->set_color(Color(0.1f, 0.1f, 0.1f));
51     }
52 }
53
54 SpriteParticle::~SpriteParticle()
55 {
56   remove_me();
57 }
58
59 void
60 SpriteParticle::hit(Player& )
61 {
62 }
63
64 void
65 SpriteParticle::update(float elapsed_time)
66 {
67   // die when animation is complete
68   if (sprite->animation_done()) {
69     remove_me();
70     return;
71   }
72
73   // calculate new position and velocity
74   position.x += velocity.x * elapsed_time;
75   position.y += velocity.y * elapsed_time;
76   velocity.x += acceleration.x * elapsed_time;
77   velocity.y += acceleration.y * elapsed_time;
78
79   // die when too far offscreen
80   Vector camera = Sector::current()->camera->get_translation();
81   if ((position.x < camera.x - 128) || (position.x > SCREEN_WIDTH + camera.x + 128) ||
82       (position.y < camera.y - 128) || (position.y > SCREEN_HEIGHT + camera.y + 128)) {
83     remove_me();
84     return;
85   }
86 }
87
88 void
89 SpriteParticle::draw(DrawingContext& context)
90 {
91   sprite->draw(context, position, drawing_layer);
92
93   //Sparkles glow in the dark
94   if(glow){
95     context.get_light(position, &light );
96     if (light.red + light.green + light.blue < 3.0){
97       context.push_target();
98       context.set_target(DrawingContext::LIGHTMAP);
99       sprite->draw(context, position, drawing_layer);
100       lightsprite->draw(context, position + Vector(12,12), 0);
101       context.pop_target();
102     }
103   }
104
105 }
106
107 /* EOF */