X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsupertux%2Fsector.cpp;h=519a2f4c8f2c56d505d97009ff66e7d4e44954f9;hb=04270f0886aa08cb8fee7dba58d41975ddb1cb72;hp=52d345fd2453648e1ee01a325e99e3f9bd251717;hpb=08813a74da6ac1fd045a105e4e8105f1d7f716f0;p=supertux.git diff --git a/src/supertux/sector.cpp b/src/supertux/sector.cpp index 52d345fd2..519a2f4c8 100644 --- a/src/supertux/sector.cpp +++ b/src/supertux/sector.cpp @@ -24,10 +24,15 @@ #include "lisp/list_iterator.hpp" #include "math/aatriangle.hpp" #include "object/background.hpp" +#include "object/bonus_block.hpp" +#include "object/brick.hpp" #include "object/bullet.hpp" #include "object/camera.hpp" +#include "object/cloud_particle_system.hpp" #include "object/coin.hpp" +#include "object/comet_particle_system.hpp" #include "object/display_effect.hpp" +#include "object/ghost_particle_system.hpp" #include "object/gradient.hpp" #include "object/invisible_block.hpp" #include "object/particlesystem.hpp" @@ -35,16 +40,22 @@ #include "object/player.hpp" #include "object/portable.hpp" #include "object/pulsing_light.hpp" +#include "object/rain_particle_system.hpp" #include "object/smoke_cloud.hpp" +#include "object/snow_particle_system.hpp" #include "object/text_object.hpp" #include "object/tilemap.hpp" -#include "physfs/physfs_stream.hpp" +#include "physfs/ifile_streambuf.hpp" +#include "scripting/scripting.hpp" #include "scripting/squirrel_util.hpp" #include "supertux/collision.hpp" #include "supertux/constants.hpp" +#include "supertux/game_session.hpp" +#include "supertux/globals.hpp" #include "supertux/level.hpp" -#include "supertux/main.hpp" #include "supertux/object_factory.hpp" +#include "supertux/player_status.hpp" +#include "supertux/savegame.hpp" #include "supertux/spawn_point.hpp" #include "supertux/tile.hpp" #include "trigger/sequence_trigger.hpp" @@ -56,40 +67,52 @@ bool Sector::show_collrects = false; bool Sector::draw_solids_only = false; Sector::Sector(Level* parent) : - level(parent), + level(parent), + name(), + bullets(), + init_script(), + gameobjects_new(), currentmusic(LEVEL_MUSIC), - ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), - gravity(10.0), - player(0), - camera(0), + sector_table(), + scripts(), + ambient_light( 1.0f, 1.0f, 1.0f, 1.0f ), + gameobjects(), + moving_objects(), + spawnpoints(), + portables(), + music(), + gravity(10.0), + player(0), + solid_tilemaps(), + camera(0), effect(0) { - add_object(new Player(player_status, "Tux")); + add_object(new Player(GameSession::current()->get_savegame().get_player_status(), "Tux")); add_object(new DisplayEffect("Effect")); add_object(new TextObject("Text")); - sound_manager->preload("sounds/shoot.wav"); + SoundManager::current()->preload("sounds/shoot.wav"); // create a new squirrel table for the sector - using namespace Scripting; + using namespace scripting; sq_collectgarbage(global_vm); sq_newtable(global_vm); sq_pushroottable(global_vm); if(SQ_FAILED(sq_setdelegate(global_vm, -2))) - throw Scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate"); + throw scripting::SquirrelError(global_vm, "Couldn't set sector_table delegate"); sq_resetobject(§or_table); if(SQ_FAILED(sq_getstackobj(global_vm, -1, §or_table))) - throw Scripting::SquirrelError(global_vm, "Couldn't get sector table"); + throw scripting::SquirrelError(global_vm, "Couldn't get sector table"); sq_addref(global_vm, §or_table); sq_pop(global_vm, 1); } Sector::~Sector() { - using namespace Scripting; + using namespace scripting; deactivate(); @@ -123,43 +146,42 @@ Sector::get_level() } GameObject* -Sector::parse_object(const std::string& name, const Reader& reader) +Sector::parse_object(const std::string& name_, const Reader& reader) { - if(name == "camera") { - Camera* camera = new Camera(this, "Camera"); - camera->parse(reader); - return camera; - } else if(name == "particles-snow") { + if(name_ == "camera") { + Camera* camera_ = new Camera(this, "Camera"); + camera_->parse(reader); + return camera_; + } else if(name_ == "particles-snow") { SnowParticleSystem* partsys = new SnowParticleSystem(); partsys->parse(reader); return partsys; - } else if(name == "particles-rain") { + } else if(name_ == "particles-rain") { RainParticleSystem* partsys = new RainParticleSystem(); partsys->parse(reader); return partsys; - } else if(name == "particles-comets") { + } else if(name_ == "particles-comets") { CometParticleSystem* partsys = new CometParticleSystem(); partsys->parse(reader); return partsys; - } else if(name == "particles-ghosts") { + } else if(name_ == "particles-ghosts") { GhostParticleSystem* partsys = new GhostParticleSystem(); partsys->parse(reader); return partsys; - } else if(name == "particles-clouds") { + } else if(name_ == "particles-clouds") { CloudParticleSystem* partsys = new CloudParticleSystem(); partsys->parse(reader); return partsys; - } else if(name == "money") { // for compatibility with old maps + } else if(name_ == "money") { // for compatibility with old maps return new Jumpy(reader); + } else { + try { + return ObjectFactory::instance().create(name_, reader); + } catch(std::exception& e) { + log_warning << e.what() << "" << std::endl; + return 0; + } } - - try { - return create_object(name, reader); - } catch(std::exception& e) { - log_warning << e.what() << "" << std::endl; - } - - return 0; } void @@ -176,7 +198,7 @@ Sector::parse(const Reader& sector) } else if(token == "music") { iter.value()->get(music); } else if(token == "spawnpoint") { - SpawnPoint* sp = new SpawnPoint(iter.lisp()); + SpawnPoint* sp = new SpawnPoint(*iter.lisp()); spawnpoints.push_back(sp); } else if(token == "init-script") { iter.value()->get(init_script); @@ -209,7 +231,7 @@ Sector::parse(const Reader& sector) update_game_objects(); - if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; + if(solid_tilemaps.size() < 1) { log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; } fix_old_tiles(); if(!camera) { @@ -369,12 +391,12 @@ Sector::parse_old_format(const Reader& reader) } // add a camera - Camera* camera = new Camera(this, "Camera"); - add_object(camera); + Camera* camera_ = new Camera(this, "Camera"); + add_object(camera_); update_game_objects(); - if(solid_tilemaps.size() < 1) log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; + if(solid_tilemaps.size() < 1) { log_warning << "sector '" << name << "' does not contain a solid tile layer." << std::endl; } fix_old_tiles(); update_game_objects(); @@ -389,19 +411,26 @@ Sector::fix_old_tiles() for(size_t y=0; y < solids->get_height(); ++y) { uint32_t id = solids->get_tile_id(x, y); const Tile *tile = solids->get_tile(x, y); - Vector pos(solids->get_x_offset() + x*32, solids->get_y_offset() + y*32); + Vector pos = solids->get_tile_position(x, y); if(id == 112) { add_object(new InvisibleBlock(pos)); solids->change(x, y, 0); } else if(tile->getAttributes() & Tile::COIN) { - add_object(new Coin(pos)); + add_object(new Coin(pos, solids)); solids->change(x, y, 0); } else if(tile->getAttributes() & Tile::FULLBOX) { add_object(new BonusBlock(pos, tile->getData())); solids->change(x, y, 0); } else if(tile->getAttributes() & Tile::BRICK) { - add_object(new Brick(pos, tile->getData())); + if( ( id == 78 ) || ( id == 105 ) ){ + add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brickIce.sprite") ); + } else if( ( id == 77 ) || ( id == 104 ) ){ + add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") ); + } else { + log_warning << "attribute 'brick #t' is not supported for tile-id " << id << std::endl; + add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") ); + } solids->change(x, y, 0); } else if(tile->getAttributes() & Tile::GOAL) { std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux"; @@ -419,8 +448,8 @@ Sector::fix_old_tiles() for(size_t x=0; x < tm->get_width(); ++x) { for(size_t y=0; y < tm->get_height(); ++y) { uint32_t id = tm->get_tile_id(x, y); - Vector pos(tm->get_x_offset() + x*32, tm->get_y_offset() + y*32); - Vector center(pos.x + 16, pos.y + 16); + Vector pos = tm->get_tile_position(x, y); + Vector center = pos + Vector(16, 16); // torch if (id == 1517) { @@ -447,7 +476,7 @@ Sector::fix_old_tiles() HSQUIRRELVM Sector::run_script(std::istream& in, const std::string& sourcename) { - using namespace Scripting; + using namespace scripting; // garbage collect thread list for(ScriptList::iterator i = scripts.begin(); @@ -486,18 +515,14 @@ void Sector::add_object(GameObject* object) { // make sure the object isn't already in the list -#ifdef DEBUG +#ifndef NDEBUG for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) { - if(*i == object) { - assert("object already added to sector" == 0); - } + assert(*i != object); } for(GameObjects::iterator i = gameobjects_new.begin(); i != gameobjects_new.end(); ++i) { - if(*i == object) { - assert("object already added to sector" == 0); - } + assert(*i != object); } #endif @@ -537,12 +562,12 @@ Sector::activate(const Vector& player_pos) _current = this; // register sectortable as sector in scripting - HSQUIRRELVM vm = Scripting::global_vm; + HSQUIRRELVM vm = scripting::global_vm; sq_pushroottable(vm); sq_pushstring(vm, "sector", -1); sq_pushobject(vm, sector_table); if(SQ_FAILED(sq_createslot(vm, -3))) - throw Scripting::SquirrelError(vm, "Couldn't set sector in roottable"); + throw scripting::SquirrelError(vm, "Couldn't set sector in roottable"); sq_pop(vm, 1); for(GameObjects::iterator i = gameobjects.begin(); @@ -554,22 +579,37 @@ Sector::activate(const Vector& player_pos) } try_expose_me(); - // spawn smalltux below spawnpoint - if (!player->is_big()) { - player->move(player_pos + Vector(0,32)); - } else { - player->move(player_pos); - } - // spawning tux in the ground would kill him - if(!is_free_of_tiles(player->get_bbox())) { - log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl; - Vector npos = player->get_bbox().p1; - npos.y-=32; - player->move(npos); + // two-player hack: move other players to main player's position + // Maybe specify 2 spawnpoints in the level? + for(GameObjects::iterator i = gameobjects.begin(); + i != gameobjects.end(); ++i) { + Player* p = dynamic_cast(*i); + if (!p) continue; + + // spawn smalltux below spawnpoint + if (!p->is_big()) { + p->move(player_pos + Vector(0,32)); + } else { + p->move(player_pos); + } + + // spawning tux in the ground would kill him + if(!is_free_of_tiles(p->get_bbox())) { + log_warning << "Tried spawning Tux in solid matter. Compensating." << std::endl; + Vector npos = p->get_bbox().p1; + npos.y-=32; + p->move(npos); + } } + //FIXME: This is a really dirty workaround for this strange camera jump + player->move(player->get_pos()+Vector(-32, 0)); camera->reset(player->get_pos()); + camera->update(1); + player->move(player->get_pos()+(Vector(32, 0))); + camera->update(1); + update_game_objects(); //Run default.nut just before init script @@ -577,7 +617,8 @@ Sector::activate(const Vector& player_pos) std::string basedir = FileSystem::dirname(get_level()->filename); if(PHYSFS_exists((basedir + "/info").c_str())) { try { - IFileStream in(basedir + "/default.nut"); + IFileStreambuf ins(basedir + "/default.nut"); + std::istream in(&ins); run_script(in, "default.nut"); } catch(std::exception& ) { // doesn't exist or erroneous; do nothing @@ -598,11 +639,11 @@ Sector::deactivate() return; // remove sector entry from global vm - HSQUIRRELVM vm = Scripting::global_vm; + HSQUIRRELVM vm = scripting::global_vm; sq_pushroottable(vm); sq_pushstring(vm, "sector", -1); if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse))) - throw Scripting::SquirrelError(vm, "Couldn't unset sector in roottable"); + throw scripting::SquirrelError(vm, "Couldn't unset sector in roottable"); sq_pop(vm, 1); for(GameObjects::iterator i = gameobjects.begin(); @@ -616,10 +657,10 @@ Sector::deactivate() _current = NULL; } -Rect +Rectf Sector::get_active_region() { - return Rect( + return Rectf( camera->get_translation() - Vector(1600, 1200), camera->get_translation() + Vector(1600, 1200) + Vector(SCREEN_WIDTH,SCREEN_HEIGHT)); } @@ -627,7 +668,7 @@ Sector::get_active_region() void Sector::update(float elapsed_time) { - player->check_bounds(camera); + player->check_bounds(); /* update objects */ for(GameObjects::iterator i = gameobjects.begin(); @@ -711,31 +752,31 @@ Sector::before_object_add(GameObject* object) solid_tilemaps.push_back(tilemap); } - Camera* camera = dynamic_cast (object); - if(camera != NULL) { + Camera* camera_ = dynamic_cast (object); + if(camera_ != NULL) { if(this->camera != 0) { log_warning << "Multiple cameras added. Ignoring" << std::endl; return false; } - this->camera = camera; + this->camera = camera_; } - Player* player = dynamic_cast (object); - if(player != NULL) { + Player* player_ = dynamic_cast (object); + if(player_ != NULL) { if(this->player != 0) { log_warning << "Multiple players added. Ignoring" << std::endl; return false; } - this->player = player; + this->player = player_; } - DisplayEffect* effect = dynamic_cast (object); - if(effect != NULL) { + DisplayEffect* effect_ = dynamic_cast (object); + if(effect_ != NULL) { if(this->effect != 0) { log_warning << "Multiple DisplayEffects added. Ignoring" << std::endl; return false; } - this->effect = effect; + this->effect = effect_; } if(_current == this) { @@ -748,11 +789,11 @@ Sector::before_object_add(GameObject* object) void Sector::try_expose(GameObject* object) { - ScriptInterface* interface = dynamic_cast (object); - if(interface != NULL) { - HSQUIRRELVM vm = Scripting::global_vm; + ScriptInterface* object_ = dynamic_cast (object); + if(object_ != NULL) { + HSQUIRRELVM vm = scripting::global_vm; sq_pushobject(vm, sector_table); - interface->expose(vm, -1); + object_->expose(vm, -1); sq_pop(vm, 1); } } @@ -760,10 +801,10 @@ Sector::try_expose(GameObject* object) void Sector::try_expose_me() { - HSQUIRRELVM vm = Scripting::global_vm; + HSQUIRRELVM vm = scripting::global_vm; sq_pushobject(vm, sector_table); - Scripting::SSector* interface = static_cast (this); - expose_object(vm, -1, interface, "settings", false); + scripting::SSector* this_ = static_cast (this); + expose_object(vm, -1, this_, "settings", false); sq_pop(vm, 1); } @@ -791,13 +832,13 @@ Sector::before_object_remove(GameObject* object) void Sector::try_unexpose(GameObject* object) { - ScriptInterface* interface = dynamic_cast (object); - if(interface != NULL) { - HSQUIRRELVM vm = Scripting::global_vm; + ScriptInterface* object_ = dynamic_cast (object); + if(object_ != NULL) { + HSQUIRRELVM vm = scripting::global_vm; SQInteger oldtop = sq_gettop(vm); sq_pushobject(vm, sector_table); try { - interface->unexpose(vm, -1); + object_->unexpose(vm, -1); } catch(std::exception& e) { log_warning << "Couldn't unregister object: " << e.what() << std::endl; } @@ -808,11 +849,11 @@ Sector::try_unexpose(GameObject* object) void Sector::try_unexpose_me() { - HSQUIRRELVM vm = Scripting::global_vm; + HSQUIRRELVM vm = scripting::global_vm; SQInteger oldtop = sq_gettop(vm); sq_pushobject(vm, sector_table); try { - Scripting::unexpose_object(vm, -1, "settings"); + scripting::unexpose_object(vm, -1, "settings"); } catch(std::exception& e) { log_warning << "Couldn't unregister object: " << e.what() << std::endl; } @@ -842,13 +883,13 @@ Sector::draw(DrawingContext& context) } if(show_collrects) { - Color col(0.2f, 0.2f, 0.2f, 0.7f); + Color color(1.0f, 0.0f, 0.0f, 0.75f); for(MovingObjects::iterator i = moving_objects.begin(); i != moving_objects.end(); ++i) { MovingObject* object = *i; - const Rect& rect = object->get_bbox(); + const Rectf& rect = object->get_bbox(); - context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10); + context.draw_filled_rect(rect, color, LAYER_FOREGROUND1 + 10); } } @@ -861,10 +902,10 @@ Sector::draw(DrawingContext& context) /** r1 is supposed to be moving, r2 a solid object */ void check_collisions(collision::Constraints* constraints, - const Vector& movement, const Rect& r1, const Rect& r2, - GameObject* object = NULL, MovingObject* other = NULL, const Vector& addl_ground_movement = Vector(0,0)) + const Vector& obj_movement, const Rectf& obj_rect, const Rectf& other_rect, + GameObject* object = NULL, MovingObject* other = NULL, const Vector& other_movement = Vector(0,0)) { - if(!collision::intersects(r1, r2)) + if(!collision::intersects(obj_rect, other_rect)) return; MovingObject *moving_object = dynamic_cast (object); @@ -875,34 +916,34 @@ void check_collisions(collision::Constraints* constraints, return; // calculate intersection - 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 itop = obj_rect.get_bottom() - other_rect.get_top(); + float ibottom = other_rect.get_bottom() - obj_rect.get_top(); + float ileft = obj_rect.get_right() - other_rect.get_left(); + float iright = other_rect.get_right() - obj_rect.get_left(); - if(fabsf(movement.y) > fabsf(movement.x)) { + if(fabsf(obj_movement.y) > fabsf(obj_movement.x)) { if(ileft < SHIFT_DELTA) { - constraints->right = std::min(constraints->right, r2.get_left()); + constraints->constrain_right(other_rect.get_left(), other_movement.x); return; } else if(iright < SHIFT_DELTA) { - constraints->left = std::max(constraints->left, r2.get_right()); + constraints->constrain_left(other_rect.get_right(), other_movement.x); return; } } else { // shiftout bottom/top if(itop < SHIFT_DELTA) { - constraints->bottom = std::min(constraints->bottom, r2.get_top()); + constraints->constrain_bottom(other_rect.get_top(), other_movement.y); return; } else if(ibottom < SHIFT_DELTA) { - constraints->top = std::max(constraints->top, r2.get_bottom()); + constraints->constrain_top(other_rect.get_bottom(), other_movement.y); return; } } - constraints->ground_movement += addl_ground_movement; + constraints->ground_movement += other_movement; if(other != NULL) { HitResponse response = other->collision(*object, dummy); - if(response == PASSTHROUGH) + if(response == ABORT_MOVE) return; if(other->get_movement() != Vector(0, 0)) { @@ -915,18 +956,18 @@ void check_collisions(collision::Constraints* constraints, float horiz_penetration = std::min(ileft, iright); if(vert_penetration < horiz_penetration) { if(itop < ibottom) { - constraints->bottom = std::min(constraints->bottom, r2.get_top()); + constraints->constrain_bottom(other_rect.get_top(), other_movement.y); constraints->hit.bottom = true; } else { - constraints->top = std::max(constraints->top, r2.get_bottom()); + constraints->constrain_top(other_rect.get_bottom(), other_movement.y); constraints->hit.top = true; } } else { if(ileft < iright) { - constraints->right = std::min(constraints->right, r2.get_left()); + constraints->constrain_right(other_rect.get_left(), other_movement.x); constraints->hit.right = true; } else { - constraints->left = std::max(constraints->left, r2.get_right()); + constraints->constrain_left(other_rect.get_right(), other_movement.x); constraints->hit.left = true; } } @@ -934,7 +975,8 @@ void check_collisions(collision::Constraints* constraints, void Sector::collision_tilemap(collision::Constraints* constraints, - const Vector& movement, const Rect& dest) const + const Vector& movement, const Rectf& dest, + MovingObject& object) const { // calculate rectangle where the object will move float x1 = dest.get_left(); @@ -946,36 +988,42 @@ Sector::collision_tilemap(collision::Constraints* constraints, TileMap* solids = *i; // test with all tiles in this rectangle - int starttilex = int(x1 - solids->get_x_offset()) / 32; - int starttiley = int(y1 - solids->get_y_offset()) / 32; - int max_x = int(x2 - solids->get_x_offset()); - int max_y = int(y2+1 - solids->get_y_offset()); + Rect test_tiles = solids->get_tiles_overlapping(Rectf(x1, y1, x2, y2)); - for(int x = starttilex; x*32 < max_x; ++x) { - for(int y = starttiley; y*32 < max_y; ++y) { + for(int x = test_tiles.left; x < test_tiles.right; ++x) { + for(int y = test_tiles.top; y < test_tiles.bottom; ++y) { const Tile* tile = solids->get_tile(x, y); if(!tile) continue; // skip non-solid tiles - if((tile->getAttributes() & Tile::SOLID) == 0) + if(!tile->is_solid ()) continue; - // only handle unisolid when the player is falling down and when he was - // above the tile before - if(tile->getAttributes() & Tile::UNISOLID) { - if(movement.y <= 0 || dest.get_bottom() - movement.y - SHIFT_DELTA > y*32) + Rectf tile_bbox = solids->get_tile_bbox(x, y); + + /* If the tile is a unisolid tile, the "is_solid()" function above + * didn't do a thorough check. Calculate the position and (relative) + * movement of the object and determine whether or not the tile is + * solid with regard to those parameters. */ + if(tile->is_unisolid ()) { + Vector relative_movement = movement + - solids->get_movement(/* actual = */ true); + + if (!tile->is_solid (tile_bbox, object.get_bbox(), relative_movement)) continue; - } + } /* if (tile->is_unisolid ()) */ - if(tile->getAttributes() & Tile::SLOPE) { // slope tile + if(tile->is_slope ()) { // slope tile AATriangle triangle; - Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset()); - Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset()); - triangle = AATriangle(p1, p2, tile->getData()); + int slope_data = tile->getData(); + if (solids->get_drawing_effect() & VERTICAL_FLIP) + slope_data = AATriangle::vertical_flip(slope_data); + triangle = AATriangle(tile_bbox, slope_data); - collision::rectangle_aatriangle(constraints, dest, triangle, solids->get_movement()); + collision::rectangle_aatriangle(constraints, dest, triangle, + solids->get_movement(/* actual = */ false)); } else { // normal rectangular tile - Rect rect(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset(), (x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset()); - check_collisions(constraints, movement, dest, rect, NULL, NULL, solids->get_movement()); + check_collisions(constraints, movement, dest, tile_bbox, NULL, NULL, + solids->get_movement(/* actual = */ false)); } } } @@ -983,30 +1031,36 @@ Sector::collision_tilemap(collision::Constraints* constraints, } uint32_t -Sector::collision_tile_attributes(const Rect& dest) const +Sector::collision_tile_attributes(const Rectf& dest) const { float x1 = dest.p1.x; float y1 = dest.p1.y; float x2 = dest.p2.x; - float y2 = dest.p2.y + SHIFT_DELTA; + float y2 = dest.p2.y; uint32_t result = 0; for(std::list::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) { TileMap* solids = *i; // test with all tiles in this rectangle - int starttilex = int(x1 - solids->get_x_offset()) / 32; - int starttiley = int(y1 - solids->get_y_offset()) / 32; - int max_x = int(x2 - solids->get_x_offset()); - int max_y = int(y2+1 - solids->get_y_offset()); + Rect test_tiles = solids->get_tiles_overlapping(Rectf(x1, y1, x2, y2)); + // For ice (only), add a little fudge to recognize tiles Tux is standing on. + Rect test_tiles_ice = solids->get_tiles_overlapping(Rectf(x1, y1, x2, y2 + SHIFT_DELTA)); - for(int x = starttilex; x*32 < max_x; ++x) { - for(int y = starttiley; y*32 < max_y; ++y) { + for(int x = test_tiles.left; x < test_tiles.right; ++x) { + int y; + for(y = test_tiles.top; y < test_tiles.bottom; ++y) { const Tile* tile = solids->get_tile(x, y); if(!tile) continue; result |= tile->getAttributes(); } + for(; y < test_tiles_ice.bottom; ++y) { + const Tile* tile = solids->get_tile(x, y); + if(!tile) + continue; + result |= (tile->getAttributes() & Tile::ICE); + } } } @@ -1014,7 +1068,7 @@ Sector::collision_tile_attributes(const Rect& dest) const } /** fills in CollisionHit and Normal vector of 2 intersecting rectangle */ -static void get_hit_normal(const Rect& r1, const Rect& r2, CollisionHit& hit, +static void get_hit_normal(const Rectf& r1, const Rectf& r2, CollisionHit& hit, Vector& normal) { float itop = r1.get_bottom() - r2.get_top(); @@ -1048,8 +1102,8 @@ Sector::collision_object(MovingObject* object1, MovingObject* object2) const { using namespace collision; - const Rect& r1 = object1->dest; - const Rect& r2 = object2->dest; + const Rectf& r1 = object1->dest; + const Rectf& r2 = object2->dest; CollisionHit hit; if(intersects(object1->dest, object2->dest)) { @@ -1085,10 +1139,10 @@ Sector::collision_object(MovingObject* object1, MovingObject* object2) const void Sector::collision_static(collision::Constraints* constraints, - const Vector& movement, const Rect& dest, - GameObject& object) + const Vector& movement, const Rectf& dest, + MovingObject& object) { - collision_tilemap(constraints, movement, dest); + collision_tilemap(constraints, movement, dest, object); // collision with other (static) objects for(MovingObjects::iterator i = moving_objects.begin(); @@ -1114,9 +1168,7 @@ Sector::collision_static_constrains(MovingObject& object) Constraints constraints; Vector movement = object.get_movement(); - Rect& dest = object.dest; - float owidth = object.get_bbox().get_width(); - float oheight = object.get_bbox().get_height(); + Rectf& dest = object.dest; for(int i = 0; i < 2; ++i) { collision_static(&constraints, Vector(0, movement.y), dest, object); @@ -1124,18 +1176,18 @@ Sector::collision_static_constrains(MovingObject& object) break; // apply calculated horizontal constraints - if(constraints.bottom < infinity) { - float height = constraints.bottom - constraints.top; - if(height < oheight) { + if(constraints.get_position_bottom() < infinity) { + float height = constraints.get_height (); + if(height < object.get_bbox().get_height()) { // we're crushed, but ignore this for now, we'll get this again // later if we're really crushed or things will solve itself when // looking at the vertical constraints } - dest.p2.y = constraints.bottom - DELTA; - dest.p1.y = dest.p2.y - oheight; - } else if(constraints.top > -infinity) { - dest.p1.y = constraints.top + DELTA; - dest.p2.y = dest.p1.y + oheight; + dest.p2.y = constraints.get_position_bottom() - DELTA; + dest.p1.y = dest.p2.y - object.get_bbox().get_height(); + } else if(constraints.get_position_top() > -infinity) { + dest.p1.y = constraints.get_position_top() + DELTA; + dest.p2.y = dest.p1.y + object.get_bbox().get_height(); } } if(constraints.has_constraints()) { @@ -1156,12 +1208,12 @@ Sector::collision_static_constrains(MovingObject& object) break; // apply calculated vertical constraints - if(constraints.right < infinity) { - float width = constraints.right - constraints.left; - if(width + SHIFT_DELTA < owidth) { + float width = constraints.get_width (); + if(width < infinity) { + if(width + SHIFT_DELTA < object.get_bbox().get_width()) { #if 0 printf("Object %p crushed horizontally... L:%f R:%f\n", &object, - constraints.left, constraints.right); + constraints.get_position_left(), constraints.get_position_right()); #endif CollisionHit h; h.left = true; @@ -1169,12 +1221,16 @@ Sector::collision_static_constrains(MovingObject& object) h.crush = true; object.collision_solid(h); } else { - dest.p2.x = constraints.right - DELTA; - dest.p1.x = dest.p2.x - owidth; + float xmid = constraints.get_x_midpoint (); + dest.p1.x = xmid - object.get_bbox().get_width()/2; + dest.p2.x = xmid + object.get_bbox().get_width()/2; } - } else if(constraints.left > -infinity) { - dest.p1.x = constraints.left + DELTA; - dest.p2.x = dest.p1.x + owidth; + } else if(constraints.get_position_right() < infinity) { + dest.p2.x = constraints.get_position_right() - DELTA; + dest.p1.x = dest.p2.x - object.get_bbox().get_width(); + } else if(constraints.get_position_left() > -infinity) { + dest.p1.x = constraints.get_position_left() + DELTA; + dest.p2.x = dest.p1.x + object.get_bbox().get_width(); } } @@ -1188,9 +1244,9 @@ Sector::collision_static_constrains(MovingObject& object) // an extra pass to make sure we're not crushed horizontally constraints = Constraints(); collision_static(&constraints, movement, dest, object); - if(constraints.bottom < infinity) { - float height = constraints.bottom - constraints.top; - if(height + SHIFT_DELTA < oheight) { + if(constraints.get_position_bottom() < infinity) { + float height = constraints.get_height (); + if(height + SHIFT_DELTA < object.get_bbox().get_height()) { #if 0 printf("Object %p crushed vertically...\n", &object); #endif @@ -1252,7 +1308,7 @@ Sector::handle_collisions() continue; uint32_t tile_attributes = collision_tile_attributes(moving_object->dest); - if(tile_attributes > Tile::FIRST_INTERESTING_FLAG) { + if(tile_attributes >= Tile::FIRST_INTERESTING_FLAG) { moving_object->collision_tile(tile_attributes); } } @@ -1322,7 +1378,7 @@ Sector::handle_collisions() } bool -Sector::is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid) const +Sector::is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid) const { using namespace collision; @@ -1330,24 +1386,26 @@ Sector::is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid) const TileMap* solids = *i; // test with all tiles in this rectangle - int starttilex = int(rect.p1.x - solids->get_x_offset()) / 32; - int starttiley = int(rect.p1.y - solids->get_y_offset()) / 32; - int max_x = int(rect.p2.x - solids->get_x_offset()); - int max_y = int(rect.p2.y - solids->get_y_offset()); + Rect test_tiles = solids->get_tiles_overlapping(rect); - for(int x = starttilex; x*32 <= max_x; ++x) { - for(int y = starttiley; y*32 <= max_y; ++y) { + for(int x = test_tiles.left; x < test_tiles.right; ++x) { + for(int y = test_tiles.top; y < test_tiles.bottom; ++y) { const Tile* tile = solids->get_tile(x, y); if(!tile) continue; - if(tile->getAttributes() & Tile::SLOPE) { + if(!(tile->getAttributes() & Tile::SOLID)) + continue; + if(tile->is_unisolid () && ignoreUnisolid) + continue; + if(tile->is_slope ()) { AATriangle triangle; - Vector p1(x*32 + solids->get_x_offset(), y*32 + solids->get_y_offset()); - Vector p2((x+1)*32 + solids->get_x_offset(), (y+1)*32 + solids->get_y_offset()); - triangle = AATriangle(p1, p2, tile->getData()); + Rectf tbbox = solids->get_tile_bbox(x, y); + triangle = AATriangle(tbbox, tile->getData()); Constraints constraints; - if(collision::rectangle_aatriangle(&constraints, rect, triangle) && (!ignoreUnisolid || !(tile->getAttributes() & Tile::UNISOLID))) return false; + if(!collision::rectangle_aatriangle(&constraints, rect, triangle)) + continue; } - if((tile->getAttributes() & Tile::SOLID) && (!ignoreUnisolid || !(tile->getAttributes() & Tile::UNISOLID))) return false; + // We have a solid tile that overlaps the given rectangle. + return false; } } } @@ -1356,7 +1414,7 @@ Sector::is_free_of_tiles(const Rect& rect, const bool ignoreUnisolid) const } bool -Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object, const bool ignoreUnisolid) const +Sector::is_free_of_statics(const Rectf& rect, const MovingObject* ignore_object, const bool ignoreUnisolid) const { using namespace collision; @@ -1376,7 +1434,7 @@ Sector::is_free_of_statics(const Rect& rect, const MovingObject* ignore_object, } bool -Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object) const +Sector::is_free_of_movingstatics(const Rectf& rect, const MovingObject* ignore_object) const { using namespace collision; @@ -1398,7 +1456,7 @@ Sector::is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_ob } bool -Sector::add_bullet(const Vector& pos, float xm, Direction dir) +Sector::add_bullet(const Vector& pos, const PlayerStatus* player_status, float xm, Direction dir) { // TODO remove this function and move these checks elsewhere... @@ -1411,7 +1469,7 @@ Sector::add_bullet(const Vector& pos, float xm, Direction dir) new_bullet = new Bullet(pos, xm, dir, player_status->bonus); add_object(new_bullet); - sound_manager->play("sounds/shoot.wav"); + SoundManager::current()->play("sounds/shoot.wav"); return true; } @@ -1429,16 +1487,16 @@ Sector::play_music(MusicType type) currentmusic = type; switch(currentmusic) { case LEVEL_MUSIC: - sound_manager->play_music(music); + SoundManager::current()->play_music(music); break; case HERRING_MUSIC: - sound_manager->play_music("music/invincible.music"); + SoundManager::current()->play_music("music/invincible.ogg"); break; case HERRING_WARNING_MUSIC: - sound_manager->stop_music(TUX_INVINCIBLE_TIME_WARNING); + SoundManager::current()->stop_music(TUX_INVINCIBLE_TIME_WARNING); break; default: - sound_manager->play_music(""); + SoundManager::current()->play_music(""); break; } } @@ -1464,14 +1522,15 @@ Sector::get_total_badguys() } bool -Sector::inside(const Rect& rect) const +Sector::inside(const Rectf& rect) const { for(std::list::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) { 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) + Rectf bbox = solids->get_bbox(); + bbox.p1.y = -INFINITY; // pretend the tilemap extends infinitely far upwards + + if (bbox.contains(rect)) return true; } return false; @@ -1484,9 +1543,7 @@ Sector::get_width() const 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(); - } + width = std::max(width, solids->get_bbox().get_right()); } return width; @@ -1499,9 +1556,7 @@ Sector::get_height() const 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(); - } + height = std::max(height, solids->get_bbox().get_bottom()); } return height; @@ -1543,10 +1598,10 @@ Sector::get_ambient_blue() } void -Sector::set_gravity(float gravity) +Sector::set_gravity(float gravity_) { log_warning << "Changing a Sector's gravitational constant might have unforeseen side-effects" << std::endl; - this->gravity = gravity; + this->gravity = gravity_; } float @@ -1555,4 +1610,52 @@ Sector::get_gravity() const return gravity; } +Player* +Sector::get_nearest_player (const Vector& pos) +{ + Player *nearest_player = NULL; + float nearest_dist = std::numeric_limits::max(); + + std::vector players = Sector::current()->get_players(); + for (std::vector::iterator playerIter = players.begin(); + playerIter != players.end(); + ++playerIter) + { + Player *this_player = *playerIter; + if (this_player->is_dying() || this_player->is_dead()) + continue; + + float this_dist = this_player->get_bbox ().distance(pos); + + if (this_dist < nearest_dist) { + nearest_player = this_player; + nearest_dist = this_dist; + } + } + + return nearest_player; +} /* Player *get_nearest_player */ + +std::vector +Sector::get_nearby_objects (const Vector& center, float max_distance) +{ + std::vector ret; + std::vector players = Sector::current()->get_players(); + + for (size_t i = 0; i < players.size (); i++) { + float distance = players[i]->get_bbox ().distance (center); + if (distance <= max_distance) + ret.push_back (players[i]); + } + + for (size_t i = 0; i < moving_objects.size (); i++) { + float distance = moving_objects[i]->get_bbox ().distance (center); + if (distance <= max_distance) + ret.push_back (moving_objects[i]); + } + + return (ret); +} + +/* vim: set sw=2 sts=2 et : */ /* EOF */