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