b188d9bcd35b2d26ceed997145ca62f3baef6f5e
[supertux.git] / src / object / particlesystem_interactive.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include <iostream>
23 #include <cmath>
24
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"
31 #include "main.hpp"
32
33 #include "tile.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"
42
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()
47 {
48     virtual_width = SCREEN_WIDTH;
49     virtual_height = SCREEN_HEIGHT;
50     z_pos = 0;
51 }
52
53 ParticleSystem_Interactive::~ParticleSystem_Interactive()
54 {
55     std::vector<Particle*>::iterator i;
56     for(i = particles.begin(); i != particles.end(); ++i) {
57         delete *i;
58     }
59 }
60
61 void ParticleSystem_Interactive::draw(DrawingContext& context)
62 {
63   context.push_transform();
64
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);
69     }
70
71     context.pop_transform();
72 }
73
74 int
75 ParticleSystem_Interactive::collision(Particle* object, Vector movement)
76 {
77   using namespace collision;
78
79   // calculate rectangle where the object will move
80   float x1, x2;
81   float y1, y2;
82   x1 = object->pos.x;
83   x2 = x1 + 32 + movement.x;
84   y1 = object->pos.y;
85   y2 = y1 + 32 + movement.y;
86   bool water = false;
87
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);
93
94   Rect dest = Rect(x1, y1, x2, y2);
95   dest.move(movement);
96   Constraints constraints;
97
98   for(std::list<TileMap*>::const_iterator i = Sector::current()->solid_tilemaps.begin(); i != Sector::current()->solid_tilemaps.end(); i++) {
99     TileMap* solids = *i;
100
101     for(int x = starttilex; x*32 < max_x; ++x) {
102       for(int y = starttiley; y*32 < max_y; ++y) {
103         const Tile* tile = solids->get_tile(x, y);
104         if(!tile)
105           continue;
106         // skip non-solid tiles, except water
107         if(! (tile->getAttributes() & (Tile::WATER | Tile::SOLID)))
108           continue;
109
110         if(tile->getAttributes() & Tile::SLOPE) { // slope tile
111           AATriangle triangle;
112           Vector p1(x*32, y*32);
113           Vector p2((x+1)*32, (y+1)*32);
114           triangle = AATriangle(p1, p2, tile->getData());
115
116           if(rectangle_aatriangle(&constraints, dest, triangle)) {
117             if(tile->getAttributes() & Tile::WATER)
118               water = true;
119           }
120         } else { // normal rectangular tile
121           Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
122           if(intersects(dest, rect)) {
123             if(tile->getAttributes() & Tile::WATER)
124               water = true;
125             set_rectangle_rectangle_constraints(&constraints, dest, rect);
126           }
127         }
128       }
129     }
130   }
131
132
133   // TODO don't use magic numbers here...
134
135   // did we collide at all?
136   if(!constraints.has_constraints())
137     return -1;
138
139   const CollisionHit& hit = constraints.hit;
140   if (water) {
141     return 0; //collision with water tile - don't draw splash
142   } else {
143     if (hit.right || hit.left) {
144       return 2; //collision from right
145     } else {
146       return 1; //collision from above
147     }
148   }
149
150   return 0;
151 }
152
153 RainParticleSystem::RainParticleSystem()
154 {
155     rainimages[0] = new Surface("images/objects/particles/rain0.png");
156     rainimages[1] = new Surface("images/objects/particles/rain1.png");
157
158     virtual_width = SCREEN_WIDTH * 2;
159
160     // create some random raindrops
161     size_t raindropcount = size_t(virtual_width/6.0);
162     for(size_t i=0; i<raindropcount; ++i) {
163         RainParticle* particle = new RainParticle;
164         particle->pos.x = systemRandom.rand(int(virtual_width));
165         particle->pos.y = systemRandom.rand(int(virtual_height));
166         int rainsize = systemRandom.rand(2);
167         particle->texture = rainimages[rainsize];
168         do {
169             particle->speed = (rainsize+1)*45 + systemRandom.randf(3.6);
170         } while(particle->speed < 1);
171         particle->speed *= 10; // gravity
172
173         particles.push_back(particle);
174     }
175 }
176
177 void
178 RainParticleSystem::parse(const lisp::Lisp& reader)
179 {
180   reader.get("z-pos", z_pos);
181 }
182
183 void
184 RainParticleSystem::write(lisp::Writer& writer)
185 {
186   writer.start_list("particles-rain");
187   writer.write_int("z-pos", z_pos);
188   writer.end_list("particles-rain");
189 }
190
191 RainParticleSystem::~RainParticleSystem()
192 {
193   for(int i=0;i<2;++i)
194     delete rainimages[i];
195 }
196
197 void RainParticleSystem::update(float elapsed_time)
198 {
199     std::vector<Particle*>::iterator i;
200     for(
201         i = particles.begin(); i != particles.end(); ++i) {
202         RainParticle* particle = (RainParticle*) *i;
203         float movement = particle->speed * elapsed_time;
204         float abs_x = Sector::current()->camera->get_translation().x;
205         float abs_y = Sector::current()->camera->get_translation().y;
206         particle->pos.y += movement;
207         particle->pos.x -= movement;
208         int col = collision(particle, Vector(-movement, movement));
209         if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
210             //Create rainsplash
211             if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
212               bool vertical = (col == 2);
213               int splash_x, splash_y;
214               if (!vertical) { //check if collision happened from above
215                 splash_x = int(particle->pos.x);
216                 splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
217                 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
218               }
219               // Uncomment the following to display vertical splashes, too
220               /* else {
221                 splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
222                 splash_y = int(particle->pos.y);
223                 Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
224               } */
225             }
226             int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
227             int new_y = 0;
228             //FIXME: Don't move particles over solid tiles
229             particle->pos.x = new_x;
230             particle->pos.y = new_y;
231         }
232     }
233 }
234
235 CometParticleSystem::CometParticleSystem()
236 {
237     cometimages[0] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
238     cometimages[1] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
239
240     virtual_width = SCREEN_WIDTH * 2;
241
242     // create some random comets
243     size_t cometcount = 2;
244     for(size_t i=0; i<cometcount; ++i) {
245         CometParticle* particle = new CometParticle;
246         particle->pos.x = systemRandom.rand(int(virtual_width));
247         particle->pos.y = systemRandom.rand(int(virtual_height));
248         int cometsize = systemRandom.rand(2);
249         particle->texture = cometimages[cometsize];
250         do {
251             particle->speed = (cometsize+1)*30 + systemRandom.randf(3.6);
252         } while(particle->speed < 1);
253         particle->speed *= 10; // gravity
254
255         particles.push_back(particle);
256     }
257 }
258
259 void
260 CometParticleSystem::parse(const lisp::Lisp& reader)
261 {
262   reader.get("z-pos", z_pos);
263 }
264
265 void
266 CometParticleSystem::write(lisp::Writer& writer)
267 {
268   writer.start_list("particles-comets");
269   writer.write_int("z-pos", z_pos);
270   writer.end_list("particles-comets");
271 }
272
273 CometParticleSystem::~CometParticleSystem()
274 {
275   for(int i=0;i<2;++i)
276     delete cometimages[i];
277 }
278
279 void CometParticleSystem::update(float elapsed_time)
280 {
281   (void) elapsed_time;
282 #if 0
283     std::vector<Particle*>::iterator i;
284     for(
285         i = particles.begin(); i != particles.end(); ++i) {
286         CometParticle* particle = (CometParticle*) *i;
287         float movement = particle->speed * elapsed_time;
288         float abs_x = Sector::current()->camera->get_translation().x;
289         float abs_y = Sector::current()->camera->get_translation().y;
290         particle->pos.y += movement;
291         particle->pos.x -= movement;
292         int col = collision(particle, Vector(-movement, movement));
293         if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
294             if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)) {
295               Sector::current()->add_object(new Bomb(particle->pos, LEFT));
296             }
297             int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
298             int new_y = 0;
299             //FIXME: Don't move particles over solid tiles
300             particle->pos.x = new_x;
301             particle->pos.y = new_y;
302         }
303     }
304 #endif
305 }