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.h"
25 #include "video/drawing_context.h"
26 #include "lisp/parser.h"
27 #include "lisp/lisp.h"
28 #include "lisp/writer.h"
29 #include "resources.h"
34 #include "math/aatriangle.h"
35 #include "collision.h"
36 #include "collision_hit.h"
39 ParticleSystem::ParticleSystem()
41 virtual_width = SCREEN_WIDTH;
42 virtual_height = SCREEN_HEIGHT;
43 layer = LAYER_BACKGROUND1;
46 ParticleSystem::~ParticleSystem()
48 std::vector<Particle*>::iterator i;
49 for(i = particles.begin(); i != particles.end(); ++i) {
54 void ParticleSystem::draw(DrawingContext& context)
56 float scrollx = context.get_translation().x;
57 float scrolly = context.get_translation().y;
59 context.push_transform();
60 context.set_translation(Vector(0,0));
62 std::vector<Particle*>::iterator i;
63 for(i = particles.begin(); i != particles.end(); ++i) {
64 Particle* particle = *i;
66 // remap x,y coordinates onto screencoordinates
68 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
69 if(pos.x < 0) pos.x += virtual_width;
70 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
71 if(pos.y < 0) pos.y += virtual_height;
73 if(pos.x > SCREEN_WIDTH) pos.x -= virtual_width;
74 if(pos.y > SCREEN_HEIGHT) pos.y -= virtual_height;
75 context.draw_surface(particle->texture, pos, layer);
78 context.pop_transform();
81 SnowParticleSystem::SnowParticleSystem()
83 snowimages[0] = new Surface(datadir+"/images/objects/particles/snow0.png", true);
84 snowimages[1] = new Surface(datadir+"/images/objects/particles/snow1.png", true);
85 snowimages[2] = new Surface(datadir+"/images/objects/particles/snow2.png", true);
87 virtual_width = SCREEN_WIDTH * 2;
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 particle->pos.x = rand() % int(virtual_width);
94 particle->pos.y = rand() % SCREEN_HEIGHT;
95 int snowsize = rand() % 3;
96 particle->texture = snowimages[snowsize];
98 particle->speed = snowsize*.2 + (float(rand()%10)*.4);
99 } while(particle->speed < 1);
100 particle->speed *= 10; // gravity
102 particles.push_back(particle);
107 SnowParticleSystem::parse(const lisp::Lisp& reader)
109 reader.get("layer", layer);
113 SnowParticleSystem::write(lisp::Writer& writer)
115 writer.start_list("particles-snow");
116 writer.write_int("layer", layer);
117 writer.end_list("particles-snow");
120 SnowParticleSystem::~SnowParticleSystem()
123 delete snowimages[i];
126 void SnowParticleSystem::update(float elapsed_time)
128 std::vector<Particle*>::iterator i;
129 for(i = particles.begin(); i != particles.end(); ++i) {
130 SnowParticle* particle = (SnowParticle*) *i;
131 particle->pos.y += particle->speed * elapsed_time;
132 if(particle->pos.y > SCREEN_HEIGHT) {
133 particle->pos.y = fmodf(particle->pos.y , virtual_height);
134 particle->pos.x = rand() % int(virtual_width);
139 RainParticleSystem::RainParticleSystem()
141 rainimages[0] = new Surface(datadir+"/images/objects/particles/rain0.png", true);
142 rainimages[1] = new Surface(datadir+"/images/objects/particles/rain1.png", true);
144 virtual_width = SCREEN_WIDTH * 2;
146 // create some random raindrops
147 size_t raindropcount = size_t(virtual_width/8.0);
148 for(size_t i=0; i<raindropcount; ++i) {
149 RainParticle* particle = new RainParticle;
150 particle->pos.x = rand() % int(virtual_width);
151 particle->pos.y = rand() % int(virtual_height);
152 int rainsize = rand() % 2;
153 particle->texture = rainimages[rainsize];
155 particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
156 } while(particle->speed < 1);
157 particle->speed *= 10; // gravity
159 particles.push_back(particle);
164 RainParticleSystem::parse(const lisp::Lisp& reader)
166 reader.get("layer", layer);
170 RainParticleSystem::write(lisp::Writer& writer)
172 writer.start_list("particles-rain");
173 writer.write_int("layer", layer);
174 writer.end_list("particles-rain");
177 RainParticleSystem::~RainParticleSystem()
180 delete rainimages[i];
183 void RainParticleSystem::update(float elapsed_time)
185 std::vector<Particle*>::iterator i;
187 i = particles.begin(); i != particles.end(); ++i) {
188 RainParticle* particle = (RainParticle*) *i;
189 float movement = particle->speed * elapsed_time;
190 particle->pos.y += movement;
191 particle->pos.x -= movement;
192 if ((particle->pos.y > SCREEN_HEIGHT) || (collision(particle, Vector(-movement, movement)))) {
194 particle->pos.x = rand() % int(virtual_width);
200 RainParticleSystem::collision(RainParticle* object, Vector movement)
202 TileMap* solids = Sector::current()->solids;
203 // calculate rectangle where the object will move
207 x2 = x1 + 32 + movement.x;
209 y2 = y1 + 32 + movement.y;
211 // test with all tiles in this rectangle
212 int starttilex = int(x1-1) / 32;
213 int starttiley = int(y1-1) / 32;
214 int max_x = int(x2+1);
215 int max_y = int(y2+1);
217 CollisionHit temphit, hit;
218 Rect dest = Rect(x1, y1, x2, y2);
220 hit.time = -1; // represents an invalid value
221 for(int x = starttilex; x*32 < max_x; ++x) {
222 for(int y = starttiley; y*32 < max_y; ++y) {
223 const Tile* tile = solids->get_tile(x, y);
226 // skip non-solid tiles
227 if(!(tile->getAttributes() & Tile::SOLID))
230 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
232 Vector p1(x*32, y*32);
233 Vector p2((x+1)*32, (y+1)*32);
234 triangle = AATriangle(p1, p2, tile->getData());
236 if(Collision::rectangle_aatriangle(temphit, dest, movement,
238 if(temphit.time > hit.time)
241 } else { // normal rectangular tile
242 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
243 if(Collision::rectangle_rectangle(temphit, dest,
245 if(temphit.time > hit.time)
252 // did we collide at all?
254 return false; else return true;
257 CloudParticleSystem::CloudParticleSystem()
259 cloudimage = new Surface(datadir + "/images/objects/particles/cloud.png", true);
261 virtual_width = 2000.0;
263 // create some random clouds
264 for(size_t i=0; i<15; ++i) {
265 CloudParticle* particle = new CloudParticle;
266 particle->pos.x = rand() % int(virtual_width);
267 particle->pos.y = rand() % int(virtual_height);
268 particle->texture = cloudimage;
269 particle->speed = -float(25 + rand() % 30);
271 particles.push_back(particle);
276 CloudParticleSystem::parse(const lisp::Lisp& reader)
278 reader.get("layer", layer);
282 CloudParticleSystem::write(lisp::Writer& writer)
284 writer.start_list("particles-clouds");
285 writer.write_int("layer", layer);
286 writer.end_list("particles-clouds");
289 CloudParticleSystem::~CloudParticleSystem()
294 void CloudParticleSystem::update(float elapsed_time)
296 std::vector<Particle*>::iterator i;
297 for(i = particles.begin(); i != particles.end(); ++i) {
298 CloudParticle* particle = (CloudParticle*) *i;
299 particle->pos.x += particle->speed * elapsed_time;