Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / particlesystem.cpp
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 #include "object/particlesystem.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 ParticleSystem::ParticleSystem(float max_particle_size) :
26   max_particle_size(max_particle_size)
27 {
28   virtual_width = SCREEN_WIDTH + max_particle_size * 2;
29   virtual_height = SCREEN_HEIGHT + max_particle_size *2;
30   z_pos = LAYER_BACKGROUND1;
31 }
32
33 ParticleSystem::~ParticleSystem()
34 {
35   std::vector<Particle*>::iterator i;
36   for(i = particles.begin(); i != particles.end(); ++i) {
37     delete *i;
38   }
39 }
40
41 void ParticleSystem::draw(DrawingContext& context)
42 {
43   float scrollx = context.get_translation().x;
44   float scrolly = context.get_translation().y;
45
46   context.push_transform();
47   context.set_translation(Vector(max_particle_size,max_particle_size));
48
49   std::vector<Particle*>::iterator i;
50   for(i = particles.begin(); i != particles.end(); ++i) {
51     Particle* particle = *i;
52
53     // remap x,y coordinates onto screencoordinates
54     Vector pos;
55
56     pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
57     if(pos.x < 0) pos.x += virtual_width;
58
59     pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
60     if(pos.y < 0) pos.y += virtual_height;
61
62     //if(pos.x > virtual_width) pos.x -= virtual_width;
63     //if(pos.y > virtual_height) pos.y -= virtual_height;
64
65     context.draw_surface(particle->texture, pos, z_pos);
66   }
67
68   context.pop_transform();
69 }
70
71 SnowParticleSystem::SnowParticleSystem()
72 {
73   snowimages[0] = new Surface("images/objects/particles/snow2.png");
74   snowimages[1] = new Surface("images/objects/particles/snow1.png");
75   snowimages[2] = new Surface("images/objects/particles/snow0.png");
76
77   virtual_width = SCREEN_WIDTH * 2;
78
79   // create some random snowflakes
80   size_t snowflakecount = size_t(virtual_width/10.0);
81   for(size_t i=0; i<snowflakecount; ++i) {
82     SnowParticle* particle = new SnowParticle;
83     int snowsize = systemRandom.rand(3);
84
85     particle->pos.x = systemRandom.randf(virtual_width);
86     particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
87     particle->anchorx = particle->pos.x + (systemRandom.randf(-0.5, 0.5) * 16);
88     particle->drift_speed = systemRandom.randf(-0.5, 0.5) * 0.3;
89     particle->wobble = 0.0;
90
91     particle->texture = snowimages[snowsize];
92
93     particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
94     particle->speed *= 20; // gravity
95
96     particles.push_back(particle);
97   }
98 }
99
100 void
101 SnowParticleSystem::parse(const Reader& reader)
102 {
103   reader.get("z-pos", z_pos);
104 }
105
106 SnowParticleSystem::~SnowParticleSystem()
107 {
108   for(int i=0;i<3;++i)
109     delete snowimages[i];
110 }
111
112 void SnowParticleSystem::update(float elapsed_time)
113 {
114   std::vector<Particle*>::iterator i;
115
116   for(i = particles.begin(); i != particles.end(); ++i) {
117     SnowParticle* particle = (SnowParticle*) *i;
118     float anchor_delta;
119
120     particle->pos.y += particle->speed * elapsed_time;
121     particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
122
123     anchor_delta = (particle->anchorx - particle->pos.x);
124     particle->wobble += (4 * anchor_delta * 0.05) + systemRandom.randf(-0.5, 0.5);
125     particle->wobble *= 0.99f;
126     particle->anchorx += particle->drift_speed * elapsed_time;
127   }
128 }
129
130 //FIXME: Sometimes both ghosts have the same image
131 //       Ghosts don't change their movement pattern - not random
132 GhostParticleSystem::GhostParticleSystem()
133 {
134   ghosts[0] = new Surface("images/objects/particles/ghost0.png");
135   ghosts[1] = new Surface("images/objects/particles/ghost1.png");
136
137   virtual_width = SCREEN_WIDTH * 2;
138
139   // create two ghosts
140   size_t ghostcount = 2;
141   for(size_t i=0; i<ghostcount; ++i) {
142     GhostParticle* particle = new GhostParticle;
143     particle->pos.x = systemRandom.randf(virtual_width);
144     particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
145     int size = systemRandom.rand(2);
146     particle->texture = ghosts[size];
147     particle->speed = systemRandom.randf(std::max(50, (size * 10)), 180 + (size * 10));
148     particles.push_back(particle);
149   }
150 }
151
152 void
153 GhostParticleSystem::parse(const Reader& reader)
154 {
155   reader.get("z-pos", z_pos);
156 }
157
158 GhostParticleSystem::~GhostParticleSystem()
159 {
160   for(int i=0;i<2;++i)
161     delete ghosts[i];
162 }
163
164 void GhostParticleSystem::update(float elapsed_time)
165 {
166   std::vector<Particle*>::iterator i;
167   for(i = particles.begin(); i != particles.end(); ++i) {
168     GhostParticle* particle = (GhostParticle*) *i;
169     particle->pos.y -= particle->speed * elapsed_time;
170     particle->pos.x -= particle->speed * elapsed_time;
171     if(particle->pos.y > SCREEN_HEIGHT) {
172       particle->pos.y = fmodf(particle->pos.y , virtual_height);
173       particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
174     }
175   }
176 }
177
178 CloudParticleSystem::CloudParticleSystem()
179   : ParticleSystem(128)
180 {
181   cloudimage = new Surface("images/objects/particles/cloud.png");
182
183   virtual_width = 2000.0;
184
185   // create some random clouds
186   for(size_t i=0; i<15; ++i) {
187     CloudParticle* particle = new CloudParticle;
188     particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
189     particle->pos.y = systemRandom.rand(static_cast<int>(virtual_height));
190     particle->texture = cloudimage;
191     particle->speed = -systemRandom.randf(25.0, 54.0);
192
193     particles.push_back(particle);
194   }
195 }
196
197 void
198 CloudParticleSystem::parse(const Reader& reader)
199 {
200   reader.get("z-pos", z_pos);
201 }
202
203 CloudParticleSystem::~CloudParticleSystem()
204 {
205   delete cloudimage;
206 }
207
208 void CloudParticleSystem::update(float elapsed_time)
209 {
210   std::vector<Particle*>::iterator i;
211   for(i = particles.begin(); i != particles.end(); ++i) {
212     CloudParticle* particle = (CloudParticle*) *i;
213     particle->pos.x += particle->speed * elapsed_time;
214   }
215 }
216
217 /* EOF */