X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsector.cpp;h=cd4276ba784e6a8994683d48e539d971ec93d20a;hb=99cf62c2d44b4555e9761f1c8f1b10cf880c33fb;hp=79ad4598627c17b996fc2dc5bad84381b8f79c33;hpb=d781e98d106edc15267599be6c471d38d108a3a7;p=supertux.git diff --git a/src/sector.cpp b/src/sector.cpp index 79ad45986..cd4276ba7 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -28,7 +28,8 @@ #include #include #include -#include +//#include +#include #include "sector.hpp" #include "object/player.hpp" @@ -177,6 +178,7 @@ Sector::parse_object(const std::string& name, const lisp::Lisp& reader) void Sector::parse(const lisp::Lisp& sector) { + bool has_background = false; lisp::ListIterator iter(§or); while(iter.next()) { const std::string& token = iter.item(); @@ -202,11 +204,22 @@ Sector::parse(const lisp::Lisp& sector) } else { GameObject* object = parse_object(token, *(iter.lisp())); if(object) { + if(dynamic_cast(object)) { + has_background = true; + } else if(dynamic_cast(object)) { + has_background = true; + } add_object(object); } } } + if(!has_background) { + Gradient* gradient = new Gradient(); + gradient->set_gradient(Color(0.3, 0.4, 0.75), Color(1, 1, 1)); + add_object(gradient); + } + update_game_objects(); if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; @@ -233,7 +246,7 @@ Sector::parse_old_format(const lisp::Lisp& reader) if (backgroundimage == "arctis2.jpg") backgroundimage = "arctis.jpg"; if (backgroundimage == "ocean.png") backgroundimage = "ocean.jpg"; backgroundimage = "images/background/" + backgroundimage; - if (!PHYSFS_exists(backgroundimage.c_str())) { + if (!Unison::VFS::FileSystem::get().exists(backgroundimage)) { log_warning << "Background image \"" << backgroundimage << "\" not found. Ignoring." << std::endl; backgroundimage = ""; } @@ -684,6 +697,18 @@ Sector::update_game_objects() gameobjects.push_back(object); } gameobjects_new.clear(); + + /* update solid_tilemaps list */ + //FIXME: this could be more efficient + solid_tilemaps.clear(); + for(std::vector::iterator i = gameobjects.begin(); + i != gameobjects.end(); ++i) + { + TileMap* tm = dynamic_cast(*i); + if (!tm) continue; + if (tm->is_solid()) solid_tilemaps.push_back(tm); + } + } bool @@ -865,11 +890,18 @@ void check_collisions(collision::Constraints* constraints, if(!collision::intersects(r1, r2)) return; + MovingObject *moving_object = dynamic_cast (object); + CollisionHit dummy; + if(other != NULL && !other->collides(*object, dummy)) + return; + if(moving_object != NULL && !moving_object->collides(*other, dummy)) + return; + // calculate intersection - float itop = r1.get_bottom() - r2.get_top(); + float itop = r1.get_bottom() - r2.get_top(); float ibottom = r2.get_bottom() - r1.get_top(); - float ileft = r1.get_right() - r2.get_left(); - float iright = r2.get_right() - r1.get_left(); + float ileft = r1.get_right() - r2.get_left(); + float iright = r2.get_right() - r1.get_left(); if(fabsf(movement.y) > fabsf(movement.x)) { if(ileft < SHIFT_DELTA) { @@ -891,10 +923,10 @@ void check_collisions(collision::Constraints* constraints, } if(other != NULL) { - CollisionHit dummy; HitResponse response = other->collision(*object, dummy); if(response == PASSTHROUGH) return; + if(other->get_movement() != Vector(0, 0)) { // TODO what todo when we collide with 2 moving objects?!? constraints->ground_movement = other->get_movement(); @@ -1048,12 +1080,19 @@ Sector::collision_object(MovingObject* object1, MovingObject* object2) const Vector normal; get_hit_normal(r1, r2, hit, normal); + if(!object1->collides(*object2, hit)) + return; + std::swap(hit.left, hit.right); + std::swap(hit.top, hit.bottom); + if(!object2->collides(*object1, hit)) + return; + std::swap(hit.left, hit.right); + std::swap(hit.top, hit.bottom); + HitResponse response1 = object1->collision(*object2, hit); std::swap(hit.left, hit.right); std::swap(hit.top, hit.bottom); HitResponse response2 = object2->collision(*object1, hit); - assert( response1 != SOLID && response1 != PASSTHROUGH ); - assert( response2 != SOLID && response2 != PASSTHROUGH ); if(response1 == CONTINUE && response2 == CONTINUE) { normal *= (0.5 + DELTA); object1->dest.move(-normal); @@ -1144,8 +1183,10 @@ Sector::collision_static_constrains(MovingObject& object) if(constraints.right < infinity) { float width = constraints.right - constraints.left; if(width + SHIFT_DELTA < owidth) { +#if 0 printf("Object %p crushed horizontally... L:%f R:%f\n", &object, constraints.left, constraints.right); +#endif CollisionHit h; h.left = true; h.right = true; @@ -1174,7 +1215,9 @@ Sector::collision_static_constrains(MovingObject& object) if(constraints.bottom < infinity) { float height = constraints.bottom - constraints.top; if(height + SHIFT_DELTA < oheight) { +#if 0 printf("Object %p crushed vertically...\n", &object); +#endif CollisionHit h; h.top = true; h.bottom = true; @@ -1184,6 +1227,10 @@ Sector::collision_static_constrains(MovingObject& object) } } +namespace { + const float MAX_SPEED = 16.0f; +} + void Sector::handle_collisions() { @@ -1193,6 +1240,13 @@ Sector::handle_collisions() for(MovingObjects::iterator i = moving_objects.begin(); i != moving_objects.end(); ++i) { MovingObject* moving_object = *i; + Vector mov = moving_object->get_movement(); + + // make sure movement is never faster than MAX_SPEED. Norm is pretty fat, so two addl. checks are done before. + if (((mov.x > MAX_SPEED * M_SQRT1_2) || (mov.y > MAX_SPEED * M_SQRT1_2)) && (mov.norm() > MAX_SPEED)) { + moving_object->movement = mov.unit() * MAX_SPEED; + //log_debug << "Temporarily reduced object's speed of " << mov.norm() << " to " << moving_object->movement.norm() << "." << std::endl; + } moving_object->dest = moving_object->get_bbox(); moving_object->dest.move(moving_object->get_movement()); @@ -1249,6 +1303,11 @@ Sector::handle_collisions() CollisionHit hit; get_hit_normal(moving_object->dest, moving_object_2->dest, hit, normal); + if(!moving_object->collides(*moving_object_2, hit)) + continue; + if(!moving_object_2->collides(*moving_object, hit)) + continue; + moving_object->collision(*moving_object_2, hit); moving_object_2->collision(*moving_object, hit); } @@ -1437,7 +1496,9 @@ Sector::inside(const Rect& rect) const TileMap* solids = *i; bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset())); bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset()); - if (horizontally && vertically) return true; + + if (horizontally && vertically) + return true; } return false; } @@ -1446,10 +1507,14 @@ float Sector::get_width() const { float width = 0; - for(std::list::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) { + for(std::list::const_iterator i = solid_tilemaps.begin(); + i != solid_tilemaps.end(); i++) { TileMap* solids = *i; - if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset()); + if ((solids->get_width() * 32 + solids->get_x_offset()) > width) { + width = solids->get_width() * 32 + solids->get_x_offset(); + } } + return width; } @@ -1457,10 +1522,14 @@ float Sector::get_height() const { float height = 0; - for(std::list::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) { + for(std::list::const_iterator i = solid_tilemaps.begin(); + i != solid_tilemaps.end(); i++) { TileMap* solids = *i; - if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset()); + if ((solids->get_height() * 32 + solids->get_y_offset()) > height) { + height = solids->get_height() * 32 + solids->get_y_offset(); + } } + return height; }