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