From 0e0389752a0912614737e5c059b5cd4719ef9cf2 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 10 Mar 2015 19:42:04 -0400 Subject: Const correctness! Also created savefile and refactored collisions a bit. --- src/components.cpp | 173 +++++++++++++++++++++++++++++------------------------ src/components.h | 33 +++++----- src/entity.cpp | 2 +- src/entity.h | 4 +- src/game.cpp | 19 +++++- src/game.h | 10 +++- src/map.cpp | 12 ++-- src/map.h | 16 ++--- 8 files changed, 156 insertions(+), 113 deletions(-) diff --git a/src/components.cpp b/src/components.cpp index 369bb9e..4d62a87 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -68,7 +68,7 @@ void UserMovementComponent::input(Game& game, Entity& entity, int key, int actio // Physics component -void PhysicsBodyComponent::receive(Game&, Entity&, Message& msg) +void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) { if (msg.type == Message::Type::walkLeft) { @@ -142,7 +142,7 @@ void PlayerSpriteComponent::render(Game&, Entity& entity, Texture& buffer) buffer.blit(sprite, src_rect, dst_rect); } -void PlayerSpriteComponent::receive(Game&, Entity&, Message& msg) +void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) { if (msg.type == Message::Type::walkLeft) { @@ -178,7 +178,7 @@ PlayerPhysicsComponent::PlayerPhysicsComponent() accel.second = jump_gravity_short; } -void PlayerPhysicsComponent::receive(Game&, Entity& entity, Message& msg) +void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) { if (msg.type == Message::Type::walkLeft) { @@ -264,7 +264,7 @@ void PlayerPhysicsComponent::tick(Game& game, Entity& entity) // Map rendering -MapRenderComponent::MapRenderComponent(Map& map) +MapRenderComponent::MapRenderComponent(const Map& map) { screen.fill(screen.entirety(), 0, 0, 0); @@ -302,13 +302,13 @@ void MapRenderComponent::render(Game&, Entity&, Texture& buffer) // Map collision -MapCollisionComponent::MapCollisionComponent(Map& map) +MapCollisionComponent::MapCollisionComponent(const Map& map) { leftMap = map.getLeftMap(); rightMap = map.getRightMap(); - add_collision(-6, 0, GAME_WIDTH, left, (map.getLeftMap() == nullptr) ? 1 : 2); - add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, (map.getRightMap() == nullptr) ? 3 : 2); + addCollision(-6, 0, GAME_WIDTH, Direction::left, (leftMap == nullptr) ? 1 : 2); + addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (rightMap == nullptr) ? 3 : 2); for (int i=0; i 0) && (!((tile >= 5) && (tile <= 7)))) { - add_collision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, right, 0); - add_collision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, left, 0); - add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); - add_collision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, up, 0); + 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); } else if ((tile >= 5) && (tile <= 7)) { - add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 3); + addCollision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, Direction::down, 4); } } } -void MapCollisionComponent::add_collision(int axis, int lower, int upper, direction_t dir, int type) +void MapCollisionComponent::addCollision(int axis, int lower, int upper, Direction dir, int type) { - std::list::iterator it; + std::list::iterator it; switch (dir) { - case up: + case Direction::up: it = up_collisions.begin(); for (; it!=up_collisions.end(); it++) { @@ -345,7 +345,7 @@ void MapCollisionComponent::add_collision(int axis, int lower, int upper, direct up_collisions.insert(it, {axis, lower, upper, type}); break; - case down: + case Direction::down: it = down_collisions.begin(); for (; it!=down_collisions.end(); it++) { @@ -355,7 +355,7 @@ void MapCollisionComponent::add_collision(int axis, int lower, int upper, direct down_collisions.insert(it, {axis, lower, upper, type}); break; - case left: + case Direction::left: it = left_collisions.begin(); for (; it!=left_collisions.end(); it++) { @@ -365,7 +365,7 @@ void MapCollisionComponent::add_collision(int axis, int lower, int upper, direct left_collisions.insert(it, {axis, lower, upper, type}); break; - case right: + case Direction::right: it = right_collisions.begin(); for (; it!=right_collisions.end(); it++) { @@ -395,20 +395,7 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide if ((fixed_oy+collider.size.second > collision.lower) && (fixed_oy < collision.upper)) { // We have a collision! - if (collision.type == 0) - { - collider.position.first = collision.axis; - - Message msg(Message::Type::stopMovingHorizontally); - collider.send(game, msg); - } else if (collision.type == 1) - { - collider.position.first = GAME_WIDTH-collider.size.first/2; - } else if (collision.type == 2) - { - collider.position.first = GAME_WIDTH-collider.size.first/2; - game.loadMap(*leftMap); - } + processCollision(game, collider, collision, Direction::left); break; } @@ -423,26 +410,7 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide if ((fixed_oy+collider.size.second > collision.lower) && (fixed_oy < collision.upper)) { // We have a collision! - if (collision.type == 0) - { - collider.position.first = collision.axis - collider.size.first; - - Message msg(Message::Type::stopMovingHorizontally); - collider.send(game, msg); - } else if (collision.type == 1) - { - collider.position.first = -collider.size.first/2; - } else if (collision.type == 2) - { - collider.position.first = -collider.size.first/2; - game.loadMap(*rightMap); - } else if (collision.type == 3) - { - collider.position.first = collision.axis - collider.size.first; - - Message msg(Message::Type::walkLeft); - collider.send(game, msg); - } + processCollision(game, collider, collision, Direction::right); break; } @@ -462,16 +430,7 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide if ((fixed_x+collider.size.first > collision.lower) && (fixed_x < collision.upper)) { // We have a collision! - if (collision.type == 0) - { - collider.position.second = collision.axis; - - Message msg(Message::Type::stopMovingVertically); - collider.send(game, msg); - } else if (collision.type == 1) - { - collider.position.second = GAME_HEIGHT-collider.size.second/2-1; - } + processCollision(game, collider, collision, Direction::up); break; } @@ -486,25 +445,83 @@ void MapCollisionComponent::detectCollision(Game& game, Entity&, Entity& collide if ((fixed_x+collider.size.first > collision.lower) && (fixed_x < collision.upper)) { // We have a collision! - if (collision.type == 0) - { - collider.position.second = collision.axis - collider.size.second; - - Message msg(Message::Type::stopMovingVertically); - collider.send(game, msg); - } else if (collision.type == 1) - { - collider.position.second = -collider.size.second/2; - } else if (collision.type == 3) - { - Message msg(Message::Type::drop); - msg.dropAxis = collision.axis; - - collider.send(game, msg); - } + processCollision(game, collider, collision, Direction::down); break; } } } } + +void MapCollisionComponent::processCollision(Game& game, Entity& collider, Collision collision, Direction dir) +{ + if (collision.type == 0) + { + if (dir == Direction::left) + { + collider.position.first = collision.axis; + + Message msg(Message::Type::stopMovingHorizontally); + collider.send(game, msg); + } else if (dir == Direction::right) + { + collider.position.first = collision.axis - collider.size.first; + + Message msg(Message::Type::stopMovingHorizontally); + collider.send(game, msg); + } else if (dir == Direction::up) + { + collider.position.second = collision.axis; + + Message msg(Message::Type::stopMovingVertically); + collider.send(game, msg); + } else if (dir == Direction::down) + { + collider.position.second = collision.axis - collider.size.second; + + Message msg(Message::Type::stopMovingVertically); + collider.send(game, msg); + } + } else if (collision.type == 1) + { + if (dir == Direction::left) + { + collider.position.first = GAME_WIDTH-collider.size.first/2; + } else if (dir == Direction::right) + { + collider.position.first = -collider.size.first/2; + } else if (dir == Direction::up) + { + collider.position.second = GAME_HEIGHT-collider.size.second/2-1; + } else if (dir == Direction::down) + { + collider.position.second = -collider.size.second/2; + } + } else if (collision.type == 2) + { + if (dir == Direction::left) + { + collider.position.first = GAME_WIDTH-collider.size.first/2; + game.loadMap(*leftMap); + } else if (dir == Direction::right) + { + collider.position.first = -collider.size.first/2; + game.loadMap(*rightMap); + } + } else if (collision.type == 3) + { + if (dir == Direction::right) + { + collider.position.first = collision.axis - collider.size.first; + + Message msg(Message::Type::walkLeft); + collider.send(game, msg); + } + } else if (collision.type == 4) + { + Message msg(Message::Type::drop); + msg.dropAxis = collision.axis; + + collider.send(game, msg); + } +} diff --git a/src/components.h b/src/components.h index f9b6e1e..985025c 100644 --- a/src/components.h +++ b/src/components.h @@ -18,11 +18,11 @@ class UserMovementComponent : public Component { class PhysicsBodyComponent : public Component { public: - void receive(Game& game, Entity& entity, Message& msg); + void receive(Game& game, Entity& entity, const Message& msg); void tick(Game& game, Entity& entity); void detectCollision(Game& game, Entity& entity, Entity& collider, std::pair old_position); - private: + private: std::pair velocity; std::pair accel; }; @@ -30,7 +30,7 @@ class PhysicsBodyComponent : public Component { class PlayerSpriteComponent : public Component { public: void render(Game& game, Entity& entity, Texture& buffer); - void receive(Game& game, Entity& entity, Message& msg); + void receive(Game& game, Entity& entity, const Message& msg); void tick(Game& game, Entity& entity); private: @@ -44,7 +44,7 @@ class PlayerPhysicsComponent : public Component { public: PlayerPhysicsComponent(); void tick(Game& game, Entity& entity); - void receive(Game& game, Entity& entity, Message& msg); + void receive(Game& game, Entity& entity, const Message& msg); private: std::pair velocity; @@ -58,7 +58,7 @@ class PlayerPhysicsComponent : public Component { class MapRenderComponent : public Component { public: - MapRenderComponent(Map& map); + MapRenderComponent(const Map& map); void render(Game& game, Entity& entity, Texture& buffer); private: @@ -67,29 +67,30 @@ class MapRenderComponent : public Component { class MapCollisionComponent : public Component { public: - MapCollisionComponent(Map& map); + MapCollisionComponent(const Map& map); void detectCollision(Game& game, Entity& entity, Entity& collider, std::pair old_position); private: - enum direction_t { + enum class Direction { up, left, down, right }; - typedef struct { + struct Collision { int axis; int lower; int upper; int type; - } collision_t; + }; - void add_collision(int axis, int lower, int upper, direction_t dir, int type); + void addCollision(int axis, int lower, int upper, Direction dir, int type); + void processCollision(Game& game, Entity& collider, Collision collision, Direction dir); - std::list left_collisions; - std::list right_collisions; - std::list up_collisions; - std::list down_collisions; - Map* leftMap; - Map* rightMap; + std::list left_collisions; + std::list right_collisions; + std::list up_collisions; + std::list down_collisions; + const Map* leftMap; + const Map* rightMap; }; #endif diff --git a/src/entity.cpp b/src/entity.cpp index 950969e..38ddffe 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -5,7 +5,7 @@ void Entity::addComponent(std::shared_ptr c) components.push_back(c); } -void Entity::send(Game& game, Message& msg) +void Entity::send(Game& game, const Message& msg) { for (auto component : components) { diff --git a/src/entity.h b/src/entity.h index 803a9b8..266fe11 100644 --- a/src/entity.h +++ b/src/entity.h @@ -34,7 +34,7 @@ class Message { class Entity { public: void addComponent(std::shared_ptr c); - void send(Game& game, Message& msg); + void send(Game& game, const Message& msg); void tick(Game& game); void input(Game& game, int key, int action); void render(Game& game, Texture& buffer); @@ -49,7 +49,7 @@ class Entity { class Component { public: - virtual void receive(Game&, Entity&, Message&) {} + virtual void receive(Game&, Entity&, const Message&) {} virtual void render(Game&, Entity&, Texture&) {} virtual void tick(Game&, Entity&) {} virtual void input(Game&, Entity&, int, int) {} diff --git a/src/game.cpp b/src/game.cpp index cbbae06..e392923 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -20,6 +20,8 @@ Game::Game() auto player_anim = std::make_shared(); player->addComponent(player_anim); + save = {&m, player->position}; + loadMap(m); } @@ -87,7 +89,7 @@ void Game::execute(GLFWwindow* window) } } -void Game::loadMap(Map& map) +void Game::loadMap(const Map& map) { auto mapEn = std::make_shared(); @@ -111,3 +113,18 @@ void Game::detectCollision(Entity& collider, std::pair old_posit entity->detectCollision(*this, collider, old_position); } } + +void Game::saveGame(const Map& map, std::pair position) +{ + save = {&map, position}; +} + +void Game::loadGame(const Map& curMap) +{ + if (&curMap != save.map) + { + loadMap(*(save.map)); + } + + player->position = save.position; +} diff --git a/src/game.h b/src/game.h index c419c5d..69b8df7 100644 --- a/src/game.h +++ b/src/game.h @@ -17,12 +17,19 @@ const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT; const int FRAMES_PER_SECOND = 60; const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; +struct Savefile { + const Map* map; + std::pair position; +}; + class Game { public: Game(); void execute(GLFWwindow* window); - void loadMap(Map& map); + 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); bool shouldQuit = false; private: @@ -34,6 +41,7 @@ class Game { std::shared_ptr player; Map m{"../maps/embarass.txt"}; Map m2{"../maps/second.txt"}; + Savefile save; }; #endif diff --git a/src/map.cpp b/src/map.cpp index 87080e8..3976b63 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -67,32 +67,32 @@ void swap(Map& first, Map& second) std::swap(first.m_rightMap, second.m_rightMap); } -const int* Map::mapdata() +const int* Map::mapdata() const { return m_mapdata; } -const char* Map::title() +const char* Map::title() const { return m_title; } -Map* Map::getLeftMap() +const Map* Map::getLeftMap() const { return m_leftMap; } -Map* Map::getRightMap() +const Map* Map::getRightMap() const { return m_rightMap; } -void Map::setLeftMap(Map* m) +void Map::setLeftMap(const Map* m) { m_leftMap = m; } -void Map::setRightMap(Map* m) +void Map::setRightMap(const Map* m) { m_rightMap = m; } diff --git a/src/map.h b/src/map.h index e3d1802..071b6f2 100644 --- a/src/map.h +++ b/src/map.h @@ -10,19 +10,19 @@ class Map { Map& operator= (Map other); friend void swap(Map& first, Map& second); - const int* mapdata(); - const char* title(); - Map* getLeftMap(); - Map* getRightMap(); - void setLeftMap(Map* m); - void setRightMap(Map* m); + const int* mapdata() const; + const char* title() const; + const Map* getLeftMap() const; + const Map* getRightMap() const; + void setLeftMap(const Map* m); + void setRightMap(const Map* m); private: Map(); int* m_mapdata; char* m_title; - Map* m_leftMap; - Map* m_rightMap; + const Map* m_leftMap = nullptr; + const Map* m_rightMap = nullptr; }; #endif -- cgit 1.4.1