1// SuperTux
2// Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "object/ghost_particle_system.hpp"
18
19#include <algorithm>
20#include <math.h>
21
22#include "math/random.hpp"
23#include "supertux/globals.hpp"
24#include "video/surface.hpp"
25#include "video/video_system.hpp"
26#include "video/viewport.hpp"
27
28//FIXME: Sometimes both ghosts have the same image
29// Ghosts don't change their movement pattern - not random
30GhostParticleSystem::GhostParticleSystem()
31{
32 init();
33}
34
35GhostParticleSystem::GhostParticleSystem(const ReaderMapping& reader) :
36 ParticleSystem(reader)
37{
38 init();
39}
40
41GhostParticleSystem::~GhostParticleSystem()
42{
43}
44
45void
46GhostParticleSystem::init()
47{
48 ghosts[0] = Surface::from_file("images/objects/particles/ghost0.png");
49 ghosts[1] = Surface::from_file("images/objects/particles/ghost1.png");
50
51 virtual_width = static_cast<float>(SCREEN_WIDTH) * 2.0f;
52
53 // create two ghosts
54 size_t ghostcount = 2;
55 for (size_t i=0; i<ghostcount; ++i) {
56 auto particle = std::make_unique<GhostParticle>();
57 particle->pos.x = graphicsRandom.randf(virtual_width);
58 particle->pos.y = graphicsRandom.randf(static_cast<float>(SCREEN_HEIGHT));
59 int size = graphicsRandom.rand(2);
60 particle->texture = ghosts[size];
61 particle->speed = graphicsRandom.randf(std::max(50.0f, static_cast<float>(size) * 10.0f),
62 180.0f + static_cast<float>(size) * 10.0f);
63 particles.push_back(std::move(particle));
64 }
65}
66
67void
68GhostParticleSystem::update(float dt_sec)
69{
70 if (!enabled)
71 return;
72
73 for (const auto& part : particles) {
74 const auto& particle = dynamic_cast<GhostParticle*>(part.get());
75 particle->pos.y -= particle->speed * dt_sec;
76 particle->pos.x -= particle->speed * dt_sec;
77 if (particle->pos.y > static_cast<float>(SCREEN_HEIGHT)) {
78 particle->pos.y = fmodf(particle->pos.y , virtual_height);
79 particle->pos.x = graphicsRandom.randf(virtual_width);
80 }
81 }
82}
83
84/* EOF */
85