1 // $Id: particlesystem.cpp 2470 2005-05-11 14:49:28Z wansti $
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_interactive.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"
33 #include "tilemap.hpp"
34 #include "math/aatriangle.hpp"
35 #include "collision.hpp"
36 #include "collision_hit.hpp"
37 #include "object/camera.hpp"
38 #include "object/rainsplash.hpp"
39 #include "badguy/bomb.hpp"
41 //TODO: Find a way to make rain collide with objects like bonus blocks
42 // Add an option to set rain strength
43 // Fix rain being "respawned" over solid tiles
44 ParticleSystem_Interactive::ParticleSystem_Interactive()
46 virtual_width = SCREEN_WIDTH;
47 virtual_height = SCREEN_HEIGHT;
51 ParticleSystem_Interactive::~ParticleSystem_Interactive()
53 std::vector<Particle*>::iterator i;
54 for(i = particles.begin(); i != particles.end(); ++i) {
59 void ParticleSystem_Interactive::draw(DrawingContext& context)
61 context.push_transform();
63 std::vector<Particle*>::iterator i;
64 for(i = particles.begin(); i != particles.end(); ++i) {
65 Particle* particle = *i;
66 context.draw_surface(particle->texture, particle->pos, layer);
69 context.pop_transform();
73 ParticleSystem_Interactive::collision(Particle* object, Vector movement)
75 TileMap* solids = Sector::current()->solids;
76 // calculate rectangle where the object will move
80 x2 = x1 + 32 + movement.x;
82 y2 = y1 + 32 + movement.y;
85 // test with all tiles in this rectangle
86 int starttilex = int(x1-1) / 32;
87 int starttiley = int(y1-1) / 32;
88 int max_x = int(x2+1);
89 int max_y = int(y2+1);
91 CollisionHit temphit, hit;
92 Rect dest = Rect(x1, y1, x2, y2);
94 hit.time = -1; // represents an invalid value
95 for(int x = starttilex; x*32 < max_x; ++x) {
96 for(int y = starttiley; y*32 < max_y; ++y) {
97 const Tile* tile = solids->get_tile(x, y);
100 // skip non-solid tiles, except water
101 if (tile->getAttributes() & Tile::WATER)
103 if(!water && !(tile->getAttributes() & Tile::SOLID))
106 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
108 Vector p1(x*32, y*32);
109 Vector p2((x+1)*32, (y+1)*32);
110 triangle = AATriangle(p1, p2, tile->getData());
112 if(Collision::rectangle_aatriangle(temphit, dest, movement,
114 if(temphit.time > hit.time)
117 } else { // normal rectangular tile
118 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
119 if(Collision::rectangle_rectangle(temphit, dest,
121 if(temphit.time > hit.time)
128 // did we collide at all?
130 return -1; //no collision
134 return 0; //collision with water tile - don't draw splash
136 if ((hit.normal.x == 1) && (hit.normal.y == 0))
137 return 2; //collision from right
138 else return 1; //collision from above
143 RainParticleSystem::RainParticleSystem()
145 rainimages[0] = new Surface("images/objects/particles/rain0.png");
146 rainimages[1] = new Surface("images/objects/particles/rain1.png");
148 virtual_width = SCREEN_WIDTH * 2;
150 // create some random raindrops
151 size_t raindropcount = size_t(virtual_width/6.0);
152 for(size_t i=0; i<raindropcount; ++i) {
153 RainParticle* particle = new RainParticle;
154 particle->pos.x = rand() % int(virtual_width);
155 particle->pos.y = rand() % int(virtual_height);
156 int rainsize = rand() % 2;
157 particle->texture = rainimages[rainsize];
159 particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
160 } while(particle->speed < 1);
161 particle->speed *= 10; // gravity
163 particles.push_back(particle);
168 RainParticleSystem::parse(const lisp::Lisp& reader)
170 reader.get("layer", layer);
174 RainParticleSystem::write(lisp::Writer& writer)
176 writer.start_list("particles-rain");
177 writer.write_int("layer", layer);
178 writer.end_list("particles-rain");
181 RainParticleSystem::~RainParticleSystem()
184 delete rainimages[i];
187 void RainParticleSystem::update(float elapsed_time)
189 std::vector<Particle*>::iterator i;
191 i = particles.begin(); i != particles.end(); ++i) {
192 RainParticle* particle = (RainParticle*) *i;
193 float movement = particle->speed * elapsed_time;
194 float abs_x = Sector::current()->camera->get_translation().x;
195 float abs_y = Sector::current()->camera->get_translation().y;
196 particle->pos.y += movement;
197 particle->pos.x -= movement;
198 int col = collision(particle, Vector(-movement, movement));
199 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
201 if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
202 bool vertical = (col == 2);
203 int splash_x, splash_y;
204 if (!vertical) { //check if collision happened from above
205 splash_x = int(particle->pos.x);
206 splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
207 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
209 // Uncomment the following to display vertical splashes, too
211 splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
212 splash_y = int(particle->pos.y);
213 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
216 int new_x = (rand() % int(virtual_width)) + int(abs_x);
218 //FIXME: Don't move particles over solid tiles
219 particle->pos.x = new_x;
220 particle->pos.y = new_y;
225 CometParticleSystem::CometParticleSystem()
227 cometimages[0] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
228 cometimages[1] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
230 virtual_width = SCREEN_WIDTH * 2;
232 // create some random comets
233 size_t cometcount = 2;
234 for(size_t i=0; i<cometcount; ++i) {
235 CometParticle* particle = new CometParticle;
236 particle->pos.x = rand() % int(virtual_width);
237 particle->pos.y = rand() % int(virtual_height);
238 int cometsize = rand() % 2;
239 particle->texture = cometimages[cometsize];
241 particle->speed = (cometsize+1)*30 + (float(rand()%10)*.4);
242 } while(particle->speed < 1);
243 particle->speed *= 10; // gravity
245 particles.push_back(particle);
250 CometParticleSystem::parse(const lisp::Lisp& reader)
252 reader.get("layer", layer);
256 CometParticleSystem::write(lisp::Writer& writer)
258 writer.start_list("particles-comets");
259 writer.write_int("layer", layer);
260 writer.end_list("particles-comets");
263 CometParticleSystem::~CometParticleSystem()
266 delete cometimages[i];
269 void CometParticleSystem::update(float elapsed_time)
271 std::vector<Particle*>::iterator i;
273 i = particles.begin(); i != particles.end(); ++i) {
274 CometParticle* particle = (CometParticle*) *i;
275 float movement = particle->speed * elapsed_time;
276 float abs_x = Sector::current()->camera->get_translation().x;
277 float abs_y = Sector::current()->camera->get_translation().y;
278 particle->pos.y += movement;
279 particle->pos.x -= movement;
280 int col = collision(particle, Vector(-movement, movement));
281 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
282 if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)) {
283 Sector::current()->add_object(new Bomb(particle->pos, LEFT));
285 int new_x = (rand() % int(virtual_width)) + int(abs_x);
287 //FIXME: Don't move particles over solid tiles
288 particle->pos.x = new_x;
289 particle->pos.y = new_y;