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.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"
40 ParticleSystem::ParticleSystem()
42 virtual_width = SCREEN_WIDTH;
43 virtual_height = SCREEN_HEIGHT;
44 layer = LAYER_BACKGROUND1;
47 ParticleSystem::~ParticleSystem()
49 std::vector<Particle*>::iterator i;
50 for(i = particles.begin(); i != particles.end(); ++i) {
55 void ParticleSystem::draw(DrawingContext& context)
57 float scrollx = context.get_translation().x;
58 float scrolly = context.get_translation().y;
60 context.push_transform();
61 context.set_translation(Vector(0,0));
63 std::vector<Particle*>::iterator i;
64 for(i = particles.begin(); i != particles.end(); ++i) {
65 Particle* particle = *i;
67 // remap x,y coordinates onto screencoordinates
69 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
70 if(pos.x < 0) pos.x += virtual_width;
71 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
72 if(pos.y < 0) pos.y += virtual_height;
74 if(pos.x > SCREEN_WIDTH) pos.x -= virtual_width;
75 if(pos.y > SCREEN_HEIGHT) pos.y -= virtual_height;
76 context.draw_surface(particle->texture, pos, layer);
79 context.pop_transform();
82 SnowParticleSystem::SnowParticleSystem()
84 snowimages[0] = new Surface(datadir+"/images/objects/particles/snow0.png", true);
85 snowimages[1] = new Surface(datadir+"/images/objects/particles/snow1.png", true);
86 snowimages[2] = new Surface(datadir+"/images/objects/particles/snow2.png", true);
88 virtual_width = SCREEN_WIDTH * 2;
90 // create some random snowflakes
91 size_t snowflakecount = size_t(virtual_width/10.0);
92 for(size_t i=0; i<snowflakecount; ++i) {
93 SnowParticle* particle = new SnowParticle;
94 particle->pos.x = rand() % int(virtual_width);
95 particle->pos.y = rand() % SCREEN_HEIGHT;
96 int snowsize = rand() % 3;
97 particle->texture = snowimages[snowsize];
99 particle->speed = snowsize*.2 + (float(rand()%10)*.4);
100 } while(particle->speed < 1);
101 particle->speed *= 10; // gravity
103 particles.push_back(particle);
108 SnowParticleSystem::parse(const lisp::Lisp& reader)
110 reader.get("layer", layer);
114 SnowParticleSystem::write(lisp::Writer& writer)
116 writer.start_list("particles-snow");
117 writer.write_int("layer", layer);
118 writer.end_list("particles-snow");
121 SnowParticleSystem::~SnowParticleSystem()
124 delete snowimages[i];
127 void SnowParticleSystem::update(float elapsed_time)
129 std::vector<Particle*>::iterator i;
130 for(i = particles.begin(); i != particles.end(); ++i) {
131 SnowParticle* particle = (SnowParticle*) *i;
132 particle->pos.y += particle->speed * elapsed_time;
133 if(particle->pos.y > SCREEN_HEIGHT) {
134 particle->pos.y = fmodf(particle->pos.y , virtual_height);
135 particle->pos.x = rand() % int(virtual_width);
140 RainParticleSystem::RainParticleSystem()
142 rainimages[0] = new Surface(datadir+"/images/objects/particles/rain0.png", true);
143 rainimages[1] = new Surface(datadir+"/images/objects/particles/rain1.png", true);
145 virtual_width = SCREEN_WIDTH * 2;
147 // create some random raindrops
148 size_t raindropcount = size_t(virtual_width/8.0);
149 for(size_t i=0; i<raindropcount; ++i) {
150 RainParticle* particle = new RainParticle;
151 particle->pos.x = rand() % int(virtual_width);
152 particle->pos.y = rand() % int(virtual_height);
153 int rainsize = rand() % 2;
154 particle->texture = rainimages[rainsize];
156 particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
157 } while(particle->speed < 1);
158 particle->speed *= 10; // gravity
160 particles.push_back(particle);
165 RainParticleSystem::parse(const lisp::Lisp& reader)
167 reader.get("layer", layer);
171 RainParticleSystem::write(lisp::Writer& writer)
173 writer.start_list("particles-rain");
174 writer.write_int("layer", layer);
175 writer.end_list("particles-rain");
178 RainParticleSystem::~RainParticleSystem()
181 delete rainimages[i];
184 void RainParticleSystem::update(float elapsed_time)
186 std::vector<Particle*>::iterator i;
188 i = particles.begin(); i != particles.end(); ++i) {
189 RainParticle* particle = (RainParticle*) *i;
190 float movement = particle->speed * elapsed_time;
191 particle->pos.y += movement;
192 particle->pos.x -= movement;
193 if ((particle->pos.y > SCREEN_HEIGHT) || (collision(particle, Vector(-movement, movement)))) {
194 particle->pos.y = Sector::current()->camera->get_translation().y;
195 particle->pos.x = rand() % int(virtual_width);
201 RainParticleSystem::collision(RainParticle* object, Vector movement)
203 TileMap* solids = Sector::current()->solids;
204 // calculate rectangle where the object will move
208 x2 = x1 + 32 + movement.x;
210 y2 = y1 + 32 + movement.y;
212 // test with all tiles in this rectangle
213 int starttilex = int(x1-1) / 32;
214 int starttiley = int(y1-1) / 32;
215 int max_x = int(x2+1);
216 int max_y = int(y2+1);
218 CollisionHit temphit, hit;
219 Rect dest = Rect(x1, y1, x2, y2);
221 hit.time = -1; // represents an invalid value
222 for(int x = starttilex; x*32 < max_x; ++x) {
223 for(int y = starttiley; y*32 < max_y; ++y) {
224 const Tile* tile = solids->get_tile(x, y);
227 // skip non-solid tiles
228 if(!(tile->getAttributes() & Tile::SOLID))
231 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
233 Vector p1(x*32, y*32);
234 Vector p2((x+1)*32, (y+1)*32);
235 triangle = AATriangle(p1, p2, tile->getData());
237 if(Collision::rectangle_aatriangle(temphit, dest, movement,
239 if(temphit.time > hit.time)
242 } else { // normal rectangular tile
243 Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
244 if(Collision::rectangle_rectangle(temphit, dest,
246 if(temphit.time > hit.time)
253 // did we collide at all?
255 return false; else return true;
258 CloudParticleSystem::CloudParticleSystem()
260 cloudimage = new Surface(datadir + "/images/objects/particles/cloud.png", true);
262 virtual_width = 2000.0;
264 // create some random clouds
265 for(size_t i=0; i<15; ++i) {
266 CloudParticle* particle = new CloudParticle;
267 particle->pos.x = rand() % int(virtual_width);
268 particle->pos.y = rand() % int(virtual_height);
269 particle->texture = cloudimage;
270 particle->speed = -float(25 + rand() % 30);
272 particles.push_back(particle);
277 CloudParticleSystem::parse(const lisp::Lisp& reader)
279 reader.get("layer", layer);
283 CloudParticleSystem::write(lisp::Writer& writer)
285 writer.start_list("particles-clouds");
286 writer.write_int("layer", layer);
287 writer.end_list("particles-clouds");
290 CloudParticleSystem::~CloudParticleSystem()
295 void CloudParticleSystem::update(float elapsed_time)
297 std::vector<Particle*>::iterator i;
298 for(i = particles.begin(); i != particles.end(); ++i) {
299 CloudParticle* particle = (CloudParticle*) *i;
300 particle->pos.x += particle->speed * elapsed_time;