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