X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fobject%2Fparticlesystem_interactive.cpp;h=de9038aba32d7aa233db4c27f0d6768448ac1136;hb=ae7bd4f460fdd93934fc0abc9589758a49309bda;hp=b2f55ca360f3297991052d9c88a5faabf6987eaa;hpb=1ff837fd2c166214bfec2b573424ddfc03cce8c1;p=supertux.git diff --git a/src/object/particlesystem_interactive.cpp b/src/object/particlesystem_interactive.cpp index b2f55ca36..de9038aba 100644 --- a/src/object/particlesystem_interactive.cpp +++ b/src/object/particlesystem_interactive.cpp @@ -38,6 +38,7 @@ #include "object/camera.hpp" #include "object/rainsplash.hpp" #include "badguy/bomb.hpp" +#include "random_generator.hpp" //TODO: Find a way to make rain collide with objects like bonus blocks // Add an option to set rain strength @@ -60,7 +61,7 @@ ParticleSystem_Interactive::~ParticleSystem_Interactive() void ParticleSystem_Interactive::draw(DrawingContext& context) { context.push_transform(); - + std::vector::iterator i; for(i = particles.begin(); i != particles.end(); ++i) { Particle* particle = *i; @@ -73,7 +74,8 @@ void ParticleSystem_Interactive::draw(DrawingContext& context) int ParticleSystem_Interactive::collision(Particle* object, Vector movement) { - TileMap* solids = Sector::current()->solids; + using namespace collision; + // calculate rectangle where the object will move float x1, x2; float y1, y2; @@ -82,63 +84,69 @@ ParticleSystem_Interactive::collision(Particle* object, Vector movement) y1 = object->pos.y; y2 = y1 + 32 + movement.y; bool water = false; - + // test with all tiles in this rectangle int starttilex = int(x1-1) / 32; int starttiley = int(y1-1) / 32; int max_x = int(x2+1); int max_y = int(y2+1); - - CollisionHit temphit, hit; + Rect dest = Rect(x1, y1, x2, y2); dest.move(movement); - hit.time = -1; // represents an invalid value - for(int x = starttilex; x*32 < max_x; ++x) { - for(int y = starttiley; y*32 < max_y; ++y) { - const Tile* tile = solids->get_tile(x, y); - if(!tile) - continue; - // skip non-solid tiles, except water - if (tile->getAttributes() & Tile::WATER) - water = true; - if(!water && !(tile->getAttributes() & Tile::SOLID)) - continue; - - if(tile->getAttributes() & Tile::SLOPE) { // slope tile - AATriangle triangle; - Vector p1(x*32, y*32); - Vector p2((x+1)*32, (y+1)*32); - triangle = AATriangle(p1, p2, tile->getData()); - - if(Collision::rectangle_aatriangle(temphit, dest, movement, - triangle)) { - if(temphit.time > hit.time) - hit = temphit; - } - } else { // normal rectangular tile - Rect rect(x*32, y*32, (x+1)*32, (y+1)*32); - if(Collision::rectangle_rectangle(temphit, dest, - movement, rect)) { - if(temphit.time > hit.time) - hit = temphit; + Constraints constraints; + + for(std::list::const_iterator i = Sector::current()->solid_tilemaps.begin(); i != Sector::current()->solid_tilemaps.end(); i++) { + TileMap* solids = *i; + for(int x = starttilex; x*32 < max_x; ++x) { + for(int y = starttiley; y*32 < max_y; ++y) { + const Tile* tile = solids->get_tile(x, y); + if(!tile) + continue; + // skip non-solid tiles, except water + if(! (tile->getAttributes() & (Tile::WATER | Tile::SOLID))) + continue; + + if(tile->getAttributes() & Tile::SLOPE) { // slope tile + AATriangle triangle; + Vector p1(x*32, y*32); + Vector p2((x+1)*32, (y+1)*32); + triangle = AATriangle(p1, p2, tile->getData()); + + if(rectangle_aatriangle(&constraints, dest, triangle)) { + if(tile->getAttributes() & Tile::WATER) + water = true; + } + } else { // normal rectangular tile + Rect rect(x*32, y*32, (x+1)*32, (y+1)*32); + if(intersects(dest, rect)) { + if(tile->getAttributes() & Tile::WATER) + water = true; + set_rectangle_rectangle_constraints(&constraints, dest, rect); + } } } } } - + + + // TODO don't use magic numbers here... + // did we collide at all? - if(hit.time < 0) { - return -1; //no collision - } - else { - if (water) - return 0; //collision with water tile - don't draw splash - else { - if ((hit.normal.x == 1) && (hit.normal.y == 0)) - return 2; //collision from right - else return 1; //collision from above + if(!constraints.has_constraints()) + return -1; + + const CollisionHit& hit = constraints.hit; + if (water) { + return 0; //collision with water tile - don't draw splash + } else { + if (hit.right || hit.left) { + return 2; //collision from right + } else { + return 1; //collision from above } } + + return 0; } RainParticleSystem::RainParticleSystem() @@ -152,12 +160,12 @@ RainParticleSystem::RainParticleSystem() size_t raindropcount = size_t(virtual_width/6.0); for(size_t i=0; ipos.x = rand() % int(virtual_width); - particle->pos.y = rand() % int(virtual_height); - int rainsize = rand() % 2; + particle->pos.x = systemRandom.rand(int(virtual_width)); + particle->pos.y = systemRandom.rand(int(virtual_height)); + int rainsize = systemRandom.rand(2); particle->texture = rainimages[rainsize]; do { - particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4); + particle->speed = (rainsize+1)*45 + systemRandom.randf(3.6); } while(particle->speed < 1); particle->speed *= 10; // gravity @@ -214,7 +222,7 @@ void RainParticleSystem::update(float elapsed_time) Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical)); } */ } - int new_x = (rand() % int(virtual_width)) + int(abs_x); + int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x); int new_y = 0; //FIXME: Don't move particles over solid tiles particle->pos.x = new_x; @@ -234,12 +242,12 @@ CometParticleSystem::CometParticleSystem() size_t cometcount = 2; for(size_t i=0; ipos.x = rand() % int(virtual_width); - particle->pos.y = rand() % int(virtual_height); - int cometsize = rand() % 2; + particle->pos.x = systemRandom.rand(int(virtual_width)); + particle->pos.y = systemRandom.rand(int(virtual_height)); + int cometsize = systemRandom.rand(2); particle->texture = cometimages[cometsize]; do { - particle->speed = (cometsize+1)*30 + (float(rand()%10)*.4); + particle->speed = (cometsize+1)*30 + systemRandom.randf(3.6); } while(particle->speed < 1); particle->speed *= 10; // gravity @@ -269,6 +277,8 @@ CometParticleSystem::~CometParticleSystem() void CometParticleSystem::update(float elapsed_time) { + (void) elapsed_time; +#if 0 std::vector::iterator i; for( i = particles.begin(); i != particles.end(); ++i) { @@ -283,11 +293,12 @@ void CometParticleSystem::update(float elapsed_time) if ((particle->pos.y <= SCREEN_HEIGHT + abs_y) && (col >= 1)) { Sector::current()->add_object(new Bomb(particle->pos, LEFT)); } - int new_x = (rand() % int(virtual_width)) + int(abs_x); + int new_x = systemRandom.rand(int(virtual_width)) + int(abs_x); int new_y = 0; //FIXME: Don't move particles over solid tiles particle->pos.x = new_x; particle->pos.y = new_y; } } +#endif }