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
206 /*if(object->get_movement().x >= 0) {
207 x1 = object->get_pos().x;
208 x2 = object->get_bbox().p2.x + object->get_movement().x;
210 x1 = object->get_pos().x + object->get_movement().x;
211 x2 = object->get_bbox().p2.x;
215 if(object->get_movement().y >= 0) {
216 y1 = object->get_pos().y;
217 y2 = object->get_bbox().p2.y + object->get_movement().y;
219 y1 = object->get_pos().y + object->get_movement().y;
220 y2 = object->get_bbox().p2.y;
223 x2 = x1 + 32 + movement.x;
225 y2 = y1 + 32 + movement.y;
227 // test with all tiles in this rectangle
228 int starttilex = int(x1-1) / 32;
229 int starttiley = int(y1-1) / 32;
230 int max_x = int(x2+1);
231 int max_y = int(y2+1);
233 CollisionHit temphit, hit;
234 Rect dest = Rect(x1, y1, x2, y2);
236 hit.time = -1; // represents an invalid value
237 for(int x = starttilex; x*32 < max_x; ++x) {
238 for(int y = starttiley; y*32 < max_y; ++y) {
239 const Tile* tile = solids->get_tile(x, y);
242 // skip non-solid tiles
243 if(!(tile->getAttributes() & Tile::SOLID))
246 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
248 Vector p1(x*32, y*32);
249 Vector p2((x+1)*32, (y+1)*32);
250 triangle = AATriangle(p1, p2, tile->getData());
252 if(Collision::rectangle_aatriangle(temphit, dest, movement,
254 if(temphit.time > hit.time)
257 } else { // normal rectangular tile
258 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
259 if(Collision::rectangle_rectangle(temphit, dest,
261 if(temphit.time > hit.time)
268 // did we collide at all?
270 return false; else return true;
273 CloudParticleSystem::CloudParticleSystem()
275 cloudimage = new Surface(datadir + "/images/objects/particles/cloud.png", true);
277 virtual_width = 2000.0;
279 // create some random clouds
280 for(size_t i=0; i<15; ++i) {
281 CloudParticle* particle = new CloudParticle;
282 particle->pos.x = rand() % int(virtual_width);
283 particle->pos.y = rand() % int(virtual_height);
284 particle->texture = cloudimage;
285 particle->speed = -float(25 + rand() % 30);
287 particles.push_back(particle);
292 CloudParticleSystem::parse(const lisp::Lisp& reader)
294 reader.get("layer", layer);
298 CloudParticleSystem::write(lisp::Writer& writer)
300 writer.start_list("particles-clouds");
301 writer.write_int("layer", layer);
302 writer.end_list("particles-clouds");
305 CloudParticleSystem::~CloudParticleSystem()
310 void CloudParticleSystem::update(float elapsed_time)
312 std::vector<Particle*>::iterator i;
313 for(i = particles.begin(); i != particles.end(); ++i) {
314 CloudParticle* particle = (CloudParticle*) *i;
315 particle->pos.x += particle->speed * elapsed_time;