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()
37 virtual_width = SCREEN_WIDTH;
38 virtual_height = SCREEN_HEIGHT;
39 z_pos = LAYER_BACKGROUND1;
42 ParticleSystem::~ParticleSystem()
44 std::vector<Particle*>::iterator i;
45 for(i = particles.begin(); i != particles.end(); ++i) {
50 void ParticleSystem::draw(DrawingContext& context)
52 float scrollx = context.get_translation().x;
53 float scrolly = context.get_translation().y;
55 context.push_transform();
56 context.set_translation(Vector(0,0));
58 std::vector<Particle*>::iterator i;
59 for(i = particles.begin(); i != particles.end(); ++i) {
60 Particle* particle = *i;
62 // remap x,y coordinates onto screencoordinates
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;
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);
74 context.pop_transform();
77 SnowParticleSystem::SnowParticleSystem()
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");
83 virtual_width = SCREEN_WIDTH * 2;
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];
94 particle->speed = snowsize*.2 + systemRandom.randf(3.6);
95 } while(particle->speed < 1);
96 particle->speed *= 10; // gravity
98 particles.push_back(particle);
103 SnowParticleSystem::parse(const lisp::Lisp& reader)
105 reader.get("z-pos", z_pos);
109 SnowParticleSystem::write(lisp::Writer& writer)
111 writer.start_list("particles-snow");
112 writer.write_int("z-pos", z_pos);
113 writer.end_list("particles-snow");
116 SnowParticleSystem::~SnowParticleSystem()
119 delete snowimages[i];
122 void SnowParticleSystem::update(float elapsed_time)
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);
135 //FIXME: Sometimes both ghosts have the same image
136 // Ghosts don't change their movement pattern - not random
137 GhostParticleSystem::GhostParticleSystem()
139 ghosts[0] = new Surface("images/objects/particles/ghost0.png");
140 ghosts[1] = new Surface("images/objects/particles/ghost1.png");
142 virtual_width = SCREEN_WIDTH * 2;
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];
153 particle->speed = size*.2 + systemRandom.randf(3.6);
154 } while(particle->speed < 1);
155 particle->speed *= 50;
156 particles.push_back(particle);
161 GhostParticleSystem::parse(const lisp::Lisp& reader)
163 reader.get("z-pos", z_pos);
167 GhostParticleSystem::write(lisp::Writer& writer)
169 writer.start_list("particles-ghosts");
170 writer.write_int("z-pos", z_pos);
171 writer.end_list("particles-ghosts");
174 GhostParticleSystem::~GhostParticleSystem()
180 void GhostParticleSystem::update(float elapsed_time)
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);
194 CloudParticleSystem::CloudParticleSystem()
196 cloudimage = new Surface("images/objects/particles/cloud.png");
198 virtual_width = 2000.0;
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);
208 particles.push_back(particle);
213 CloudParticleSystem::parse(const lisp::Lisp& reader)
215 reader.get("z-pos", z_pos);
219 CloudParticleSystem::write(lisp::Writer& writer)
221 writer.start_list("particles-clouds");
222 writer.write_int("z-pos", z_pos);
223 writer.end_list("particles-clouds");
226 CloudParticleSystem::~CloudParticleSystem()
231 void CloudParticleSystem::update(float elapsed_time)
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;