From 47d9d7884c57c2c14dd363b4ccb0df1dcbb5375e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 12 Mar 2015 22:46:32 -0400 Subject: Fixed bug that would prevent player from continuing to move after dying, and also refactored collisions and dying a bit --- src/components.cpp | 52 ++++++++++++++++++++++++++++------------------------ src/components.h | 13 +++++++++++-- src/entity.h | 3 ++- src/game.cpp | 45 ++++++++++++++++++++------------------------- src/game.h | 2 +- 5 files changed, 62 insertions(+), 53 deletions(-) diff --git a/src/components.cpp b/src/components.cpp index 5925d14..ad0f501 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -157,6 +157,11 @@ void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer) void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) { + if (msg.type == Message::Type::stopDying) + { + dying = false; + } + if (dying) { return; @@ -216,7 +221,10 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) velocity.second = 0.0; } else if (msg.type == Message::Type::jump) { - playSound("../res/Randomize87.wav", 0.25); + if (!frozen) + { + playSound("../res/Randomize87.wav", 0.25); + } velocity.second = jump_velocity; accel.second = jump_gravity; @@ -241,6 +249,9 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) } else if (msg.type == Message::Type::die) { frozen = true; + } else if (msg.type == Message::Type::stopDying) + { + frozen = false; } } @@ -331,8 +342,8 @@ void MapRenderComponent::render(Game&, Entity&, Texture& buffer) MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) { - addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? 1 : 2); - addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? 3 : 2); + addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? Collision::Type::wrap : Collision::Type::teleport); + addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? Collision::Type::reverse : Collision::Type::teleport); for (int i=0; i 0) && (tile < 28) && (!((tile >= 5) && (tile <= 7)))) { - addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, 0); - addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, 0); - addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 0); - addCollision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::up, 0); + addCollision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::right, Collision::Type::wall); + addCollision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, Direction::left, Collision::Type::wall); + addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::wall); + addCollision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::up, Collision::Type::wall); } else if ((tile >= 5) && (tile <= 7)) { - addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 4); + addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::platform); } else if (tile == 42) { - addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 5); + addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, Collision::Type::danger); } } } -void MapCollisionComponent::addCollision(int axis, int lower, int upper, Direction dir, int type) +void MapCollisionComponent::addCollision(int axis, int lower, int upper, Direction dir, Collision::Type type) { std::list::iterator it; @@ -498,7 +509,7 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Collision collision, Direction dir) { - if (collision.type == 0) + if (collision.type == Collision::Type::wall) { if (dir == Direction::left) { @@ -525,7 +536,7 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli Message msg(Message::Type::stopMovingVertically); collider.send(game, msg); } - } else if (collision.type == 1) + } else if (collision.type == Collision::Type::wrap) { if (dir == Direction::left) { @@ -540,7 +551,7 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli { collider.position.second = -collider.size.second/2; } - } else if (collision.type == 2) + } else if (collision.type == Collision::Type::teleport) { if (dir == Direction::left) { @@ -553,7 +564,7 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli } return true; - } else if (collision.type == 3) + } else if (collision.type == Collision::Type::reverse) { if (dir == Direction::right) { @@ -562,22 +573,15 @@ bool MapCollisionComponent::processCollision(Game& game, Entity& collider, Colli Message msg(Message::Type::walkLeft); collider.send(game, msg); } - } else if (collision.type == 4) + } else if (collision.type == Collision::Type::platform) { Message msg(Message::Type::drop); msg.dropAxis = collision.axis; collider.send(game, msg); - } else if (collision.type == 5) + } else if (collision.type == Collision::Type::danger) { - Message msg(Message::Type::die); - collider.send(game, msg); - - playSound("../res/Hit_Hurt5.wav", 0.25); - - game.schedule(FRAMES_PER_SECOND * 0.75, [&] () { - game.loadGame(map); - }); + game.playerDie(collider, map); } return false; diff --git a/src/components.h b/src/components.h index f182590..f421529 100644 --- a/src/components.h +++ b/src/components.h @@ -77,13 +77,22 @@ class MapCollisionComponent : public Component { }; struct Collision { + enum class Type { + wall, + wrap, + teleport, + reverse, + platform, + danger + }; + int axis; int lower; int upper; - int type; + Type type; }; - void addCollision(int axis, int lower, int upper, Direction dir, int type); + void addCollision(int axis, int lower, int upper, Direction dir, Collision::Type type); bool processCollision(Game& game, Entity& collider, Collision collision, Direction dir); std::list left_collisions; diff --git a/src/entity.h b/src/entity.h index 772c98b..04772e9 100644 --- a/src/entity.h +++ b/src/entity.h @@ -22,7 +22,8 @@ class Message { drop, canDrop, cantDrop, - die + die, + stopDying }; Message(Type type) : type(type) {} diff --git a/src/game.cpp b/src/game.cpp index 50c01f5..b2de5e9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,6 +1,7 @@ #include "game.h" #include "renderer.h" #include "components.h" +#include "muxer.h" Game::Game() { @@ -132,33 +133,27 @@ void Game::saveGame(const Map& map, std::pair position) save = {&map, position}; } -void Game::loadGame(const Map& curMap) +void Game::schedule(int frames, std::function&& callback) { - if (&curMap == save.map) - { - entities.remove(player); - } - - player = std::make_shared(); - player->position = save.position; - player->size = std::make_pair(10.0,12.0); - - auto player_input = std::make_shared(); - player->addComponent(player_input); - - auto player_physics = std::make_shared(); - player->addComponent(player_physics); - - auto player_anim = std::make_shared(); - player->addComponent(player_anim); - - if (&curMap != save.map) - { - loadMap(*(save.map)); - } + scheduled.emplace_front(frames, callback); } -void Game::schedule(int frames, std::function&& callback) +void Game::playerDie(Entity& player, const Map& curMap) { - scheduled.emplace_front(frames, callback); + Message msg(Message::Type::die); + player.send(*this, msg); + + playSound("../res/Hit_Hurt5.wav", 0.25); + + schedule(FRAMES_PER_SECOND * 0.75, [&] () { + if (&curMap != save.map) + { + loadMap(*(save.map)); + } + + player.position = save.position; + + Message msg2(Message::Type::stopDying); + player.send(*this, msg2); + }); } diff --git a/src/game.h b/src/game.h index ca6c00b..4f2f3df 100644 --- a/src/game.h +++ b/src/game.h @@ -30,8 +30,8 @@ class Game { void loadMap(const Map& map); void detectCollision(Entity& collider, std::pair old_position); void saveGame(const Map& map, std::pair position); - void loadGame(const Map& curMap); void schedule(int frames, std::function&& callback); + void playerDie(Entity& player, const Map& curMap); private: friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); -- cgit 1.4.1