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.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"
37 #include "object/camera.h"
38 #include "object/rainsplash.h"
39 #include "badguy/bomb.h"
41 //TODO: Dynamically create splashes at collision spots
42 // Find a way to make rain collide with objects like bonus blocks
43 // Add an option to set rain strength
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;
84 // test with all tiles in this rectangle
85 int starttilex = int(x1-1) / 32;
86 int starttiley = int(y1-1) / 32;
87 int max_x = int(x2+1);
88 int max_y = int(y2+1);
90 CollisionHit temphit, hit;
91 Rect dest = Rect(x1, y1, x2, y2);
93 hit.time = -1; // represents an invalid value
94 for(int x = starttilex; x*32 < max_x; ++x) {
95 for(int y = starttiley; y*32 < max_y; ++y) {
96 const Tile* tile = solids->get_tile(x, y);
99 // skip non-solid tiles
100 if(!(tile->getAttributes() & Tile::SOLID))
103 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
105 Vector p1(x*32, y*32);
106 Vector p2((x+1)*32, (y+1)*32);
107 triangle = AATriangle(p1, p2, tile->getData());
109 if(Collision::rectangle_aatriangle(temphit, dest, movement,
111 if(temphit.time > hit.time)
114 } else { // normal rectangular tile
115 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
116 if(Collision::rectangle_rectangle(temphit, dest,
118 if(temphit.time > hit.time)
125 // did we collide at all?
127 return false; else return true;
130 RainParticleSystem::RainParticleSystem()
132 rainimages[0] = new Surface(datadir+"/images/objects/particles/rain0.png", true);
133 rainimages[1] = new Surface(datadir+"/images/objects/particles/rain1.png", true);
135 virtual_width = SCREEN_WIDTH * 2;
137 // create some random raindrops
138 size_t raindropcount = size_t(virtual_width/6.0);
139 for(size_t i=0; i<raindropcount; ++i) {
140 RainParticle* particle = new RainParticle;
141 particle->pos.x = rand() % int(virtual_width);
142 particle->pos.y = rand() % int(virtual_height);
143 int rainsize = rand() % 2;
144 particle->texture = rainimages[rainsize];
146 particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
147 } while(particle->speed < 1);
148 particle->speed *= 10; // gravity
150 particles.push_back(particle);
155 RainParticleSystem::parse(const lisp::Lisp& reader)
157 reader.get("layer", layer);
161 RainParticleSystem::write(lisp::Writer& writer)
163 writer.start_list("particles-rain");
164 writer.write_int("layer", layer);
165 writer.end_list("particles-rain");
168 RainParticleSystem::~RainParticleSystem()
171 delete rainimages[i];
174 void RainParticleSystem::update(float elapsed_time)
176 std::vector<Particle*>::iterator i;
178 i = particles.begin(); i != particles.end(); ++i) {
179 RainParticle* particle = (RainParticle*) *i;
180 float movement = particle->speed * elapsed_time;
181 float abs_x = Sector::current()->camera->get_translation().x;
182 float abs_y = Sector::current()->camera->get_translation().y;
183 particle->pos.y += movement;
184 particle->pos.x -= movement;
185 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (collision(particle, Vector(-movement, movement)))) {
187 if (particle->pos.y <= SCREEN_HEIGHT + abs_y){
188 //TODO: Find out at which side of the tile the collision happens
189 bool vertical = false;
190 int splash_x, splash_y;
192 splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
193 splash_y = int(particle->pos.y);
196 splash_x = int(particle->pos.x);
197 splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
199 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
201 int new_x = (rand() % int(virtual_width)) + int(abs_x);
203 //FIXME: Don't move particles over solid tiles
204 particle->pos.x = new_x;
205 particle->pos.y = new_y;
210 CometParticleSystem::CometParticleSystem()
212 cometimages[0] = new Surface(datadir+"/images/creatures/mr_bomb/exploding-left-0.png", true);
213 cometimages[1] = new Surface(datadir+"/images/creatures/mr_bomb/exploding-left-0.png", true);
215 virtual_width = SCREEN_WIDTH * 2;
217 // create some random comets
218 size_t cometcount = 2;
219 for(size_t i=0; i<cometcount; ++i) {
220 CometParticle* particle = new CometParticle;
221 particle->pos.x = rand() % int(virtual_width);
222 particle->pos.y = rand() % int(virtual_height);
223 int cometsize = rand() % 2;
224 particle->texture = cometimages[cometsize];
226 particle->speed = (cometsize+1)*30 + (float(rand()%10)*.4);
227 } while(particle->speed < 1);
228 particle->speed *= 10; // gravity
230 particles.push_back(particle);
235 CometParticleSystem::parse(const lisp::Lisp& reader)
237 reader.get("layer", layer);
241 CometParticleSystem::write(lisp::Writer& writer)
243 writer.start_list("particles-comets");
244 writer.write_int("layer", layer);
245 writer.end_list("particles-comets");
248 CometParticleSystem::~CometParticleSystem()
251 delete cometimages[i];
254 void CometParticleSystem::update(float elapsed_time)
256 std::vector<Particle*>::iterator i;
258 i = particles.begin(); i != particles.end(); ++i) {
259 CometParticle* particle = (CometParticle*) *i;
260 float movement = particle->speed * elapsed_time;
261 float abs_x = Sector::current()->camera->get_translation().x;
262 float abs_y = Sector::current()->camera->get_translation().y;
263 particle->pos.y += movement;
264 particle->pos.x -= movement;
265 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (collision(particle, Vector(-movement, movement)))) {
266 if (particle->pos.y <= SCREEN_HEIGHT + abs_y) {
267 Sector::current()->add_object(new Bomb(particle->pos, LEFT));
269 int new_x = (rand() % int(virtual_width)) + int(abs_x);
271 //FIXME: Don't move particles over solid tiles
272 particle->pos.x = new_x;
273 particle->pos.y = new_y;