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_absolute.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"
39 //TODO: Dynamically create splashes at collision spots
40 // Find a way to make rain collide with objects like bonus blocks
41 // Add an option to set rain strength
42 ParticleSystem_Absolute::ParticleSystem_Absolute()
44 virtual_width = SCREEN_WIDTH;
45 virtual_height = SCREEN_HEIGHT;
49 ParticleSystem_Absolute::~ParticleSystem_Absolute()
51 std::vector<Particle*>::iterator i;
52 for(i = particles.begin(); i != particles.end(); ++i) {
57 void ParticleSystem_Absolute::draw(DrawingContext& context)
59 context.push_transform();
61 std::vector<Particle*>::iterator i;
62 for(i = particles.begin(); i != particles.end(); ++i) {
63 Particle* particle = *i;
64 context.draw_surface(particle->texture, particle->pos, layer);
67 context.pop_transform();
70 RainParticleSystem::RainParticleSystem()
72 rainimages[0] = new Surface(datadir+"/images/objects/particles/rain0.png", true);
73 rainimages[1] = new Surface(datadir+"/images/objects/particles/rain1.png", true);
75 virtual_width = SCREEN_WIDTH * 2;
77 // create some random raindrops
78 size_t raindropcount = size_t(virtual_width/6.0);
79 for(size_t i=0; i<raindropcount; ++i) {
80 RainParticle* particle = new RainParticle;
81 particle->pos.x = rand() % int(virtual_width);
82 particle->pos.y = rand() % int(virtual_height);
83 int rainsize = rand() % 2;
84 particle->texture = rainimages[rainsize];
86 particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
87 } while(particle->speed < 1);
88 particle->speed *= 10; // gravity
90 particles.push_back(particle);
95 RainParticleSystem::parse(const lisp::Lisp& reader)
97 reader.get("layer", layer);
101 RainParticleSystem::write(lisp::Writer& writer)
103 writer.start_list("particles-rain");
104 writer.write_int("layer", layer);
105 writer.end_list("particles-rain");
108 RainParticleSystem::~RainParticleSystem()
111 delete rainimages[i];
114 void RainParticleSystem::update(float elapsed_time)
116 std::vector<Particle*>::iterator i;
118 i = particles.begin(); i != particles.end(); ++i) {
119 RainParticle* particle = (RainParticle*) *i;
120 float movement = particle->speed * elapsed_time;
121 float abs_x = Sector::current()->camera->get_translation().x;
122 float abs_y = Sector::current()->camera->get_translation().y;
123 particle->pos.y += movement;
124 particle->pos.x -= movement;
125 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (collision(particle, Vector(-movement, movement)))) {
126 int new_x = (rand() % int(virtual_width)) + int(abs_x);
128 //FIXME: Don't move particles over solid tiles
129 particle->pos.x = new_x;
130 particle->pos.y = new_y;
136 RainParticleSystem::collision(RainParticle* object, Vector movement)
138 TileMap* solids = Sector::current()->solids;
139 // calculate rectangle where the object will move
143 x2 = x1 + 32 + movement.x;
145 y2 = y1 + 32 + movement.y;
147 // test with all tiles in this rectangle
148 int starttilex = int(x1-1) / 32;
149 int starttiley = int(y1-1) / 32;
150 int max_x = int(x2+1);
151 int max_y = int(y2+1);
153 CollisionHit temphit, hit;
154 Rect dest = Rect(x1, y1, x2, y2);
156 hit.time = -1; // represents an invalid value
157 for(int x = starttilex; x*32 < max_x; ++x) {
158 for(int y = starttiley; y*32 < max_y; ++y) {
159 const Tile* tile = solids->get_tile(x, y);
162 // skip non-solid tiles
163 if(!(tile->getAttributes() & Tile::SOLID))
166 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
168 Vector p1(x*32, y*32);
169 Vector p2((x+1)*32, (y+1)*32);
170 triangle = AATriangle(p1, p2, tile->getData());
172 if(Collision::rectangle_aatriangle(temphit, dest, movement,
174 if(temphit.time > hit.time)
177 } else { // normal rectangular tile
178 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
179 if(Collision::rectangle_rectangle(temphit, dest,
181 if(temphit.time > hit.time)
188 // did we collide at all?
190 return false; else return true;