d49506245171a6d7b183a796d93e895454dbab6f
[supertux.git] / src / object / particlesystem.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef SUPERTUX_PARTICLESYSTEM_H
21 #define SUPERTUX_PARTICLESYSTEM_H
22
23 #include <vector>
24
25 #include "video/surface.hpp"
26 #include "game_object.hpp"
27 #include "serializable.hpp"
28 #include "sector.hpp"
29 #include "math/vector.hpp"
30
31 namespace lisp {
32 class Lisp;
33 }
34
35 class DisplayManager;
36
37 /**
38  * This is the base class for particle systems. It is responsible for storing a
39  * set of particles with each having an x- and y-coordinate the number of the
40  * layer where it should be drawn and a texture.
41  * The coordinate system used here is a virtual one. It would be a bad idea to
42  * populate whole levels with particles. So we're using a virtual rectangle
43  * here that is tiled onto the level when drawing. This rect.has the size
44  * (virtual_width, virtual_height). We're using modulo on the particle
45  * coordinates, so when a particle leaves left, it'll reenter at the right
46  * side.
47  *
48  * Classes that implement a particle system should subclass from this class,
49  * initialize particles in the constructor and move them in the simulate
50  * function.
51  */
52 class ParticleSystem : public GameObject
53 {
54 public:
55     ParticleSystem(float max_particle_size = 60);
56     virtual ~ParticleSystem();
57
58     virtual void draw(DrawingContext& context);
59
60 protected:
61     float max_particle_size;
62     int z_pos;
63
64     class Particle
65     {
66     public:
67         virtual ~Particle()
68         { }
69
70         Vector pos;
71         Surface* texture;
72     };
73
74     std::vector<Particle*> particles;
75     float virtual_width, virtual_height;
76 };
77
78 class SnowParticleSystem : public ParticleSystem, public Serializable
79 {
80 public:
81     SnowParticleSystem();
82     virtual ~SnowParticleSystem();
83
84     void parse(const lisp::Lisp& lisp);
85     void write(lisp::Writer& writer);
86
87     virtual void update(float elapsed_time);
88
89     std::string type() const
90     { return "SnowParticleSystem"; }
91
92 private:
93     class SnowParticle : public Particle
94     {
95     public:
96         float speed;
97         float wobble;
98         float anchorx;
99         float drift_speed;
100     };
101
102     Surface* snowimages[3];
103 };
104
105 class GhostParticleSystem : public ParticleSystem, public Serializable
106 {
107 public:
108     GhostParticleSystem();
109     virtual ~GhostParticleSystem();
110
111     void parse(const lisp::Lisp& lisp);
112     void write(lisp::Writer& writer);
113
114     virtual void update(float elapsed_time);
115
116     std::string type() const
117     { return "GhostParticleSystem"; }
118
119 private:
120     class GhostParticle : public Particle
121     {
122     public:
123         float speed;
124     };
125
126     Surface* ghosts[2];
127 };
128
129 class CloudParticleSystem : public ParticleSystem, public Serializable
130 {
131 public:
132     CloudParticleSystem();
133     virtual ~CloudParticleSystem();
134
135     void parse(const lisp::Lisp& lisp);
136     void write(lisp::Writer& writer);
137
138     virtual void update(float elapsed_time);
139
140     std::string type() const
141     { return "CloudParticleSystem"; }
142
143 private:
144     class CloudParticle : public Particle
145     {
146     public:
147         float speed;
148     };
149
150     Surface* cloudimage;
151 };
152
153 #endif