05dd9f30ddab68f1f4fc786347f9c853c40c5275
[supertux.git] / src / object / particlesystem.h
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 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 #ifndef SUPERTUX_PARTICLESYSTEM_H
20 #define SUPERTUX_PARTICLESYSTEM_H
21
22 #include <vector>
23
24 #include "video/surface.h"
25 #include "game_object.h"
26 #include "serializable.h"
27 #include "sector.h"
28 #include "math/vector.h"
29
30 namespace lisp {
31 class Lisp;
32 }
33
34 class DisplayManager;
35
36 /**
37  * This is the base class for particle systems. It is responsible for storing a
38  * set of particles with each having an x- and y-coordinate the number of the
39  * layer where it should be drawn and a texture.
40  * The coordinate system used here is a virtual one. It would be a bad idea to
41  * populate whole levels with particles. So we're using a virtual rectangle
42  * here that is tiled onto the level when drawing. This rect.has the size
43  * (virtual_width, virtual_height). We're using modulo on the particle
44  * coordinates, so when a particle leaves left, it'll reenter at the right
45  * side.
46  *
47  * Classes that implement a particle system should subclass from this class,
48  * initialize particles in the constructor and move them in the simulate
49  * function.
50  */
51 class ParticleSystem : public GameObject
52 {
53 public:
54     ParticleSystem();
55     virtual ~ParticleSystem();
56     
57     virtual void draw(DrawingContext& context);
58
59 protected:
60     int layer;
61
62     class Particle
63     {
64     public:
65         virtual ~Particle()
66         { }
67
68         Vector pos;
69         Surface* texture;
70     };
71     
72     std::vector<Particle*> particles;
73     float virtual_width, virtual_height;
74 };
75
76 class SnowParticleSystem : public ParticleSystem, public Serializable
77 {
78 public:
79     SnowParticleSystem();
80     virtual ~SnowParticleSystem();
81
82     void parse(const lisp::Lisp& lisp);
83     void write(lisp::Writer& writer);
84
85     virtual void update(float elapsed_time);
86
87     std::string type() const
88     { return "SnowParticleSystem"; }
89     
90 private:
91     class SnowParticle : public Particle
92     {
93     public:
94         float speed;
95     };
96     
97     Surface* snowimages[3];
98 };
99
100 class CloudParticleSystem : public ParticleSystem, public Serializable
101 {
102 public:
103     CloudParticleSystem();
104     virtual ~CloudParticleSystem();
105
106     void parse(const lisp::Lisp& lisp);
107     void write(lisp::Writer& writer);
108
109     virtual void update(float elapsed_time);
110
111     std::string type() const
112     { return "SnowParticleSystem"; }    
113     
114 private:
115     class CloudParticle : public Particle
116     {
117     public:
118         float speed;
119     };
120     
121     Surface* cloudimage;
122 };
123
124 #endif
125