4 // Copyright (C) 2004 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.
24 #include "particlesystem.hpp"
25 #include "video/drawing_context.hpp"
26 #include "lisp/parser.hpp"
27 #include "lisp/lisp.hpp"
28 #include "lisp/writer.hpp"
29 #include "resources.hpp"
32 ParticleSystem::ParticleSystem()
34 virtual_width = SCREEN_WIDTH;
35 virtual_height = SCREEN_HEIGHT;
36 layer = LAYER_BACKGROUND1;
39 ParticleSystem::~ParticleSystem()
41 std::vector<Particle*>::iterator i;
42 for(i = particles.begin(); i != particles.end(); ++i) {
47 void ParticleSystem::draw(DrawingContext& context)
49 float scrollx = context.get_translation().x;
50 float scrolly = context.get_translation().y;
52 context.push_transform();
53 context.set_translation(Vector(0,0));
55 std::vector<Particle*>::iterator i;
56 for(i = particles.begin(); i != particles.end(); ++i) {
57 Particle* particle = *i;
59 // remap x,y coordinates onto screencoordinates
61 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
62 if(pos.x < 0) pos.x += virtual_width;
63 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
64 if(pos.y < 0) pos.y += virtual_height;
66 if(pos.x > SCREEN_WIDTH) pos.x -= virtual_width;
67 if(pos.y > SCREEN_HEIGHT) pos.y -= virtual_height;
68 context.draw_surface(particle->texture, pos, layer);
71 context.pop_transform();
74 SnowParticleSystem::SnowParticleSystem()
76 snowimages[0] = new Surface("images/objects/particles/snow0.png");
77 snowimages[1] = new Surface("images/objects/particles/snow1.png");
78 snowimages[2] = new Surface("images/objects/particles/snow2.png");
80 virtual_width = SCREEN_WIDTH * 2;
82 // create some random snowflakes
83 size_t snowflakecount = size_t(virtual_width/10.0);
84 for(size_t i=0; i<snowflakecount; ++i) {
85 SnowParticle* particle = new SnowParticle;
86 particle->pos.x = fmodf(rand(), virtual_width);
87 particle->pos.y = fmodf(rand(), SCREEN_HEIGHT);
88 int snowsize = rand() % 3;
89 particle->texture = snowimages[snowsize];
91 particle->speed = snowsize*.2 + (float(rand()%10)*.4);
92 } while(particle->speed < 1);
93 particle->speed *= 10; // gravity
95 particles.push_back(particle);
100 SnowParticleSystem::parse(const lisp::Lisp& reader)
102 reader.get("layer", layer);
106 SnowParticleSystem::write(lisp::Writer& writer)
108 writer.start_list("particles-snow");
109 writer.write_int("layer", layer);
110 writer.end_list("particles-snow");
113 SnowParticleSystem::~SnowParticleSystem()
116 delete snowimages[i];
119 void SnowParticleSystem::update(float elapsed_time)
121 std::vector<Particle*>::iterator i;
122 for(i = particles.begin(); i != particles.end(); ++i) {
123 SnowParticle* particle = (SnowParticle*) *i;
124 particle->pos.y += particle->speed * elapsed_time;
125 if(particle->pos.y > SCREEN_HEIGHT) {
126 particle->pos.y = fmodf(particle->pos.y , virtual_height);
127 particle->pos.x = rand() % int(virtual_width);
132 //FIXME: Sometimes both ghosts have the same image
133 // Ghosts don't change their movement pattern - not random
134 GhostParticleSystem::GhostParticleSystem()
136 ghosts[0] = new Surface("images/objects/particles/ghost0.png");
137 ghosts[1] = new Surface("images/objects/particles/ghost1.png");
139 virtual_width = SCREEN_WIDTH * 2;
142 size_t ghostcount = 2;
143 for(size_t i=0; i<ghostcount; ++i) {
144 GhostParticle* particle = new GhostParticle;
145 particle->pos.x = fmodf(rand(), virtual_width);
146 particle->pos.y = fmodf(rand(), SCREEN_HEIGHT);
147 int size = rand() % 2;
148 particle->texture = ghosts[size];
150 particle->speed = size*.2 + (float(rand()%10)*.4);
151 } while(particle->speed < 1);
152 particle->speed *= 50;
153 particles.push_back(particle);
158 GhostParticleSystem::parse(const lisp::Lisp& reader)
160 reader.get("layer", layer);
164 GhostParticleSystem::write(lisp::Writer& writer)
166 writer.start_list("particles-ghosts");
167 writer.write_int("layer", layer);
168 writer.end_list("particles-ghosts");
171 GhostParticleSystem::~GhostParticleSystem()
177 void GhostParticleSystem::update(float elapsed_time)
179 std::vector<Particle*>::iterator i;
180 for(i = particles.begin(); i != particles.end(); ++i) {
181 GhostParticle* particle = (GhostParticle*) *i;
182 particle->pos.y -= particle->speed * elapsed_time;
183 particle->pos.x -= particle->speed * elapsed_time;
184 if(particle->pos.y > SCREEN_HEIGHT) {
185 particle->pos.y = fmodf(particle->pos.y , virtual_height);
186 particle->pos.x = rand() % int(virtual_width);
191 CloudParticleSystem::CloudParticleSystem()
193 cloudimage = new Surface("images/objects/particles/cloud.png");
195 virtual_width = 2000.0;
197 // create some random clouds
198 for(size_t i=0; i<15; ++i) {
199 CloudParticle* particle = new CloudParticle;
200 particle->pos.x = rand() % int(virtual_width);
201 particle->pos.y = rand() % int(virtual_height);
202 particle->texture = cloudimage;
203 particle->speed = -float(25 + rand() % 30);
205 particles.push_back(particle);
210 CloudParticleSystem::parse(const lisp::Lisp& reader)
212 reader.get("layer", layer);
216 CloudParticleSystem::write(lisp::Writer& writer)
218 writer.start_list("particles-clouds");
219 writer.write_int("layer", layer);
220 writer.end_list("particles-clouds");
223 CloudParticleSystem::~CloudParticleSystem()
228 void CloudParticleSystem::update(float elapsed_time)
230 std::vector<Particle*>::iterator i;
231 for(i = particles.begin(); i != particles.end(); ++i) {
232 CloudParticle* particle = (CloudParticle*) *i;
233 particle->pos.x += particle->speed * elapsed_time;