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