added new class for particle systems that need absolute coordinates (currently only...
[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: Try to equally distribute rain throughout the level
40 //      Dynamically create splashes at collision spots
41 //      Check why it can rain through boxes
42 //      Add an option to set rain strength
43 ParticleSystem_Absolute::ParticleSystem_Absolute()
44 {
45     virtual_width = SCREEN_WIDTH;
46     virtual_height = SCREEN_HEIGHT;
47     layer = LAYER_BACKGROUND1;
48 }
49
50 ParticleSystem_Absolute::~ParticleSystem_Absolute()
51 {
52     std::vector<Particle*>::iterator i;
53     for(i = particles.begin(); i != particles.end(); ++i) {
54         delete *i;
55     }
56 }
57
58 void ParticleSystem_Absolute::draw(DrawingContext& context)
59 {
60   //float scrollx = context.get_translation().x;
61   //float scrolly = context.get_translation().y;
62
63   context.push_transform();
64   //context.set_translation(Vector(0,0));
65   
66     std::vector<Particle*>::iterator i;
67     for(i = particles.begin(); i != particles.end(); ++i) {
68         Particle* particle = *i;
69
70         // remap x,y coordinates onto screencoordinates
71         /*Vector pos;
72         pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
73         if(pos.x < 0) pos.x += virtual_width;
74         pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
75         if(pos.y < 0) pos.y += virtual_height;
76
77         if(pos.x > SCREEN_WIDTH) pos.x -= virtual_width;
78         if(pos.y > SCREEN_HEIGHT) pos.y -= virtual_height;*/
79         context.draw_surface(particle->texture, particle->pos, layer);
80     }
81
82     context.pop_transform();
83 }
84
85 RainParticleSystem::RainParticleSystem()
86 {
87     rainimages[0] = new Surface(datadir+"/images/objects/particles/rain0.png", true);
88     rainimages[1] = new Surface(datadir+"/images/objects/particles/rain1.png", true);
89
90     virtual_width = SCREEN_WIDTH * 2;
91
92     // create some random raindrops
93     size_t raindropcount = size_t(virtual_width/4.0);
94     for(size_t i=0; i<raindropcount; ++i) {
95         RainParticle* particle = new RainParticle;
96         particle->pos.x = rand() % int(virtual_width);
97         particle->pos.y = rand() % int(virtual_height);
98         int rainsize = rand() % 2;
99         particle->texture = rainimages[rainsize];
100         do {
101             particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
102         } while(particle->speed < 1);
103         particle->speed *= 10; // gravity
104
105         particles.push_back(particle);
106     }
107 }
108
109 void
110 RainParticleSystem::parse(const lisp::Lisp& reader)
111 {
112   reader.get("layer", layer);
113 }
114
115 void
116 RainParticleSystem::write(lisp::Writer& writer)
117 {
118   writer.start_list("particles-rain");
119   writer.write_int("layer", layer);
120   writer.end_list("particles-rain");
121 }
122
123 RainParticleSystem::~RainParticleSystem()
124 {
125   for(int i=0;i<2;++i)
126     delete rainimages[i];
127 }
128
129 void RainParticleSystem::update(float elapsed_time)
130 {
131     std::vector<Particle*>::iterator i;
132     for(
133         i = particles.begin(); i != particles.end(); ++i) {
134         RainParticle* particle = (RainParticle*) *i;
135         float movement = particle->speed * elapsed_time;
136         float abs_x = Sector::current()->camera->get_translation().x;
137         float abs_y = Sector::current()->camera->get_translation().y;
138         particle->pos.y += movement;
139         particle->pos.x -= movement;
140         if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (collision(particle, Vector(-movement, movement)))) {
141             particle->pos.y = 0;
142             particle->pos.x = rand() % int(abs_x + virtual_width);
143         }
144     }
145 }
146
147 bool
148 RainParticleSystem::collision(RainParticle* object, Vector movement)
149 {
150   TileMap* solids = Sector::current()->solids;
151   // calculate rectangle where the object will move
152   float x1, x2;
153   float y1, y2;
154   x1 = object->pos.x;
155   x2 = x1 + 32 + movement.x;
156   y1 = object->pos.y;
157   y2 = y1 + 32 + movement.y;
158   
159   // test with all tiles in this rectangle
160   int starttilex = int(x1-1) / 32;
161   int starttiley = int(y1-1) / 32;
162   int max_x = int(x2+1);
163   int max_y = int(y2+1);
164   
165   CollisionHit temphit, hit;
166   Rect dest = Rect(x1, y1, x2, y2);
167   dest.move(movement);
168   hit.time = -1; // represents an invalid value
169   for(int x = starttilex; x*32 < max_x; ++x) {
170     for(int y = starttiley; y*32 < max_y; ++y) {
171       const Tile* tile = solids->get_tile(x, y);
172       //std::cout << "Tile at: " << x << "," << y << std::endl;
173       if(!tile)
174         continue;
175       // skip non-solid tiles
176       if(!(tile->getAttributes() & Tile::SOLID))
177         continue;
178
179       if(tile->getAttributes() & Tile::SLOPE) { // slope tile
180         AATriangle triangle;
181         Vector p1(x*32, y*32);
182         Vector p2((x+1)*32, (y+1)*32);
183         triangle = AATriangle(p1, p2, tile->getData());
184
185         if(Collision::rectangle_aatriangle(temphit, dest, movement,
186               triangle)) {
187           if(temphit.time > hit.time)
188             hit = temphit;
189         }
190       } else { // normal rectangular tile
191         Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
192         if(Collision::rectangle_rectangle(temphit, dest,
193               movement, rect)) {
194           if(temphit.time > hit.time)
195             hit = temphit;
196         }
197       }
198     }
199   }
200   
201   // did we collide at all?
202   if(hit.time < 0)
203     return false; else return true;
204 }