5347f13559da64685bb191a7c5abb38ad19a0c03
[supertux.git] / src / object / particlesystem_interactive.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/particlesystem_interactive.hpp"
18 #include "supertux/main.hpp"
19
20 #include "math/aatriangle.hpp"
21 #include "math/random_generator.hpp"
22 #include "object/camera.hpp"
23 #include "object/rainsplash.hpp"
24 #include "object/tilemap.hpp"
25 #include "supertux/collision.hpp"
26 #include "supertux/tile.hpp"
27
28 //TODO: Find a way to make rain collide with objects like bonus blocks
29 //      Add an option to set rain strength
30 //      Fix rain being "respawned" over solid tiles
31 ParticleSystem_Interactive::ParticleSystem_Interactive() :
32   z_pos(),
33   particles(),
34   virtual_width(),
35   virtual_height()
36 {
37   virtual_width = SCREEN_WIDTH;
38   virtual_height = SCREEN_HEIGHT;
39   z_pos = 0;
40 }
41
42 ParticleSystem_Interactive::~ParticleSystem_Interactive()
43 {
44   std::vector<Particle*>::iterator i;
45   for(i = particles.begin(); i != particles.end(); ++i) {
46     delete *i;
47   }
48 }
49
50 void ParticleSystem_Interactive::draw(DrawingContext& context)
51 {
52   context.push_transform();
53
54   std::vector<Particle*>::iterator i;
55   for(i = particles.begin(); i != particles.end(); ++i) {
56     Particle* particle = *i;
57     context.draw_surface(particle->texture, particle->pos, z_pos);
58   }
59
60   context.pop_transform();
61 }
62
63 int
64 ParticleSystem_Interactive::collision(Particle* object, Vector movement)
65 {
66   using namespace collision;
67
68   // calculate rectangle where the object will move
69   float x1, x2;
70   float y1, y2;
71   x1 = object->pos.x;
72   x2 = x1 + 32 + movement.x;
73   y1 = object->pos.y;
74   y2 = y1 + 32 + movement.y;
75   bool water = false;
76
77   // test with all tiles in this rectangle
78   int starttilex = int(x1-1) / 32;
79   int starttiley = int(y1-1) / 32;
80   int max_x = int(x2+1);
81   int max_y = int(y2+1);
82
83   Rect dest = Rect(x1, y1, x2, y2);
84   dest.move(movement);
85   Constraints constraints;
86
87   for(std::list<TileMap*>::const_iterator i = Sector::current()->solid_tilemaps.begin(); i != Sector::current()->solid_tilemaps.end(); i++) {
88     TileMap* solids = *i;
89     for(int x = starttilex; x*32 < max_x; ++x) {
90       for(int y = starttiley; y*32 < max_y; ++y) {
91         const Tile* tile = solids->get_tile(x, y);
92         if(!tile)
93           continue;
94         // skip non-solid tiles, except water
95         if(! (tile->getAttributes() & (Tile::WATER | Tile::SOLID)))
96           continue;
97
98         if(tile->getAttributes() & Tile::SLOPE) { // slope tile
99           AATriangle triangle;
100           Vector p1(x*32, y*32);
101           Vector p2((x+1)*32, (y+1)*32);
102           triangle = AATriangle(p1, p2, tile->getData());
103
104           if(rectangle_aatriangle(&constraints, dest, triangle)) {
105             if(tile->getAttributes() & Tile::WATER)
106               water = true;
107           }
108         } else { // normal rectangular tile
109           Rect rect(x*32, y*32, (x+1)*32, (y+1)*32);
110           if(intersects(dest, rect)) {
111             if(tile->getAttributes() & Tile::WATER)
112               water = true;
113             set_rectangle_rectangle_constraints(&constraints, dest, rect);
114           }
115         }
116       }
117     }
118   }
119
120   // TODO don't use magic numbers here...
121
122   // did we collide at all?
123   if(!constraints.has_constraints())
124     return -1;
125
126   const CollisionHit& hit = constraints.hit;
127   if (water) {
128     return 0; //collision with water tile - don't draw splash
129   } else {
130     if (hit.right || hit.left) {
131       return 2; //collision from right
132     } else {
133       return 1; //collision from above
134     }
135   }
136
137   return 0;
138 }
139
140 RainParticleSystem::RainParticleSystem()
141 {
142   rainimages[0] = new Surface("images/objects/particles/rain0.png");
143   rainimages[1] = new Surface("images/objects/particles/rain1.png");
144
145   virtual_width = SCREEN_WIDTH * 2;
146
147   // create some random raindrops
148   size_t raindropcount = size_t(virtual_width/6.0);
149   for(size_t i=0; i<raindropcount; ++i) {
150     RainParticle* particle = new RainParticle;
151     particle->pos.x = systemRandom.rand(int(virtual_width));
152     particle->pos.y = systemRandom.rand(int(virtual_height));
153     int rainsize = systemRandom.rand(2);
154     particle->texture = rainimages[rainsize];
155     do {
156       particle->speed = (rainsize+1)*45 + systemRandom.randf(3.6);
157     } while(particle->speed < 1);
158     particle->speed *= 10; // gravity
159
160     particles.push_back(particle);
161   }
162 }
163
164 void
165 RainParticleSystem::parse(const Reader& reader)
166 {
167   reader.get("z-pos", z_pos);
168 }
169
170 RainParticleSystem::~RainParticleSystem()
171 {
172   for(int i=0;i<2;++i)
173     delete rainimages[i];
174 }
175
176 void RainParticleSystem::update(float elapsed_time)
177 {
178   std::vector<Particle*>::iterator i;
179   for(
180     i = particles.begin(); i != particles.end(); ++i) {
181     RainParticle* particle = (RainParticle*) *i;
182     float movement = particle->speed * elapsed_time;
183     float abs_x = Sector::current()->camera->get_translation().x;
184     float abs_y = Sector::current()->camera->get_translation().y;
185     particle->pos.y += movement;
186     particle->pos.x -= movement;
187     int col = collision(particle, Vector(-movement, movement));
188     if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
189       //Create rainsplash
190       if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)){
191         bool vertical = (col == 2);
192         int splash_x, splash_y;
193         if (!vertical) { //check if collision happened from above
194           splash_x = int(particle->pos.x);
195           splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
196           Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
197         }
198         // Uncomment the following to display vertical splashes, too
199         /* else {
200            splash_x = int(particle->pos.x) - (int(particle->pos.x) % 32) + 32;
201            splash_y = int(particle->pos.y);
202            Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
203            } */
204       }
205       int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
206       int new_y = 0;
207       //FIXME: Don't move particles over solid tiles
208       particle->pos.x = new_x;
209       particle->pos.y = new_y;
210     }
211   }
212 }
213
214 CometParticleSystem::CometParticleSystem()
215 {
216   cometimages[0] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
217   cometimages[1] = new Surface("images/creatures/mr_bomb/exploding-left-0.png");
218
219   virtual_width = SCREEN_WIDTH * 2;
220
221   // create some random comets
222   size_t cometcount = 2;
223   for(size_t i=0; i<cometcount; ++i) {
224     CometParticle* particle = new CometParticle;
225     particle->pos.x = systemRandom.rand(int(virtual_width));
226     particle->pos.y = systemRandom.rand(int(virtual_height));
227     int cometsize = systemRandom.rand(2);
228     particle->texture = cometimages[cometsize];
229     do {
230       particle->speed = (cometsize+1)*30 + systemRandom.randf(3.6);
231     } while(particle->speed < 1);
232     particle->speed *= 10; // gravity
233
234     particles.push_back(particle);
235   }
236 }
237
238 void
239 CometParticleSystem::parse(const Reader& reader)
240 {
241   reader.get("z-pos", z_pos);
242 }
243
244 CometParticleSystem::~CometParticleSystem()
245 {
246   for(int i=0;i<2;++i)
247     delete cometimages[i];
248 }
249
250 void CometParticleSystem::update(float elapsed_time)
251 {
252   (void) elapsed_time;
253 #if 0
254   std::vector<Particle*>::iterator i;
255   for(
256     i = particles.begin(); i != particles.end(); ++i) {
257     CometParticle* particle = (CometParticle*) *i;
258     float movement = particle->speed * elapsed_time;
259     float abs_x = Sector::current()->camera->get_translation().x;
260     float abs_y = Sector::current()->camera->get_translation().y;
261     particle->pos.y += movement;
262     particle->pos.x -= movement;
263     int col = collision(particle, Vector(-movement, movement));
264     if ((particle->pos.y > SCREEN_HEIGHT + abs_y) || (col >= 0)) {
265       if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)) {
266         Sector::current()->add_object(new Bomb(particle->pos, LEFT));
267       }
268       int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
269       int new_y = 0;
270       //FIXME: Don't move particles over solid tiles
271       particle->pos.x = new_x;
272       particle->pos.y = new_y;
273     }
274   }
275 #endif
276 }
277
278 /* EOF */