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