4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
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"
32 #include "object/camera.hpp"
33 #include "random_generator.hpp"
35 ParticleSystem::ParticleSystem(float max_particle_size)
36 : max_particle_size(max_particle_size)
38 virtual_width = SCREEN_WIDTH + max_particle_size * 2;
39 virtual_height = SCREEN_HEIGHT + max_particle_size *2;
40 z_pos = LAYER_BACKGROUND1;
43 ParticleSystem::~ParticleSystem()
45 std::vector<Particle*>::iterator i;
46 for(i = particles.begin(); i != particles.end(); ++i) {
51 void ParticleSystem::draw(DrawingContext& context)
53 float scrollx = context.get_translation().x;
54 float scrolly = context.get_translation().y;
56 context.push_transform();
57 context.set_translation(Vector(max_particle_size,max_particle_size));
59 std::vector<Particle*>::iterator i;
60 for(i = particles.begin(); i != particles.end(); ++i) {
61 Particle* particle = *i;
63 // remap x,y coordinates onto screencoordinates
66 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
67 if(pos.x < 0) pos.x += virtual_width;
69 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
70 if(pos.y < 0) pos.y += virtual_height;
73 //if(pos.x > virtual_width) pos.x -= virtual_width;
74 //if(pos.y > virtual_height) pos.y -= virtual_height;
76 context.draw_surface(particle->texture, pos, z_pos);
79 context.pop_transform();
82 SnowParticleSystem::SnowParticleSystem()
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");
88 virtual_width = SCREEN_WIDTH * 2;
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);
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;
102 particle->texture = snowimages[snowsize];
104 particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
105 particle->speed *= 20; // gravity
107 particles.push_back(particle);
112 SnowParticleSystem::parse(const lisp::Lisp& reader)
114 reader.get("z-pos", z_pos);
118 SnowParticleSystem::write(lisp::Writer& writer)
120 writer.start_list("particles-snow");
121 writer.write_int("z-pos", z_pos);
122 writer.end_list("particles-snow");
125 SnowParticleSystem::~SnowParticleSystem()
128 delete snowimages[i];
131 void SnowParticleSystem::update(float elapsed_time)
133 std::vector<Particle*>::iterator i;
135 for(i = particles.begin(); i != particles.end(); ++i) {
136 SnowParticle* particle = (SnowParticle*) *i;
139 particle->pos.y += particle->speed * elapsed_time;
140 particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
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;
149 //FIXME: Sometimes both ghosts have the same image
150 // Ghosts don't change their movement pattern - not random
151 GhostParticleSystem::GhostParticleSystem()
153 ghosts[0] = new Surface("images/objects/particles/ghost0.png");
154 ghosts[1] = new Surface("images/objects/particles/ghost1.png");
156 virtual_width = SCREEN_WIDTH * 2;
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];
167 particle->speed = size*.2 + systemRandom.randf(3.6);
168 } while(particle->speed < 1);
169 particle->speed *= 50;
170 particles.push_back(particle);
175 GhostParticleSystem::parse(const lisp::Lisp& reader)
177 reader.get("z-pos", z_pos);
181 GhostParticleSystem::write(lisp::Writer& writer)
183 writer.start_list("particles-ghosts");
184 writer.write_int("z-pos", z_pos);
185 writer.end_list("particles-ghosts");
188 GhostParticleSystem::~GhostParticleSystem()
194 void GhostParticleSystem::update(float elapsed_time)
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));
208 CloudParticleSystem::CloudParticleSystem()
209 : ParticleSystem(128)
211 cloudimage = new Surface("images/objects/particles/cloud.png");
213 virtual_width = 2000.0;
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);
223 particles.push_back(particle);
228 CloudParticleSystem::parse(const lisp::Lisp& reader)
230 reader.get("z-pos", z_pos);
234 CloudParticleSystem::write(lisp::Writer& writer)
236 writer.start_list("particles-clouds");
237 writer.write_int("z-pos", z_pos);
238 writer.end_list("particles-clouds");
241 CloudParticleSystem::~CloudParticleSystem()
246 void CloudParticleSystem::update(float elapsed_time)
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;