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: Try to equally distribute rain throughout the level
40 // Dynamically create splashes at collision spots
41 // Check why it can rain through boxes
42 // Add an option to set rain strength
43 ParticleSystem_Absolute::ParticleSystem_Absolute()
45 virtual_width = SCREEN_WIDTH;
46 virtual_height = SCREEN_HEIGHT;
47 layer = LAYER_BACKGROUND1;
50 ParticleSystem_Absolute::~ParticleSystem_Absolute()
52 std::vector<Particle*>::iterator i;
53 for(i = particles.begin(); i != particles.end(); ++i) {
58 void ParticleSystem_Absolute::draw(DrawingContext& context)
60 //float scrollx = context.get_translation().x;
61 //float scrolly = context.get_translation().y;
63 context.push_transform();
64 //context.set_translation(Vector(0,0));
66 std::vector<Particle*>::iterator i;
67 for(i = particles.begin(); i != particles.end(); ++i) {
68 Particle* particle = *i;
70 // remap x,y coordinates onto screencoordinates
72 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
73 if(pos.x < 0) pos.x += virtual_width;
74 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
75 if(pos.y < 0) pos.y += virtual_height;
77 if(pos.x > SCREEN_WIDTH) pos.x -= virtual_width;
78 if(pos.y > SCREEN_HEIGHT) pos.y -= virtual_height;*/
79 context.draw_surface(particle->texture, particle->pos, layer);
82 context.pop_transform();
85 RainParticleSystem::RainParticleSystem()
87 rainimages[0] = new Surface(datadir+"/images/objects/particles/rain0.png", true);
88 rainimages[1] = new Surface(datadir+"/images/objects/particles/rain1.png", true);
90 virtual_width = SCREEN_WIDTH * 2;
92 // create some random raindrops
93 size_t raindropcount = size_t(virtual_width/4.0);
94 for(size_t i=0; i<raindropcount; ++i) {
95 RainParticle* particle = new RainParticle;
96 particle->pos.x = rand() % int(virtual_width);
97 particle->pos.y = rand() % int(virtual_height);
98 int rainsize = rand() % 2;
99 particle->texture = rainimages[rainsize];
101 particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
102 } while(particle->speed < 1);
103 particle->speed *= 10; // gravity
105 particles.push_back(particle);
110 RainParticleSystem::parse(const lisp::Lisp& reader)
112 reader.get("layer", layer);
116 RainParticleSystem::write(lisp::Writer& writer)
118 writer.start_list("particles-rain");
119 writer.write_int("layer", layer);
120 writer.end_list("particles-rain");
123 RainParticleSystem::~RainParticleSystem()
126 delete rainimages[i];
129 void RainParticleSystem::update(float elapsed_time)
131 std::vector<Particle*>::iterator i;
133 i = particles.begin(); i != particles.end(); ++i) {
134 RainParticle* particle = (RainParticle*) *i;
135 float movement = particle->speed * elapsed_time;
136 float abs_x = Sector::current()->camera->get_translation().x;
137 float abs_y = Sector::current()->camera->get_translation().y;
138 particle->pos.y += movement;
139 particle->pos.x -= movement;
140 if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (collision(particle, Vector(-movement, movement)))) {
142 particle->pos.x = rand() % int(abs_x + virtual_width);
148 RainParticleSystem::collision(RainParticle* object, Vector movement)
150 TileMap* solids = Sector::current()->solids;
151 // calculate rectangle where the object will move
155 x2 = x1 + 32 + movement.x;
157 y2 = y1 + 32 + movement.y;
159 // test with all tiles in this rectangle
160 int starttilex = int(x1-1) / 32;
161 int starttiley = int(y1-1) / 32;
162 int max_x = int(x2+1);
163 int max_y = int(y2+1);
165 CollisionHit temphit, hit;
166 Rect dest = Rect(x1, y1, x2, y2);
168 hit.time = -1; // represents an invalid value
169 for(int x = starttilex; x*32 < max_x; ++x) {
170 for(int y = starttiley; y*32 < max_y; ++y) {
171 const Tile* tile = solids->get_tile(x, y);
172 //std::cout << "Tile at: " << x << "," << y << std::endl;
175 // skip non-solid tiles
176 if(!(tile->getAttributes() & Tile::SOLID))
179 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
181 Vector p1(x*32, y*32);
182 Vector p2((x+1)*32, (y+1)*32);
183 triangle = AATriangle(p1, p2, tile->getData());
185 if(Collision::rectangle_aatriangle(temphit, dest, movement,
187 if(temphit.time > hit.time)
190 } else { // normal rectangular tile
191 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
192 if(Collision::rectangle_rectangle(temphit, dest,
194 if(temphit.time > hit.time)
201 // did we collide at all?
203 return false; else return true;