New object weak_block: a block that can be destroyed by bullet hits
[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     int snowsize = systemRandom.rand(3);
94
95     particle->pos.x = systemRandom.randf(virtual_width);
96     particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
97     particle->anchorx = particle->pos.x + (systemRandom.randf(-0.5, 0.5) * 16);
98     particle->drift_speed = systemRandom.randf(-0.5, 0.5) * 0.3;
99     particle->wobble = 0.0;
100
101     particle->texture = snowimages[snowsize];
102
103     particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
104     particle->speed *= 20; // gravity
105
106     particles.push_back(particle);
107   }
108 }
109
110 void
111 SnowParticleSystem::parse(const lisp::Lisp& reader)
112 {
113   reader.get("z-pos", z_pos);
114 }
115
116 void
117 SnowParticleSystem::write(lisp::Writer& writer)
118 {
119   writer.start_list("particles-snow");
120   writer.write_int("z-pos", z_pos);
121   writer.end_list("particles-snow");
122 }
123
124 SnowParticleSystem::~SnowParticleSystem()
125 {
126   for(int i=0;i<3;++i)
127     delete snowimages[i];
128 }
129
130 void SnowParticleSystem::update(float elapsed_time)
131 {
132   std::vector<Particle*>::iterator i;
133
134   for(i = particles.begin(); i != particles.end(); ++i) {
135     SnowParticle* particle = (SnowParticle*) *i;
136     float anchor_delta;
137
138     particle->pos.y += particle->speed * elapsed_time;
139     particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
140     
141     anchor_delta = (particle->anchorx - particle->pos.x);
142     particle->wobble += (4 * anchor_delta * 0.05) + systemRandom.randf(-0.5, 0.5);
143     particle->wobble *= 0.99;
144     particle->anchorx += particle->drift_speed * elapsed_time;
145   }
146 }
147
148 //FIXME: Sometimes both ghosts have the same image
149 //       Ghosts don't change their movement pattern - not random
150 GhostParticleSystem::GhostParticleSystem()
151 {
152   ghosts[0] = new Surface("images/objects/particles/ghost0.png");
153   ghosts[1] = new Surface("images/objects/particles/ghost1.png");
154
155   virtual_width = SCREEN_WIDTH * 2;
156
157   // create two ghosts
158   size_t ghostcount = 2;
159   for(size_t i=0; i<ghostcount; ++i) {
160     GhostParticle* particle = new GhostParticle;
161     particle->pos.x = systemRandom.randf(virtual_width);
162     particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
163     int size = systemRandom.rand(2);
164     particle->texture = ghosts[size];
165     do {
166       particle->speed = size*.2 + systemRandom.randf(3.6);
167     } while(particle->speed < 1);
168     particle->speed *= 50;
169     particles.push_back(particle);
170   }
171 }
172
173 void
174 GhostParticleSystem::parse(const lisp::Lisp& reader)
175 {
176   reader.get("z-pos", z_pos);
177 }
178
179 void
180 GhostParticleSystem::write(lisp::Writer& writer)
181 {
182   writer.start_list("particles-ghosts");
183   writer.write_int("z-pos", z_pos);
184   writer.end_list("particles-ghosts");
185 }
186
187 GhostParticleSystem::~GhostParticleSystem()
188 {
189   for(int i=0;i<2;++i)
190     delete ghosts[i];
191 }
192
193 void GhostParticleSystem::update(float elapsed_time)
194 {
195   std::vector<Particle*>::iterator i;
196   for(i = particles.begin(); i != particles.end(); ++i) {
197     GhostParticle* particle = (GhostParticle*) *i;
198     particle->pos.y -= particle->speed * elapsed_time;
199     particle->pos.x -= particle->speed * elapsed_time;
200     if(particle->pos.y > SCREEN_HEIGHT) {
201       particle->pos.y = fmodf(particle->pos.y , virtual_height);
202       particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
203     }
204   }
205 }
206
207 CloudParticleSystem::CloudParticleSystem()
208 {
209   cloudimage = new Surface("images/objects/particles/cloud.png");
210
211   virtual_width = 2000.0;
212
213   // create some random clouds
214   for(size_t i=0; i<15; ++i) {
215     CloudParticle* particle = new CloudParticle;
216     particle->pos.x = systemRandom.rand(static_cast<int>(virtual_width));
217     particle->pos.y = systemRandom.rand(static_cast<int>(virtual_height));
218     particle->texture = cloudimage;
219     particle->speed = -systemRandom.randf(25.0, 54.0);
220
221     particles.push_back(particle);
222   }
223 }
224
225 void
226 CloudParticleSystem::parse(const lisp::Lisp& reader)
227 {
228   reader.get("z-pos", z_pos);
229 }
230
231 void
232 CloudParticleSystem::write(lisp::Writer& writer)
233 {
234   writer.start_list("particles-clouds");
235   writer.write_int("z-pos", z_pos);
236   writer.end_list("particles-clouds");
237 }
238
239 CloudParticleSystem::~CloudParticleSystem()
240 {
241   delete cloudimage;
242 }
243
244 void CloudParticleSystem::update(float elapsed_time)
245 {
246   std::vector<Particle*>::iterator i;
247   for(i = particles.begin(); i != particles.end(); ++i) {
248     CloudParticle* particle = (CloudParticle*) *i;
249     particle->pos.x += particle->speed * elapsed_time;
250   }
251 }