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