b36e3d14c7974c44e39fa776ff010646580a16c4
[supertux.git] / src / object / ghost_particle_system.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
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 <cmath>
20
21 #include "math/random_generator.hpp"
22 #include "supertux/main.hpp"
23 #include "video/drawing_context.hpp"
24
25 //FIXME: Sometimes both ghosts have the same image
26 //       Ghosts don't change their movement pattern - not random
27 GhostParticleSystem::GhostParticleSystem()
28 {
29   ghosts[0] = new Surface("images/objects/particles/ghost0.png");
30   ghosts[1] = new Surface("images/objects/particles/ghost1.png");
31
32   virtual_width = SCREEN_WIDTH * 2;
33
34   // create two ghosts
35   size_t ghostcount = 2;
36   for(size_t i=0; i<ghostcount; ++i) {
37     GhostParticle* particle = new GhostParticle;
38     particle->pos.x = systemRandom.randf(virtual_width);
39     particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
40     int size = systemRandom.rand(2);
41     particle->texture = ghosts[size];
42     particle->speed = systemRandom.randf(std::max(50, (size * 10)), 180 + (size * 10));
43     particles.push_back(particle);
44   }
45 }
46
47 void
48 GhostParticleSystem::parse(const Reader& reader)
49 {
50   reader.get("z-pos", z_pos);
51 }
52
53 GhostParticleSystem::~GhostParticleSystem()
54 {
55   for(int i=0;i<2;++i)
56     delete ghosts[i];
57 }
58
59 void GhostParticleSystem::update(float elapsed_time)
60 {
61   std::vector<Particle*>::iterator i;
62   for(i = particles.begin(); i != particles.end(); ++i) {
63     GhostParticle* particle = (GhostParticle*) *i;
64     particle->pos.y -= particle->speed * elapsed_time;
65     particle->pos.x -= particle->speed * elapsed_time;
66     if(particle->pos.y > SCREEN_HEIGHT) {
67       particle->pos.y = fmodf(particle->pos.y , virtual_height);
68       particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
69     }
70   }
71 }
72
73 /* EOF */