added a powerup object that can be placed in levels and represent various powerups...
[supertux.git] / src / object / particlesystem_absolute.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_absolute.h"
25 #include "video/drawing_context.h"
26 #include "lisp/parser.h"
27 #include "lisp/lisp.h"
28 #include "lisp/writer.h"
29 #include "resources.h"
30 #include "main.h"
31
32 #include "tile.h"
33 #include "tilemap.h"
34 #include "math/aatriangle.h"
35 #include "collision.h"
36 #include "collision_hit.h"
37 #include "object/camera.h"
38
39 //TODO: Dynamically create splashes at collision spots
40 //      Find a way to make rain collide with objects like bonus blocks
41 //      Add an option to set rain strength
42 ParticleSystem_Absolute::ParticleSystem_Absolute()
43 {
44     virtual_width = SCREEN_WIDTH;
45     virtual_height = SCREEN_HEIGHT;
46     layer = LAYER_TILES;
47 }
48
49 ParticleSystem_Absolute::~ParticleSystem_Absolute()
50 {
51     std::vector<Particle*>::iterator i;
52     for(i = particles.begin(); i != particles.end(); ++i) {
53         delete *i;
54     }
55 }
56
57 void ParticleSystem_Absolute::draw(DrawingContext& context)
58 {
59   context.push_transform();
60   
61     std::vector<Particle*>::iterator i;
62     for(i = particles.begin(); i != particles.end(); ++i) {
63         Particle* particle = *i;
64         context.draw_surface(particle->texture, particle->pos, layer);
65     }
66
67     context.pop_transform();
68 }
69
70 RainParticleSystem::RainParticleSystem()
71 {
72     rainimages[0] = new Surface(datadir+"/images/objects/particles/rain0.png", true);
73     rainimages[1] = new Surface(datadir+"/images/objects/particles/rain1.png", true);
74
75     virtual_width = SCREEN_WIDTH * 2;
76
77     // create some random raindrops
78     size_t raindropcount = size_t(virtual_width/6.0);
79     for(size_t i=0; i<raindropcount; ++i) {
80         RainParticle* particle = new RainParticle;
81         particle->pos.x = rand() % int(virtual_width);
82         particle->pos.y = rand() % int(virtual_height);
83         int rainsize = rand() % 2;
84         particle->texture = rainimages[rainsize];
85         do {
86             particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
87         } while(particle->speed < 1);
88         particle->speed *= 10; // gravity
89
90         particles.push_back(particle);
91     }
92 }
93
94 void
95 RainParticleSystem::parse(const lisp::Lisp& reader)
96 {
97   reader.get("layer", layer);
98 }
99
100 void
101 RainParticleSystem::write(lisp::Writer& writer)
102 {
103   writer.start_list("particles-rain");
104   writer.write_int("layer", layer);
105   writer.end_list("particles-rain");
106 }
107
108 RainParticleSystem::~RainParticleSystem()
109 {
110   for(int i=0;i<2;++i)
111     delete rainimages[i];
112 }
113
114 void RainParticleSystem::update(float elapsed_time)
115 {
116     std::vector<Particle*>::iterator i;
117     for(
118         i = particles.begin(); i != particles.end(); ++i) {
119         RainParticle* particle = (RainParticle*) *i;
120         float movement = particle->speed * elapsed_time;
121         float abs_x = Sector::current()->camera->get_translation().x;
122         float abs_y = Sector::current()->camera->get_translation().y;
123         particle->pos.y += movement;
124         particle->pos.x -= movement;
125         if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (collision(particle, Vector(-movement, movement)))) {
126             int new_x = (rand() % int(virtual_width)) + int(abs_x);
127             int new_y = 0;
128             //FIXME: Don't move particles over solid tiles
129             particle->pos.x = new_x;
130             particle->pos.y = new_y;
131         }
132     }
133 }
134
135 bool
136 RainParticleSystem::collision(RainParticle* object, Vector movement)
137 {
138   TileMap* solids = Sector::current()->solids;
139   // calculate rectangle where the object will move
140   float x1, x2;
141   float y1, y2;
142   x1 = object->pos.x;
143   x2 = x1 + 32 + movement.x;
144   y1 = object->pos.y;
145   y2 = y1 + 32 + movement.y;
146   
147   // test with all tiles in this rectangle
148   int starttilex = int(x1-1) / 32;
149   int starttiley = int(y1-1) / 32;
150   int max_x = int(x2+1);
151   int max_y = int(y2+1);
152   
153   CollisionHit temphit, hit;
154   Rect dest = Rect(x1, y1, x2, y2);
155   dest.move(movement);
156   hit.time = -1; // represents an invalid value
157   for(int x = starttilex; x*32 < max_x; ++x) {
158     for(int y = starttiley; y*32 < max_y; ++y) {
159       const Tile* tile = solids->get_tile(x, y);
160       if(!tile)
161         continue;
162       // skip non-solid tiles
163       if(!(tile->getAttributes() & Tile::SOLID))
164         continue;
165
166       if(tile->getAttributes() & Tile::SLOPE) { // slope tile
167         AATriangle triangle;
168         Vector p1(x*32, y*32);
169         Vector p2((x+1)*32, (y+1)*32);
170         triangle = AATriangle(p1, p2, tile->getData());
171
172         if(Collision::rectangle_aatriangle(temphit, dest, movement,
173               triangle)) {
174           if(temphit.time > hit.time)
175             hit = temphit;
176         }
177       } else { // normal rectangular tile
178         Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
179         if(Collision::rectangle_rectangle(temphit, dest,
180               movement, rect)) {
181           if(temphit.time > hit.time)
182             hit = temphit;
183         }
184       }
185     }
186   }
187   
188   // did we collide at all?
189   if(hit.time < 0)
190     return false; else return true;
191 }