Updated addon repository URL and improved debug output on download
[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
19 #include "math/aatriangle.hpp"
20 #include "object/tilemap.hpp"
21 #include "supertux/collision.hpp"
22 #include "supertux/tile.hpp"
23
24 //TODO: Find a way to make rain collide with objects like bonus blocks
25 //      Add an option to set rain strength
26 //      Fix rain being "respawned" over solid tiles
27 ParticleSystem_Interactive::ParticleSystem_Interactive() :
28   z_pos(),
29   particles(),
30   virtual_width(),
31   virtual_height()
32 {
33   virtual_width = SCREEN_WIDTH;
34   virtual_height = SCREEN_HEIGHT;
35   z_pos = 0;
36 }
37
38 ParticleSystem_Interactive::~ParticleSystem_Interactive()
39 {
40   std::vector<Particle*>::iterator i;
41   for(i = particles.begin(); i != particles.end(); ++i) {
42     delete *i;
43   }
44 }
45
46 void ParticleSystem_Interactive::draw(DrawingContext& context)
47 {
48   context.push_transform();
49
50   std::vector<Particle*>::iterator i;
51   for(i = particles.begin(); i != particles.end(); ++i) {
52     Particle* particle = *i;
53     context.draw_surface(particle->texture, particle->pos, z_pos);
54   }
55
56   context.pop_transform();
57 }
58
59 int
60 ParticleSystem_Interactive::collision(Particle* object, Vector movement)
61 {
62   using namespace collision;
63
64   // calculate rectangle where the object will move
65   float x1, x2;
66   float y1, y2;
67   x1 = object->pos.x;
68   x2 = x1 + 32 + movement.x;
69   y1 = object->pos.y;
70   y2 = y1 + 32 + movement.y;
71   bool water = false;
72
73   // test with all tiles in this rectangle
74   int starttilex = int(x1-1) / 32;
75   int starttiley = int(y1-1) / 32;
76   int max_x = int(x2+1);
77   int max_y = int(y2+1);
78
79   Rectf dest(x1, y1, x2, y2);
80   dest.move(movement);
81   Constraints constraints;
82
83   for(std::list<TileMap*>::const_iterator i = Sector::current()->solid_tilemaps.begin(); i != Sector::current()->solid_tilemaps.end(); ++i) {
84     TileMap* solids = *i;
85     // FIXME Handle a nonzero tilemap offset
86     for(int x = starttilex; x*32 < max_x; ++x) {
87       for(int y = starttiley; y*32 < max_y; ++y) {
88         const Tile* tile = solids->get_tile(x, y);
89         if(!tile)
90           continue;
91         // skip non-solid tiles, except water
92         if(! (tile->getAttributes() & (Tile::WATER | Tile::SOLID)))
93           continue;
94
95         Rectf rect = solids->get_tile_bbox(x, y);
96         if(tile->is_slope ()) { // slope tile
97           AATriangle triangle = AATriangle(rect, tile->getData());
98
99           if(rectangle_aatriangle(&constraints, dest, triangle)) {
100             if(tile->getAttributes() & Tile::WATER)
101               water = true;
102           }
103         } else { // normal rectangular tile
104           if(intersects(dest, rect)) {
105             if(tile->getAttributes() & Tile::WATER)
106               water = true;
107             set_rectangle_rectangle_constraints(&constraints, dest, rect);
108           }
109         }
110       }
111     }
112   }
113
114   // TODO don't use magic numbers here...
115
116   // did we collide at all?
117   if(!constraints.has_constraints())
118     return -1;
119
120   const CollisionHit& hit = constraints.hit;
121   if (water) {
122     return 0; //collision with water tile - don't draw splash
123   } else {
124     if (hit.right || hit.left) {
125       return 2; //collision from right
126     } else {
127       return 1; //collision from above
128     }
129   }
130
131   return 0;
132 }
133
134 /* EOF */