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_interactive.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"
34 #include "tilemap.hpp"
35 #include "math/aatriangle.hpp"
36 #include "collision.hpp"
37 #include "collision_hit.hpp"
38 #include "object/camera.hpp"
39 #include "object/rainsplash.hpp"
40 #include "badguy/bomb.hpp"
41 #include "random_generator.hpp"
43 //TODO: Find a way to make rain collide with objects like bonus blocks
44 // Add an option to set rain strength
45 // Fix rain being "respawned" over solid tiles
46 ParticleSystem_Interactive::ParticleSystem_Interactive()
48 virtual_width = SCREEN_WIDTH;
49 virtual_height = SCREEN_HEIGHT;
53 ParticleSystem_Interactive::~ParticleSystem_Interactive()
55 std::vector<Particle*>::iterator i;
56 for(i = particles.begin(); i != particles.end(); ++i) {
61 void ParticleSystem_Interactive::draw(DrawingContext& context)
63 context.push_transform();
65 std::vector<Particle*>::iterator i;
66 for(i = particles.begin(); i != particles.end(); ++i) {
67 Particle* particle = *i;
68 context.draw_surface(particle->texture, particle->pos, z_pos);
71 context.pop_transform();
75 ParticleSystem_Interactive::collision(Particle* object, Vector movement)
77 using namespace collision;
79 // calculate rectangle where the object will move
83 x2 = x1 + 32 + movement.x;
85 y2 = y1 + 32 + movement.y;
88 // test with all tiles in this rectangle
89 int starttilex = int(x1-1) / 32;
90 int starttiley = int(y1-1) / 32;
91 int max_x = int(x2+1);
92 int max_y = int(y2+1);
94 Rect dest = Rect(x1, y1, x2, y2);
96 Constraints constraints;
98 for(std::list<TileMap*>::const_iterator i = Sector::current()->solid_tilemaps.begin(); i != Sector::current()->solid_tilemaps.end(); i++) {
100 for(int x = starttilex; x*32 < max_x; ++x) {
101 for(int y = starttiley; y*32 < max_y; ++y) {
102 const Tile* tile = solids->get_tile(x, y);
105 // skip non-solid tiles, except water
106 if(! (tile->getAttributes() & (Tile::WATER | Tile::SOLID)))
109 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
111 Vector p1(x*32, y*32);
112 Vector p2((x+1)*32, (y+1)*32);
113 triangle = AATriangle(p1, p2, tile->getData());
115 if(rectangle_aatriangle(&constraints, dest, triangle)) {
116 if(tile->getAttributes() & Tile::WATER)
119 } else { // normal rectangular tile
120 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
121 if(intersects(dest, rect)) {
122 if(tile->getAttributes() & Tile::WATER)
124 set_rectangle_rectangle_constraints(&constraints, dest, rect);
132 // TODO don't use magic numbers here...
134 // did we collide at all?
135 if(!constraints.has_constraints())
138 const CollisionHit& hit = constraints.hit;
140 return 0; //collision with water tile - don't draw splash
142 if (hit.right || hit.left) {
143 return 2; //collision from right
145 return 1; //collision from above
152 RainParticleSystem::RainParticleSystem()
154 rainimages[0] = new Surface("images/objects/particles/rain0.png");
155 rainimages[1] = new Surface("images/objects/particles/rain1.png");
157 virtual_width = SCREEN_WIDTH * 2;
159 // create some random raindrops
160 size_t raindropcount = size_t(virtual_width/6.0);
161 for(size_t i=0; i<raindropcount; ++i) {
162 RainParticle* particle = new RainParticle;
163 particle->pos.x = systemRandom.rand(int(virtual_width));
164 particle->pos.y = systemRandom.rand(int(virtual_height));
165 int rainsize = systemRandom.rand(2);
166 particle->texture = rainimages[rainsize];
168 particle->speed = (rainsize+1)*45 + systemRandom.randf(3.6);
169 } while(particle->speed < 1);
170 particle->speed *= 10; // gravity
172 particles.push_back(particle);
177 RainParticleSystem::parse(const lisp::Lisp& reader)
179 reader.get("z-pos", z_pos);
183 RainParticleSystem::write(lisp::Writer& writer)
185 writer.start_list("particles-rain");
186 writer.write_int("z-pos", z_pos);
187 writer.end_list("particles-rain");
190 RainParticleSystem::~RainParticleSystem()
193 delete rainimages[i];
196 void RainParticleSystem::update(float elapsed_time)
198 std::vector<Particle*>::iterator i;
200 i = particles.begin(); i != particles.end(); ++i) {
201 RainParticle* particle = (RainParticle*) *i;
202 float movement = particle->speed * elapsed_time;
203 float abs_x = Sector::current()->camera->get_translation().x;
204 float abs_y = Sector::current()->camera->get_translation().y;
205 particle->pos.y += movement;
206 particle->pos.x -= movement;
207 int col = collision(particle, Vector(-movement, movement));
208 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
210 if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
211 bool vertical = (col == 2);
212 int splash_x, splash_y;
213 if (!vertical) { //check if collision happened from above
214 splash_x = int(particle->pos.x);
215 splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
216 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
218 // Uncomment the following to display vertical splashes, too
220 splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
221 splash_y = int(particle->pos.y);
222 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
225 int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
227 //FIXME: Don't move particles over solid tiles
228 particle->pos.x = new_x;
229 particle->pos.y = new_y;
234 CometParticleSystem::CometParticleSystem()
236 cometimages[0] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
237 cometimages[1] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
239 virtual_width = SCREEN_WIDTH * 2;
241 // create some random comets
242 size_t cometcount = 2;
243 for(size_t i=0; i<cometcount; ++i) {
244 CometParticle* particle = new CometParticle;
245 particle->pos.x = systemRandom.rand(int(virtual_width));
246 particle->pos.y = systemRandom.rand(int(virtual_height));
247 int cometsize = systemRandom.rand(2);
248 particle->texture = cometimages[cometsize];
250 particle->speed = (cometsize+1)*30 + systemRandom.randf(3.6);
251 } while(particle->speed < 1);
252 particle->speed *= 10; // gravity
254 particles.push_back(particle);
259 CometParticleSystem::parse(const lisp::Lisp& reader)
261 reader.get("z-pos", z_pos);
265 CometParticleSystem::write(lisp::Writer& writer)
267 writer.start_list("particles-comets");
268 writer.write_int("z-pos", z_pos);
269 writer.end_list("particles-comets");
272 CometParticleSystem::~CometParticleSystem()
275 delete cometimages[i];
278 void CometParticleSystem::update(float elapsed_time)
282 std::vector<Particle*>::iterator i;
284 i = particles.begin(); i != particles.end(); ++i) {
285 CometParticle* particle = (CometParticle*) *i;
286 float movement = particle->speed * elapsed_time;
287 float abs_x = Sector::current()->camera->get_translation().x;
288 float abs_y = Sector::current()->camera->get_translation().y;
289 particle->pos.y += movement;
290 particle->pos.x -= movement;
291 int col = collision(particle, Vector(-movement, movement));
292 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
293 if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)) {
294 Sector::current()->add_object(new Bomb(particle->pos, LEFT));
296 int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
298 //FIXME: Don't move particles over solid tiles
299 particle->pos.x = new_x;
300 particle->pos.y = new_y;