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