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/sprite_particle.hpp"
19
20#include "object/camera.hpp"
21#include "sprite/sprite.hpp"
22#include "sprite/sprite_manager.hpp"
23#include "supertux/sector.hpp"
24#include "video/video_system.hpp"
25#include "video/viewport.hpp"
26
27SpriteParticle::SpriteParticle(const std::string& sprite_name, const std::string& action,
28 const Vector& position_, AnchorPoint anchor, const Vector& velocity_, const Vector& acceleration_,
29 int drawing_layer_) :
30 SpriteParticle(SpriteManager::current()->create(sprite_name), action,
31 position_, anchor, velocity_, acceleration_,
32 drawing_layer_)
33{
34 if (sprite_name == "images/objects/particles/sparkle.sprite")
35 {
36 glow = true;
37 if (action=="dark") {
38 lightsprite->set_blend(Blend::ADD);
39 lightsprite->set_color(Color(0.1f, 0.1f, 0.1f));
40 }
41 }
42}
43
44SpriteParticle::SpriteParticle(SpritePtr sprite_, const std::string& action,
45 const Vector& position_, AnchorPoint anchor, const Vector& velocity_, const Vector& acceleration_,
46 int drawing_layer_) :
47 sprite(std::move(sprite_)),
48 position(position_),
49 velocity(velocity_),
50 acceleration(acceleration_),
51 drawing_layer(drawing_layer_),
52 lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-tiny.sprite")),
53 glow(false)
54{
55 sprite->set_action(action, 1);
56 sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
57
58 position -= get_anchor_pos(sprite->get_current_hitbox(), anchor);
59}
60
61SpriteParticle::~SpriteParticle()
62{
63 remove_me();
64}
65
66void
67SpriteParticle::update(float dt_sec)
68{
69 // die when animation is complete
70 if (sprite->animation_done()) {
71 remove_me();
72 return;
73 }
74
75 // calculate new position and velocity
76 position.x += velocity.x * dt_sec;
77 position.y += velocity.y * dt_sec;
78 velocity.x += acceleration.x * dt_sec;
79 velocity.y += acceleration.y * dt_sec;
80
81 // die when too far offscreen
82 Vector camera = Sector::get().get_camera().get_translation();
83 if ((position.x < camera.x - 128.0f) || (position.x > static_cast<float>(SCREEN_WIDTH) + camera.x + 128.0f) ||
84 (position.y < camera.y - 128.0f) || (position.y > static_cast<float>(SCREEN_HEIGHT) + camera.y + 128.0f)) {
85 remove_me();
86 return;
87 }
88}
89
90void
91SpriteParticle::draw(DrawingContext& context)
92{
93 sprite->draw(context.color(), position, drawing_layer);
94
95 //Sparkles glow in the dark
96 if (glow)
97 {
98 sprite->draw(context.light(), position, drawing_layer);
99 lightsprite->draw(context.light(), position + Vector(12,12), 0);
100 }
101
102}
103
104/* EOF */
105