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"
31 #include "object/camera.hpp"
33 ParticleSystem::ParticleSystem()
35 virtual_width = SCREEN_WIDTH;
36 virtual_height = SCREEN_HEIGHT;
37 layer = LAYER_BACKGROUND1;
40 ParticleSystem::~ParticleSystem()
42 std::vector<Particle*>::iterator i;
43 for(i = particles.begin(); i != particles.end(); ++i) {
48 void ParticleSystem::draw(DrawingContext& context)
50 float scrollx = context.get_translation().x;
51 float scrolly = context.get_translation().y;
53 context.push_transform();
54 context.set_translation(Vector(0,0));
56 std::vector<Particle*>::iterator i;
57 for(i = particles.begin(); i != particles.end(); ++i) {
58 Particle* particle = *i;
60 // remap x,y coordinates onto screencoordinates
62 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
63 if(pos.x < 0) pos.x += virtual_width;
64 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
65 if(pos.y < 0) pos.y += virtual_height;
67 if(pos.x > SCREEN_WIDTH) pos.x -= virtual_width;
68 if(pos.y > SCREEN_HEIGHT) pos.y -= virtual_height;
69 context.draw_surface(particle->texture, pos, layer);
72 context.pop_transform();
75 SnowParticleSystem::SnowParticleSystem()
77 snowimages[0] = new Surface("images/objects/particles/snow0.png");
78 snowimages[1] = new Surface("images/objects/particles/snow1.png");
79 snowimages[2] = new Surface("images/objects/particles/snow2.png");
81 virtual_width = SCREEN_WIDTH * 2;
83 // create some random snowflakes
84 size_t snowflakecount = size_t(virtual_width/10.0);
85 for(size_t i=0; i<snowflakecount; ++i) {
86 SnowParticle* particle = new SnowParticle;
87 particle->pos.x = fmodf(rand(), virtual_width);
88 particle->pos.y = fmodf(rand(), SCREEN_HEIGHT);
89 int snowsize = rand() % 3;
90 particle->texture = snowimages[snowsize];
92 particle->speed = snowsize*.2 + (float(rand()%10)*.4);
93 } while(particle->speed < 1);
94 particle->speed *= 10; // gravity
96 particles.push_back(particle);
101 SnowParticleSystem::parse(const lisp::Lisp& reader)
103 reader.get("layer", layer);
107 SnowParticleSystem::write(lisp::Writer& writer)
109 writer.start_list("particles-snow");
110 writer.write_int("layer", layer);
111 writer.end_list("particles-snow");
114 SnowParticleSystem::~SnowParticleSystem()
117 delete snowimages[i];
120 void SnowParticleSystem::update(float elapsed_time)
122 std::vector<Particle*>::iterator i;
123 for(i = particles.begin(); i != particles.end(); ++i) {
124 SnowParticle* particle = (SnowParticle*) *i;
125 particle->pos.y += particle->speed * elapsed_time;
126 if(particle->pos.y > SCREEN_HEIGHT + Sector::current()->camera->get_translation().y) {
127 particle->pos.y = fmodf(particle->pos.y , virtual_height);
128 particle->pos.x = rand() % int(virtual_width);
133 //FIXME: Sometimes both ghosts have the same image
134 // Ghosts don't change their movement pattern - not random
135 GhostParticleSystem::GhostParticleSystem()
137 ghosts[0] = new Surface("images/objects/particles/ghost0.png");
138 ghosts[1] = new Surface("images/objects/particles/ghost1.png");
140 virtual_width = SCREEN_WIDTH * 2;
143 size_t ghostcount = 2;
144 for(size_t i=0; i<ghostcount; ++i) {
145 GhostParticle* particle = new GhostParticle;
146 particle->pos.x = fmodf(rand(), virtual_width);
147 particle->pos.y = fmodf(rand(), SCREEN_HEIGHT);
148 int size = rand() % 2;
149 particle->texture = ghosts[size];
151 particle->speed = size*.2 + (float(rand()%10)*.4);
152 } while(particle->speed < 1);
153 particle->speed *= 50;
154 particles.push_back(particle);
159 GhostParticleSystem::parse(const lisp::Lisp& reader)
161 reader.get("layer", layer);
165 GhostParticleSystem::write(lisp::Writer& writer)
167 writer.start_list("particles-ghosts");
168 writer.write_int("layer", layer);
169 writer.end_list("particles-ghosts");
172 GhostParticleSystem::~GhostParticleSystem()
178 void GhostParticleSystem::update(float elapsed_time)
180 std::vector<Particle*>::iterator i;
181 for(i = particles.begin(); i != particles.end(); ++i) {
182 GhostParticle* particle = (GhostParticle*) *i;
183 particle->pos.y -= particle->speed * elapsed_time;
184 particle->pos.x -= particle->speed * elapsed_time;
185 if(particle->pos.y > SCREEN_HEIGHT) {
186 particle->pos.y = fmodf(particle->pos.y , virtual_height);
187 particle->pos.x = rand() % int(virtual_width);
192 CloudParticleSystem::CloudParticleSystem()
194 cloudimage = new Surface("images/objects/particles/cloud.png");
196 virtual_width = 2000.0;
198 // create some random clouds
199 for(size_t i=0; i<15; ++i) {
200 CloudParticle* particle = new CloudParticle;
201 particle->pos.x = rand() % int(virtual_width);
202 particle->pos.y = rand() % int(virtual_height);
203 particle->texture = cloudimage;
204 particle->speed = -float(25 + rand() % 30);
206 particles.push_back(particle);
211 CloudParticleSystem::parse(const lisp::Lisp& reader)
213 reader.get("layer", layer);
217 CloudParticleSystem::write(lisp::Writer& writer)
219 writer.start_list("particles-clouds");
220 writer.write_int("layer", layer);
221 writer.end_list("particles-clouds");
224 CloudParticleSystem::~CloudParticleSystem()
229 void CloudParticleSystem::update(float elapsed_time)
231 std::vector<Particle*>::iterator i;
232 for(i = particles.begin(); i != particles.end(); ++i) {
233 CloudParticle* particle = (CloudParticle*) *i;
234 particle->pos.x += particle->speed * elapsed_time;