2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "object/particlesystem_interactive.hpp"
18 #include "supertux/main.hpp"
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"
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() :
37 virtual_width = SCREEN_WIDTH;
38 virtual_height = SCREEN_HEIGHT;
42 ParticleSystem_Interactive::~ParticleSystem_Interactive()
44 std::vector<Particle*>::iterator i;
45 for(i = particles.begin(); i != particles.end(); ++i) {
50 void ParticleSystem_Interactive::draw(DrawingContext& context)
52 context.push_transform();
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);
60 context.pop_transform();
64 ParticleSystem_Interactive::collision(Particle* object, Vector movement)
66 using namespace collision;
68 // calculate rectangle where the object will move
72 x2 = x1 + 32 + movement.x;
74 y2 = y1 + 32 + movement.y;
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);
83 Rect dest = Rect(x1, y1, x2, y2);
85 Constraints constraints;
87 for(std::list<TileMap*>::const_iterator i = Sector::current()->solid_tilemaps.begin(); i != Sector::current()->solid_tilemaps.end(); 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);
94 // skip non-solid tiles, except water
95 if(! (tile->getAttributes() & (Tile::WATER | Tile::SOLID)))
98 if(tile->getAttributes() & Tile::SLOPE) { // slope tile
100 Vector p1(x*32, y*32);
101 Vector p2((x+1)*32, (y+1)*32);
102 triangle = AATriangle(p1, p2, tile->getData());
104 if(rectangle_aatriangle(&constraints, dest, triangle)) {
105 if(tile->getAttributes() & Tile::WATER)
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)
113 set_rectangle_rectangle_constraints(&constraints, dest, rect);
120 // TODO don't use magic numbers here...
122 // did we collide at all?
123 if(!constraints.has_constraints())
126 const CollisionHit& hit = constraints.hit;
128 return 0; //collision with water tile - don't draw splash
130 if (hit.right || hit.left) {
131 return 2; //collision from right
133 return 1; //collision from above
140 RainParticleSystem::RainParticleSystem()
142 rainimages[0] = new Surface("images/objects/particles/rain0.png");
143 rainimages[1] = new Surface("images/objects/particles/rain1.png");
145 virtual_width = SCREEN_WIDTH * 2;
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];
156 particle->speed = (rainsize+1)*45 + systemRandom.randf(3.6);
157 } while(particle->speed < 1);
158 particle->speed *= 10; // gravity
160 particles.push_back(particle);
165 RainParticleSystem::parse(const Reader& reader)
167 reader.get("z-pos", z_pos);
170 RainParticleSystem::~RainParticleSystem()
173 delete rainimages[i];
176 void RainParticleSystem::update(float elapsed_time)
178 std::vector<Particle*>::iterator i;
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)) {
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));
198 // Uncomment the following to display vertical splashes, too
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));
205 int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
207 //FIXME: Don't move particles over solid tiles
208 particle->pos.x = new_x;
209 particle->pos.y = new_y;
214 CometParticleSystem::CometParticleSystem()
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");
219 virtual_width = SCREEN_WIDTH * 2;
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];
230 particle->speed = (cometsize+1)*30 + systemRandom.randf(3.6);
231 } while(particle->speed < 1);
232 particle->speed *= 10; // gravity
234 particles.push_back(particle);
239 CometParticleSystem::parse(const Reader& reader)
241 reader.get("z-pos", z_pos);
244 CometParticleSystem::~CometParticleSystem()
247 delete cometimages[i];
250 void CometParticleSystem::update(float elapsed_time)
254 std::vector<Particle*>::iterator i;
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));
268 int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x);
270 //FIXME: Don't move particles over solid tiles
271 particle->pos.x = new_x;
272 particle->pos.y = new_y;