Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / particlesystem.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 #ifndef HEADER_SUPERTUX_OBJECT_PARTICLESYSTEM_HPP
18 #define HEADER_SUPERTUX_OBJECT_PARTICLESYSTEM_HPP
19
20 #include <vector>
21
22 #include "math/vector.hpp"
23 #include "supertux/game_object.hpp"
24 #include "util/reader.hpp"
25
26 class Surface;
27
28 class DisplayManager;
29
30 /**
31  * This is the base class for particle systems. It is responsible for storing a
32  * set of particles with each having an x- and y-coordinate the number of the
33  * layer where it should be drawn and a texture.
34  * The coordinate system used here is a virtual one. It would be a bad idea to
35  * populate whole levels with particles. So we're using a virtual rectangle
36  * here that is tiled onto the level when drawing. This rect.has the size
37  * (virtual_width, virtual_height). We're using modulo on the particle
38  * coordinates, so when a particle leaves left, it'll reenter at the right
39  * side.
40  *
41  * Classes that implement a particle system should subclass from this class,
42  * initialize particles in the constructor and move them in the simulate
43  * function.
44  */
45 class ParticleSystem : public GameObject
46 {
47 public:
48   ParticleSystem(float max_particle_size = 60);
49   virtual ~ParticleSystem();
50
51   virtual void draw(DrawingContext& context);
52
53 protected:
54   float max_particle_size;
55   int z_pos;
56
57   class Particle
58   {
59   public:
60     virtual ~Particle()
61     { }
62
63     Vector pos;
64     Surface* texture;
65   };
66
67   std::vector<Particle*> particles;
68   float virtual_width, virtual_height;
69 };
70
71 class SnowParticleSystem : public ParticleSystem
72 {
73 public:
74   SnowParticleSystem();
75   virtual ~SnowParticleSystem();
76
77   void parse(const Reader& lisp);
78
79   virtual void update(float elapsed_time);
80
81   std::string type() const
82   { return "SnowParticleSystem"; }
83
84 private:
85   class SnowParticle : public Particle
86   {
87   public:
88     float speed;
89     float wobble;
90     float anchorx;
91     float drift_speed;
92   };
93
94   Surface* snowimages[3];
95 };
96
97 class GhostParticleSystem : public ParticleSystem
98 {
99 public:
100   GhostParticleSystem();
101   virtual ~GhostParticleSystem();
102
103   void parse(const Reader& lisp);
104   
105   virtual void update(float elapsed_time);
106
107   std::string type() const
108   { return "GhostParticleSystem"; }
109
110 private:
111   class GhostParticle : public Particle
112   {
113   public:
114     float speed;
115   };
116
117   Surface* ghosts[2];
118 };
119
120 class CloudParticleSystem : public ParticleSystem
121 {
122 public:
123   CloudParticleSystem();
124   virtual ~CloudParticleSystem();
125
126   void parse(const Reader& lisp);
127   
128   virtual void update(float elapsed_time);
129
130   std::string type() const
131   { return "CloudParticleSystem"; }
132
133 private:
134   class CloudParticle : public Particle
135   {
136   public:
137     float speed;
138   };
139
140   Surface* cloudimage;
141 };
142
143 #endif
144
145 /* EOF */