X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fsector.cpp;h=1d14695a6fbe874b5a84218e0956d0ace439dca7;hb=4a54087f52d6a8a2e5b4c498e772685bb0885991;hp=401e6b62490e806d8e622156566fdaed617dcc13;hpb=2074a5e3f8167dec24989c008ddadda14687a3a6;p=supertux.git diff --git a/src/sector.cpp b/src/sector.cpp index 401e6b624..1d14695a6 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -1,40 +1,88 @@ -#include "sector.h" +// $Id$ +// +// SuperTux - A Jump'n Run +// Copyright (C) 2004 Matthias Braun #include +#include #include #include #include #include -#include "lispreader.h" - -#include "badguy.h" -#include "special.h" -#include "gameobjs.h" -#include "camera.h" -#include "background.h" -#include "particlesystem.h" + +#include "sector.h" +#include "player_status.h" +#include "object/gameobjs.h" +#include "object/camera.h" +#include "object/background.h" +#include "object/particlesystem.h" +#include "object/tilemap.h" +#include "lisp/parser.h" +#include "lisp/lisp.h" +#include "lisp/writer.h" +#include "lisp/list_iterator.h" #include "tile.h" -#include "tilemap.h" -#include "music_manager.h" -#include "gameloop.h" +#include "audio/sound_manager.h" +#include "game_session.h" #include "resources.h" +#include "statistics.h" +#include "collision_grid.h" +#include "collision_grid_iterator.h" +#include "object_factory.h" +#include "collision.h" +#include "math/rect.h" +#include "math/aatriangle.h" +#include "object/coin.h" +#include "object/block.h" +#include "object/invisible_block.h" +#include "object/bullet.h" +#include "badguy/jumpy.h" +#include "badguy/spike.h" +#include "trigger/sequence_trigger.h" +#include "player_status.h" + +//#define USE_GRID Sector* Sector::_current = 0; Sector::Sector() - : gravity(10), player(0), solids(0), background(0), camera(0), + : gravity(10), player(0), solids(0), camera(0), currentmusic(LEVEL_MUSIC) { song_title = "Mortimers_chipdisko.mod"; - player = new Player(); + player = new Player(&player_status); add_object(player); + + grid = new CollisionGrid(32000, 32000); } Sector::~Sector() { + update_game_objects(); + assert(gameobjects_new.size() == 0); + + delete grid; + for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); - ++i) + ++i) { delete *i; + } for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end(); ++i) @@ -44,125 +92,134 @@ Sector::~Sector() _current = 0; } +GameObject* +Sector::parse_object(const std::string& name, const lisp::Lisp& reader) +{ + if(name == "camera") { + Camera* camera = new Camera(this); + camera->parse(reader); + return camera; + } else if(name == "particles-snow") { + SnowParticleSystem* partsys = new SnowParticleSystem(); + partsys->parse(reader); + return partsys; + } else if(name == "particles-rain") { + RainParticleSystem* partsys = new RainParticleSystem(); + partsys->parse(reader); + return partsys; + } else if(name == "particles-clouds") { + CloudParticleSystem* partsys = new CloudParticleSystem(); + partsys->parse(reader); + return partsys; + } else if(name == "money") { // for compatibility with old maps + return new Jumpy(reader); + } + + try { + return create_object(name, reader); + } catch(std::exception& e) { + std::cerr << e.what() << "\n"; + } + + return 0; +} + void -Sector::parse(LispReader& lispreader) +Sector::parse(const lisp::Lisp& sector) { _current = this; - for(lisp_object_t* cur = lispreader.get_lisp(); !lisp_nil_p(cur); - cur = lisp_cdr(cur)) { - std::string token = lisp_symbol(lisp_car(lisp_car(cur))); - lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur))); - LispReader reader(lisp_cdr(lisp_car(cur))); - + lisp::ListIterator iter(§or); + while(iter.next()) { + const std::string& token = iter.item(); if(token == "name") { - name = lisp_string(data); + iter.value()->get(name); } else if(token == "gravity") { - gravity = lisp_integer(data); + iter.value()->get(gravity); } else if(token == "music") { - song_title = lisp_string(data); + iter.value()->get(song_title); load_music(); - } else if(token == "camera") { - if(camera) { - std::cerr << "Warning: More than 1 camera defined in sector.\n"; - continue; - } - camera = new Camera(this); - camera->read(reader); - add_object(camera); - } else if(token == "background") { - background = new Background(reader); - add_object(background); - } else if(token == "playerspawn") { + } else if(token == "spawnpoint") { + const lisp::Lisp* spawnpoint_lisp = iter.lisp(); + SpawnPoint* sp = new SpawnPoint; - reader.read_string("name", sp->name); - reader.read_float("x", sp->pos.x); - reader.read_float("y", sp->pos.y); - } else if(token == "tilemap") { - TileMap* tilemap = new TileMap(reader); - add_object(tilemap); - - if(tilemap->is_solid()) { - if(solids) { - std::cerr << "Warning multiple solid tilemaps in sector.\n"; - continue; - } - solids = tilemap; + spawnpoint_lisp->get("name", sp->name); + spawnpoint_lisp->get("x", sp->pos.x); + spawnpoint_lisp->get("y", sp->pos.y); + spawnpoints.push_back(sp); + } else { + GameObject* object = parse_object(token, *(iter.lisp())); + if(object) { + add_object(object); } - } else if(badguykind_from_string(token) != BAD_INVALID) { - add_object(new BadGuy(badguykind_from_string(token), reader)); - } else if(token == "trampoline") { - add_object(new Trampoline(reader)); - } else if(token == "flying-platform") { - add_object(new FlyingPlatform(reader)); - } else if(token == "particles-snow") { - SnowParticleSystem* partsys = new SnowParticleSystem(); - partsys->parse(reader); - add_object(partsys); - } else if(token == "particles-clouds") { - CloudParticleSystem* partsys = new CloudParticleSystem(); - partsys->parse(reader); - add_object(partsys); } } + update_game_objects(); + fix_old_tiles(); if(!camera) { - std::cerr << "sector does not contain a camera.\n"; - camera = new Camera(this); + std::cerr << "sector '" << name << "' does not contain a camera.\n"; + update_game_objects(); + add_object(new Camera(this)); } if(!solids) throw std::runtime_error("sector does not contain a solid tile layer."); + + update_game_objects(); } void -Sector::parse_old_format(LispReader& reader) +Sector::parse_old_format(const lisp::Lisp& reader) { _current = this; name = "main"; - reader.read_float("gravity", gravity); + reader.get("gravity", gravity); std::string backgroundimage; - reader.read_string("background", backgroundimage); + reader.get("background", backgroundimage); float bgspeed = .5; - reader.read_float("bkgd_speed", bgspeed); + reader.get("bkgd_speed", bgspeed); + bgspeed /= 100; Color bkgd_top, bkgd_bottom; int r = 0, g = 0, b = 128; - reader.read_int("bkgd_red_top", r); - reader.read_int("bkgd_green_top", g); - reader.read_int("bkgd_blue_top", b); + reader.get("bkgd_red_top", r); + reader.get("bkgd_green_top", g); + reader.get("bkgd_blue_top", b); bkgd_top.red = r; bkgd_top.green = g; bkgd_top.blue = b; - reader.read_int("bkgd_red_bottom", r); - reader.read_int("bkgd_green_bottom", g); - reader.read_int("bkgd_blue_bottom", b); + reader.get("bkgd_red_bottom", r); + reader.get("bkgd_green_bottom", g); + reader.get("bkgd_blue_bottom", b); bkgd_bottom.red = r; bkgd_bottom.green = g; bkgd_bottom.blue = b; if(backgroundimage != "") { - background = new Background; + Background* background = new Background; background->set_image(backgroundimage, bgspeed); add_object(background); } else { - background = new Background; + Background* background = new Background; background->set_gradient(bkgd_top, bkgd_bottom); add_object(background); } std::string particlesystem; - reader.read_string("particle_system", particlesystem); + reader.get("particle_system", particlesystem); if(particlesystem == "clouds") add_object(new CloudParticleSystem()); else if(particlesystem == "snow") add_object(new SnowParticleSystem()); + else if(particlesystem == "rain") + add_object(new RainParticleSystem()); Vector startpos(100, 170); - reader.read_float("start_pos_x", startpos.x); - reader.read_float("start_pos_y", startpos.y); + reader.get("start_pos_x", startpos.x); + reader.get("start_pos_y", startpos.y); SpawnPoint* spawn = new SpawnPoint; spawn->pos = startpos; @@ -170,73 +227,139 @@ Sector::parse_old_format(LispReader& reader) spawnpoints.push_back(spawn); song_title = "Mortimers_chipdisko.mod"; - reader.read_string("music", song_title); + reader.get("music", song_title); load_music(); int width, height = 15; - reader.read_int("width", width); - reader.read_int("height", height); + reader.get("width", width); + reader.get("height", height); std::vector tiles; - if(reader.read_int_vector("interactive-tm", tiles) - || reader.read_int_vector("tilemap", tiles)) { + if(reader.get_vector("interactive-tm", tiles) + || reader.get_vector("tilemap", tiles)) { TileMap* tilemap = new TileMap(); tilemap->set(width, height, tiles, LAYER_TILES, true); - solids = tilemap; add_object(tilemap); } - if(reader.read_int_vector("background-tm", tiles)) { + if(reader.get_vector("background-tm", tiles)) { TileMap* tilemap = new TileMap(); tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false); add_object(tilemap); } - if(reader.read_int_vector("foreground-tm", tiles)) { + if(reader.get_vector("foreground-tm", tiles)) { TileMap* tilemap = new TileMap(); tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false); add_object(tilemap); } - // TODO read resetpoints + // read reset-points (now spawn-points) + const lisp::Lisp* resetpoints = reader.get_lisp("reset-points"); + if(resetpoints) { + lisp::ListIterator iter(resetpoints); + while(iter.next()) { + if(iter.item() == "point") { + Vector sp_pos; + if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y)) + { + SpawnPoint* sp = new SpawnPoint; + sp->name = "main"; + sp->pos = sp_pos; + spawnpoints.push_back(sp); + } + } else { + std::cerr << "Unknown token '" << iter.item() << "' in reset-points.\n"; + } + } + } // read objects - { - lisp_object_t* cur = 0; - if(reader.read_lisp("objects", cur)) { - while(!lisp_nil_p(cur)) { - lisp_object_t* data = lisp_car(cur); - std::string object_type = lisp_symbol(lisp_car(data)); - - LispReader reader(lisp_cdr(data)); - - if(object_type == "trampoline") { - add_object(new Trampoline(reader)); - } - else if(object_type == "flying-platform") { - add_object(new FlyingPlatform(reader)); - } - else { - BadGuyKind kind = badguykind_from_string(object_type); - add_object(new BadGuy(kind, reader)); - } - - cur = lisp_cdr(cur); + const lisp::Lisp* objects = reader.get_lisp("objects"); + if(objects) { + lisp::ListIterator iter(objects); + while(iter.next()) { + GameObject* object = parse_object(iter.item(), *(iter.lisp())); + if(object) { + add_object(object); + } else { + std::cerr << "Unknown object '" << iter.item() << "' in level.\n"; } } } // add a camera - camera = new Camera(this); + Camera* camera = new Camera(this); add_object(camera); + + update_game_objects(); + fix_old_tiles(); + update_game_objects(); + if(solids == 0) + throw std::runtime_error("sector does not contain a solid tile layer."); +} + +void +Sector::fix_old_tiles() +{ + // hack for now... + for(size_t x=0; x < solids->get_width(); ++x) { + for(size_t y=0; y < solids->get_height(); ++y) { + const Tile* tile = solids->get_tile(x, y); + Vector pos(x*32, y*32); + + if(tile->getID() == 112) { + add_object(new InvisibleBlock(pos)); + solids->change(x, y, 0); + } else if(tile->getID() == 295) { + add_object(new Spike(pos, Spike::NORTH)); + solids->change(x, y, 0); + } else if(tile->getID() == 296) { + add_object(new Spike(pos, Spike::EAST)); + solids->change(x, y, 0); + } else if(tile->getID() == 297) { + add_object(new Spike(pos, Spike::SOUTH)); + solids->change(x, y, 0); + } else if(tile->getID() == 298) { + add_object(new Spike(pos, Spike::WEST)); + solids->change(x, y, 0); + } else if(tile->getAttributes() & Tile::COIN) { + add_object(new Coin(pos)); + 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())); + solids->change(x, y, 0); + } else if(tile->getAttributes() & Tile::GOAL) { + std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux"; + add_object(new SequenceTrigger(pos, sequence)); + solids->change(x, y, 0); + } + } + } } void -Sector::write(LispWriter& writer) +Sector::write(lisp::Writer& writer) { writer.write_string("name", name); writer.write_float("gravity", gravity); + writer.write_string("music", song_title); + // write spawnpoints + for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end(); + ++i) { + SpawnPoint* spawn = *i; + writer.start_list("spawn-points"); + writer.write_string("name", spawn->name); + writer.write_float("x", spawn->pos.x); + writer.write_float("y", spawn->pos.y); + writer.end_list("spawn-points"); + } + + // write objects for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) { Serializable* serializable = dynamic_cast (*i); @@ -248,49 +371,28 @@ Sector::write(LispWriter& writer) void Sector::add_object(GameObject* object) { - // XXX a bit hackish, at least try to keep the number of these things down... - BadGuy* badguy = dynamic_cast (object); - if(badguy) - badguys.push_back(badguy); - Bullet* bullet = dynamic_cast (object); - if(bullet) - bullets.push_back(bullet); - Upgrade* upgrade = dynamic_cast (object); - if(upgrade) - upgrades.push_back(upgrade); - Trampoline* trampoline = dynamic_cast (object); - if(trampoline) - trampolines.push_back(trampoline); - FlyingPlatform* flying_platform = dynamic_cast (object); - if(flying_platform) - flying_platforms.push_back(flying_platform); - Background* background = dynamic_cast (object); - if(background) - this->background = background; - - gameobjects.push_back(object); + // make sure the object isn't already in the list +#ifdef DEBUG + for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); + ++i) { + if(*i == object) { + assert("object already added to sector" == 0); + } + } + for(GameObjects::iterator i = gameobjects_new.begin(); + i != gameobjects_new.end(); ++i) { + if(*i == object) { + assert("object already added to sector" == 0); + } + } +#endif + + gameobjects_new.push_back(object); } void Sector::activate(const std::string& spawnpoint) { - _current = this; - - // Apply bonuses from former levels - switch (player_status.bonus) - { - case PlayerStatus::NO_BONUS: - break; - - case PlayerStatus::FLOWER_BONUS: - player->got_power = Player::FIRE_POWER; // FIXME: add ice power to here - // fall through - - case PlayerStatus::GROWUP_BONUS: - player->grow(false); - break; - } - SpawnPoint* sp = 0; for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end(); ++i) { @@ -298,71 +400,128 @@ Sector::activate(const std::string& spawnpoint) sp = *i; break; } - } + } if(!sp) { std::cerr << "Spawnpoint '" << spawnpoint << "' not found.\n"; + if(spawnpoint != "main") { + activate("main"); + } else { + activate(Vector(0, 0)); + } } else { - player->move(sp->pos); + activate(sp->pos); } +} + +void +Sector::activate(const Vector& player_pos) +{ + _current = this; + + player->move(player_pos); + camera->reset(player->get_pos()); +} - camera->reset(Vector(player->base.x, player->base.y)); +Rect +Sector::get_active_region() +{ + return Rect( + camera->get_translation() - Vector(1600, 1200), + camera->get_translation() + Vector(1600, 1200)); } void Sector::action(float elapsed_time) { player->check_bounds(camera); - - /* update objects (don't use iterators here, because the list might change - * during the iteration) - */ - for(size_t i = 0; i < gameobjects.size(); ++i) - if(gameobjects[i]->is_valid()) - gameobjects[i]->action(elapsed_time); - + +#if 0 + CollisionGridIterator iter(*grid, get_active_region()); + while(MovingObject* object = iter.next()) { + if(!object->is_valid()) + continue; + + object->action(elapsed_time); + } +#else + /* update objects */ + for(GameObjects::iterator i = gameobjects.begin(); + i != gameobjects.end(); ++i) { + GameObject* object = *i; + if(!object->is_valid()) + continue; + + object->action(elapsed_time); + } +#endif + /* Handle all possible collisions. */ - collision_handler(); - + collision_handler(); + update_game_objects(); +} + +void +Sector::update_game_objects() +{ /** cleanup marked objects */ for(std::vector::iterator i = gameobjects.begin(); i != gameobjects.end(); /* nothing */) { - if((*i)->is_valid() == false) { - BadGuy* badguy = dynamic_cast (*i); - if(badguy) { - badguys.erase(std::remove(badguys.begin(), badguys.end(), badguy), - badguys.end()); - } - Bullet* bullet = dynamic_cast (*i); - if(bullet) { - bullets.erase( - std::remove(bullets.begin(), bullets.end(), bullet), - bullets.end()); - } - Upgrade* upgrade = dynamic_cast (*i); - if(upgrade) { - upgrades.erase( - std::remove(upgrades.begin(), upgrades.end(), upgrade), - upgrades.end()); - } - Trampoline* trampoline = dynamic_cast (*i); - if(trampoline) { - trampolines.erase( - std::remove(trampolines.begin(), trampolines.end(), trampoline), - trampolines.end()); + GameObject* object = *i; + + if(object->is_valid()) { + ++i; + continue; + } + + Bullet* bullet = dynamic_cast (object); + if(bullet) { + bullets.erase( + std::remove(bullets.begin(), bullets.end(), bullet), + bullets.end()); + } + MovingObject* movingobject = dynamic_cast (object); + if(movingobject) { + grid->remove_object(movingobject); + } + delete *i; + i = gameobjects.erase(i); + } + + /* add newly created objects */ + for(std::vector::iterator i = gameobjects_new.begin(); + i != gameobjects_new.end(); ++i) + { + GameObject* object = *i; + + Bullet* bullet = dynamic_cast (object); + if(bullet) + bullets.push_back(bullet); + + MovingObject* movingobject = dynamic_cast (object); + if(movingobject) + grid->add_object(movingobject); + + TileMap* tilemap = dynamic_cast (object); + if(tilemap && tilemap->is_solid()) { + if(solids == 0) { + solids = tilemap; + } else { + std::cerr << "Another solid tilemaps added. Ignoring."; } - FlyingPlatform* flying_platform= dynamic_cast (*i); - if(flying_platform) { - flying_platforms.erase( - std::remove(flying_platforms.begin(), flying_platforms.end(), flying_platform), - flying_platforms.end()); + } + + Camera* camera = dynamic_cast (object); + if(camera) { + if(this->camera != 0) { + std::cerr << "Warning: Multiple cameras added. Ignoring."; + continue; } - - delete *i; - i = gameobjects.erase(i); - } else { - ++i; + this->camera = camera; } + + gameobjects.push_back(object); } + gameobjects_new.clear(); } void @@ -370,419 +529,287 @@ Sector::draw(DrawingContext& context) { context.push_transform(); context.set_translation(camera->get_translation()); - + +#if 0 + CollisionGridIterator iter(*grid, get_active_region()); + while(MovingObject* object = iter.next()) { + if(!object->is_valid()) + continue; + + object->draw(context); + } +#else for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) { - if( (*i)->is_valid() ) - (*i)->draw(context); + GameObject* object = *i; + if(!object->is_valid()) + continue; + + object->draw(context); } +#endif context.pop_transform(); } +static const float DELTA = .001; + void -Sector::collision_handler() +Sector::collision_tilemap(MovingObject* object, int depth) { - // CO_BULLET & CO_BADGUY check - for(unsigned int i = 0; i < bullets.size(); ++i) - { - for (BadGuys::iterator j = badguys.begin(); j != badguys.end(); ++j) - { - if((*j)->dying != DYING_NOT) - continue; - - if(rectcollision(bullets[i]->base, (*j)->base)) - { - // We have detected a collision and now call the - // collision functions of the collided objects. - (*j)->collision(bullets[i], CO_BULLET, COLLISION_NORMAL); - bullets[i]->collision(CO_BADGUY); - break; // bullet is invalid now, so break - } - } - } - - /* CO_BADGUY & CO_BADGUY check */ - for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i) - { - if((*i)->dying != DYING_NOT) + if(depth >= 4) { +#ifdef DEBUG + std::cout << "Max collision depth reached.\n"; +#endif + object->movement = Vector(0, 0); + return; + } + + // calculate rectangle where the object will move + float x1, x2; + if(object->get_movement().x >= 0) { + x1 = object->get_pos().x; + x2 = object->get_bbox().p2.x + object->get_movement().x; + } else { + x1 = object->get_pos().x + object->get_movement().x; + x2 = object->get_bbox().p2.x; + } + float y1, y2; + if(object->get_movement().y >= 0) { + y1 = object->get_pos().y; + y2 = object->get_bbox().p2.y + object->get_movement().y; + } else { + y1 = object->get_pos().y + object->get_movement().y; + y2 = object->get_bbox().p2.y; + } + + // 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 = object->get_bbox(); + dest.move(object->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; - - BadGuys::iterator j = i; - ++j; - for (; j != badguys.end(); ++j) - { - if(j == i || (*j)->dying != DYING_NOT) - continue; - - if(rectcollision((*i)->base, (*j)->base)) - { - // We have detected a collision and now call the - // collision functions of the collided objects. - (*j)->collision(*i, CO_BADGUY); - (*i)->collision(*j, CO_BADGUY); - } - } - } - if(player->dying != DYING_NOT) return; - - // CO_BADGUY & CO_PLAYER check - for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i) - { - if((*i)->dying != DYING_NOT) + if(!(tile->getAttributes() & Tile::SOLID)) continue; - - if(rectcollision_offset((*i)->base, player->base, 0, 0)) - { - // We have detected a collision and now call the collision - // functions of the collided objects. - if (player->previous_base.y < player->base.y && - player->previous_base.y + player->previous_base.height - < (*i)->base.y + (*i)->base.height/2 - && !player->invincible_timer.started()) - { - (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH); - } - else - { - player->collision(*i, CO_BADGUY); - (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL); - } + if(tile->getAttributes() & Tile::UNISOLID + && (object->movement.y < 0 || dest.p2.y > y*32)) + 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, object->movement, + triangle)) { + if(temphit.time > hit.time) + hit = temphit; } - } - - // CO_UPGRADE & CO_PLAYER check - for(unsigned int i = 0; i < upgrades.size(); ++i) - { - if(rectcollision(upgrades[i]->base, player->base)) - { - // We have detected a collision and now call the collision - // functions of the collided objects. - upgrades[i]->collision(player, CO_PLAYER, COLLISION_NORMAL); + } else { // normal rectangular tile + Rect rect(x*32, y*32, (x+1)*32, (y+1)*32); + if(Collision::rectangle_rectangle(temphit, dest, + object->movement, rect)) { + if(temphit.time > hit.time) + hit = temphit; } - } - - // CO_TRAMPOLINE & (CO_PLAYER or CO_BADGUY) - for (Trampolines::iterator i = trampolines.begin(); i != trampolines.end(); ++i) - { - if (rectcollision((*i)->base, player->base)) - { - if (player->previous_base.y < player->base.y && - player->previous_base.y + player->previous_base.height - < (*i)->base.y + (*i)->base.height/2) - { - (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH); - } - else if (player->previous_base.y <= player->base.y) - { - player->collision(*i, CO_TRAMPOLINE); - (*i)->collision(player, CO_PLAYER, COLLISION_NORMAL); } } } - - // CO_FLYING_PLATFORM & (CO_PLAYER or CO_BADGUY) - for (FlyingPlatforms::iterator i = flying_platforms.begin(); i != flying_platforms.end(); ++i) - { - if (rectcollision((*i)->base, player->base)) - { - if (player->previous_base.y < player->base.y && - player->previous_base.y + player->previous_base.height - < (*i)->base.y + (*i)->base.height/2) - { - (*i)->collision(player, CO_PLAYER, COLLISION_SQUISH); - player->collision(*i, CO_FLYING_PLATFORM); - } -/* else if (player->previous_base.y <= player->base.y) - { - }*/ - } + + // did we collide at all? + if(hit.time < 0) + return; + + // call collision function + HitResponse response = object->collision(*solids, hit); + if(response == ABORT_MOVE) { + object->movement = Vector(0, 0); + return; + } + if(response == FORCE_MOVE) { + return; } + // move out of collision and try again + object->movement += hit.normal * (hit.depth + DELTA); + collision_tilemap(object, depth+1); } void -Sector::add_score(const Vector& pos, int s) -{ - player_status.score += s; - - add_object(new FloatingScore(pos, s)); -} - -void -Sector::add_bouncy_distro(const Vector& pos) -{ - add_object(new BouncyDistro(pos)); -} - -void -Sector::add_broken_brick(const Vector& pos, Tile* tile) -{ - add_broken_brick_piece(pos, Vector(-1, -4), tile); - add_broken_brick_piece(pos + Vector(0, 16), Vector(-1.5, -3), tile); - - add_broken_brick_piece(pos + Vector(16, 0), Vector(1, -4), tile); - add_broken_brick_piece(pos + Vector(16, 16), Vector(1.5, -3), tile); -} - -void -Sector::add_broken_brick_piece(const Vector& pos, const Vector& movement, - Tile* tile) -{ - add_object(new BrokenBrick(tile, pos, movement)); -} - -void -Sector::add_bouncy_brick(const Vector& pos) +Sector::collision_object(MovingObject* object1, MovingObject* object2) { - add_object(new BouncyBrick(pos)); + CollisionHit hit; + Rect dest1 = object1->get_bbox(); + dest1.move(object1->get_movement()); + Rect dest2 = object2->get_bbox(); + dest2.move(object2->get_movement()); + + Vector movement = object1->get_movement() - object2->get_movement(); + if(Collision::rectangle_rectangle(hit, dest1, movement, dest2)) { + HitResponse response1 = object1->collision(*object2, hit); + hit.normal *= -1; + HitResponse response2 = object2->collision(*object1, hit); + + if(response1 != CONTINUE) { + if(response1 == ABORT_MOVE) + object1->movement = Vector(0, 0); + if(response2 == CONTINUE) + object2->movement += hit.normal * (hit.depth + DELTA); + } else if(response2 != CONTINUE) { + if(response2 == ABORT_MOVE) + object2->movement = Vector(0, 0); + if(response1 == CONTINUE) + object1->movement += -hit.normal * (hit.depth + DELTA); + } else { + object1->movement += -hit.normal * (hit.depth/2 + DELTA); + object2->movement += hit.normal * (hit.depth/2 + DELTA); + } + } } -BadGuy* -Sector::add_bad_guy(float x, float y, BadGuyKind kind) -{ - BadGuy* badguy = new BadGuy(kind, x, y); - add_object(badguy); - return badguy; -} - void -Sector::add_upgrade(const Vector& pos, Direction dir, UpgradeKind kind) +Sector::collision_handler() { - add_object(new Upgrade(pos, dir, kind)); +#ifdef USE_GRID + grid->check_collisions(); +#else + for(std::vector::iterator i = gameobjects.begin(); + i != gameobjects.end(); ++i) { + GameObject* gameobject = *i; + if(!gameobject->is_valid()) + continue; + MovingObject* movingobject = dynamic_cast (gameobject); + if(!movingobject) + continue; + if(movingobject->get_flags() & GameObject::FLAG_NO_COLLDET) { + movingobject->bbox.move(movingobject->movement); + movingobject->movement = Vector(0, 0); + continue; + } + + // collision with tilemap + if(! (movingobject->movement == Vector(0, 0))) + collision_tilemap(movingobject, 0); + + // collision with other objects + for(std::vector::iterator i2 = i+1; + i2 != gameobjects.end(); ++i2) { + GameObject* other_object = *i2; + if(!other_object->is_valid() + || other_object->get_flags() & GameObject::FLAG_NO_COLLDET) + continue; + MovingObject* movingobject2 = dynamic_cast (other_object); + if(!movingobject2) + continue; + + collision_object(movingobject, movingobject2); + } + + movingobject->bbox.move(movingobject->get_movement()); + movingobject->movement = Vector(0, 0); + } +#endif } - + bool Sector::add_bullet(const Vector& pos, float xm, Direction dir) { - if(player->got_power == Player::FIRE_POWER) - { + // TODO remove this function and move these checks elsewhere... + static const size_t MAX_FIRE_BULLETS = 2; + static const size_t MAX_ICE_BULLETS = 1; + + Bullet* new_bullet = 0; + if(player_status.bonus == FIRE_BONUS) { if(bullets.size() > MAX_FIRE_BULLETS-1) return false; - } - else if(player->got_power == Player::ICE_POWER) - { + new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET); + } else if(player_status.bonus == ICE_BONUS) { if(bullets.size() > MAX_ICE_BULLETS-1) return false; - } - - Bullet* new_bullet = 0; - if(player->got_power == Player::FIRE_POWER) - new_bullet = new Bullet(pos, xm, dir, FIRE_BULLET); - else if(player->got_power == Player::ICE_POWER) new_bullet = new Bullet(pos, xm, dir, ICE_BULLET); - else - throw std::runtime_error("wrong bullet type."); + } else { + return false; + } add_object(new_bullet); - - play_sound(sounds[SND_SHOOT], SOUND_CENTER_SPEAKER); - + + sound_manager->play_sound("shoot"); + return true; } -/* Break a brick: */ bool -Sector::trybreakbrick(const Vector& pos, bool small) +Sector::add_smoke_cloud(const Vector& pos) { - Tile* tile = solids->get_tile_at(pos); - if (tile->attributes & Tile::BRICK) - { - if (tile->data > 0) - { - /* Get a distro from it: */ - add_bouncy_distro( - Vector(((int)(pos.x + 1) / 32) * 32, (int)(pos.y / 32) * 32)); - - // TODO: don't handle this in a global way but per-tile... - if (!counting_distros) - { - counting_distros = true; - distro_counter = 5; - } - else - { - distro_counter--; - } - - if (distro_counter <= 0) - { - counting_distros = false; - solids->change_at(pos, tile->next_tile); - } - - play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER); - player_status.score = player_status.score + SCORE_DISTRO; - player_status.distros++; - return true; - } - else if (!small) - { - /* Get rid of it: */ - solids->change_at(pos, tile->next_tile); - - /* Replace it with broken bits: */ - add_broken_brick(Vector( - ((int)(pos.x + 1) / 32) * 32, - (int)(pos.y / 32) * 32), tile); - - /* Get some score: */ - play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER); - player_status.score = player_status.score + SCORE_BRICK; - - return true; - } - } - - return false; -} - -/* Empty a box: */ -void -Sector::tryemptybox(const Vector& pos, Direction col_side) -{ - Tile* tile = solids->get_tile_at(pos); - if (!(tile->attributes & Tile::FULLBOX)) - return; - - // according to the collision side, set the upgrade direction - if(col_side == LEFT) - col_side = RIGHT; - else - col_side = LEFT; - - int posx = ((int)(pos.x+1) / 32) * 32; - int posy = (int)(pos.y/32) * 32 - 32; - switch(tile->data) - { - case 1: // Box with a distro! - add_bouncy_distro(Vector(posx, posy)); - play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER); - player_status.score = player_status.score + SCORE_DISTRO; - player_status.distros++; - break; - - case 2: // Add a fire flower upgrade! - if (player->size == SMALL) /* Tux is small, add mints! */ - add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP); - else /* Tux is big, add a fireflower: */ - add_upgrade(Vector(posx, posy), col_side, UPGRADE_FIREFLOWER); - play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER); - break; - - case 5: // Add an ice flower upgrade! - if (player->size == SMALL) /* Tux is small, add mints! */ - add_upgrade(Vector(posx, posy), col_side, UPGRADE_GROWUP); - else /* Tux is big, add an iceflower: */ - add_upgrade(Vector(posx, posy), col_side, UPGRADE_ICEFLOWER); - play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER); - break; - - case 3: // Add a golden herring - add_upgrade(Vector(posx, posy), col_side, UPGRADE_HERRING); - break; - - case 4: // Add a 1up extra - add_upgrade(Vector(posx, posy), col_side, UPGRADE_1UP); - break; - default: - break; - } - - /* Empty the box: */ - solids->change_at(pos, tile->next_tile); + add_object(new SmokeCloud(pos)); + return true; } - -/* Try to grab a distro: */ -void -Sector::trygrabdistro(const Vector& pos, int bounciness) -{ - Tile* tile = solids->get_tile_at(pos); - if (!(tile->attributes & Tile::COIN)) - return; - solids->change_at(pos, tile->next_tile); - play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER); - - if (bounciness == BOUNCE) - { - add_bouncy_distro(Vector(((int)(pos.x + 1) / 32) * 32, - (int)(pos.y / 32) * 32)); - } - - player_status.score = player_status.score + SCORE_DISTRO; - player_status.distros++; -} - -/* Try to bump a bad guy from below: */ void -Sector::trybumpbadguy(const Vector& pos) +Sector::add_floating_text(const Vector& pos, const std::string& text) { - // Bad guys: - for (BadGuys::iterator i = badguys.begin(); i != badguys.end(); ++i) - { - if ((*i)->base.x >= pos.x - 32 && (*i)->base.x <= pos.x + 32 && - (*i)->base.y >= pos.y - 16 && (*i)->base.y <= pos.y + 16) - { - (*i)->collision(player, CO_PLAYER, COLLISION_BUMP); - } - } - - // Upgrades: - for (unsigned int i = 0; i < upgrades.size(); i++) - { - if (upgrades[i]->base.height == 32 && - upgrades[i]->base.x >= pos.x - 32 && upgrades[i]->base.x <= pos.x + 32 && - upgrades[i]->base.y >= pos.y - 16 && upgrades[i]->base.y <= pos.y + 16) - { - upgrades[i]->collision(player, CO_PLAYER, COLLISION_BUMP); - } - } + add_object(new FloatingText(pos, text)); } void Sector::load_music() { - char* song_path; - char* song_subtitle; - - level_song = music_manager->load_music(datadir + "/music/" + song_title); - - song_path = (char *) malloc(sizeof(char) * datadir.length() + - strlen(song_title.c_str()) + 8 + 5); - song_subtitle = strdup(song_title.c_str()); - strcpy(strstr(song_subtitle, "."), "\0"); - sprintf(song_path, "%s/music/%s-fast%s", datadir.c_str(), - song_subtitle, strstr(song_title.c_str(), ".")); - if(!music_manager->exists_music(song_path)) { - level_song_fast = level_song; - } else { - level_song_fast = music_manager->load_music(song_path); - } - free(song_subtitle); - free(song_path); + level_song = sound_manager->load_music( + get_resource_filename("/music/" + song_title)); } void -Sector::play_music(int type) +Sector::play_music(MusicType type) { currentmusic = type; switch(currentmusic) { - case HURRYUP_MUSIC: - music_manager->play_music(level_song_fast); - break; case LEVEL_MUSIC: - music_manager->play_music(level_song); + sound_manager->play_music(level_song); break; case HERRING_MUSIC: - music_manager->play_music(herring_song); + sound_manager->play_music(herring_song); break; default: - music_manager->halt_music(); + sound_manager->halt_music(); break; } } -int +MusicType Sector::get_music_type() { return currentmusic; } + +int +Sector::get_total_badguys() +{ + int total_badguys = 0; + for(GameObjects::iterator i = gameobjects.begin(); + i != gameobjects.end(); ++i) { + BadGuy* badguy = dynamic_cast (*i); + if(badguy) + total_badguys++; + } + + return total_badguys; +} + +bool +Sector::inside(const Rect& rect) const +{ + if(rect.p1.x > solids->get_width() * 32 + || rect.p1.y > solids->get_height() * 32 + || rect.p2.x < 0 || rect.p2.y < 0) + return false; + + return true; +}