From e16fb5be90c889c371cbb0ca2444735c2e12073c Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 18 Feb 2018 12:35:45 -0500 Subject: Implemented map adjacency This brings along with it the ability to move to different maps, for which the PlayingSystem and PlayableComponent were introduced. The PlayingSystem is a general overseer system that handles big picture stuff like initializing the player and changing maps. The PlayableComponent represents the player. While the ControllableComponent is also likely to always only be on the player entity, the two are distinct by separation of concerns. This also required a refactoring of how collisions are processed, because of a bug where the player can move to a new map when horizontal collisions are checked, and vertical collisions are skipped, causing the player to clip through the ground because the normal force was never handled. --- src/systems/mapping.cpp | 28 ++++ src/systems/playing.cpp | 98 +++++++++++++ src/systems/playing.h | 21 +++ src/systems/pondering.cpp | 347 +++++++++++++++++++++++++++++++--------------- src/systems/pondering.h | 11 -- 5 files changed, 381 insertions(+), 124 deletions(-) create mode 100644 src/systems/playing.cpp create mode 100644 src/systems/playing.h (limited to 'src/systems') diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index 120a27a..05167c1 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp @@ -93,6 +93,34 @@ void MappingSystem::loadMap(size_t mapId) const Map& map = game_.getWorld().getMap(mappable.getMapId()); + addBoundary( + mappable.getLeftBoundaries(), + -WALL_GAP, + 0, + MAP_HEIGHT * TILE_HEIGHT, + MappableComponent::Boundary::Type::adjacency); + + addBoundary( + mappable.getRightBoundaries(), + GAME_WIDTH + WALL_GAP, + 0, + MAP_HEIGHT * TILE_HEIGHT, + MappableComponent::Boundary::Type::adjacency); + + addBoundary( + mappable.getUpBoundaries(), + -WALL_GAP, + 0, + GAME_WIDTH, + MappableComponent::Boundary::Type::adjacency); + + addBoundary( + mappable.getDownBoundaries(), + MAP_HEIGHT * TILE_HEIGHT + WALL_GAP, + 0, + GAME_WIDTH, + MappableComponent::Boundary::Type::adjacency); + for (size_t i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++) { size_t x = i % MAP_WIDTH; diff --git a/src/systems/playing.cpp b/src/systems/playing.cpp new file mode 100644 index 0000000..2c6a419 --- /dev/null +++ b/src/systems/playing.cpp @@ -0,0 +1,98 @@ +#include "playing.h" +#include "game.h" +#include "components/transformable.h" +#include "components/animatable.h" +#include "components/playable.h" +#include "components/controllable.h" +#include "components/orientable.h" +#include "systems/mapping.h" +#include "systems/pondering.h" +#include "animation.h" + +void PlayingSystem::tick(double) +{ + // Check if we need to change the map + auto players = game_.getEntityManager().getEntitiesWithComponents< + PlayableComponent>(); + + for (id_type player : players) + { + auto& playable = game_.getEntityManager(). + getComponent(player); + + if (playable.changingMap) + { + // Change the map! + auto entities = game_.getEntityManager().getEntities(); + + for (id_type entity : entities) + { + if (entity != player) + { + game_.getEntityManager().deleteEntity(entity); + } + } + + game_.getSystemManager().getSystem(). + loadMap(playable.newMapId); + + auto& transformable = game_.getEntityManager(). + getComponent(player); + + transformable.setX(playable.newMapX); + transformable.setY(playable.newMapY); + + playable.changingMap = false; + + break; + } + } +} + +void PlayingSystem::initPlayer() +{ + id_type player = game_.getEntityManager().emplaceEntity(); + + AnimationSet playerGraphics {"res/Starla.png", 10, 12, 6}; + playerGraphics.emplaceAnimation("stillLeft", 3, 1, 1); + playerGraphics.emplaceAnimation("stillRight", 0, 1, 1); + playerGraphics.emplaceAnimation("walkingLeft", 4, 2, 10); + playerGraphics.emplaceAnimation("walkingRight", 1, 2, 10); + + game_.getEntityManager().emplaceComponent( + player, + std::move(playerGraphics), + "stillLeft"); + + game_.getEntityManager().emplaceComponent( + player, + 203, 44, 10, 12); + + game_.getSystemManager().getSystem().initializeBody( + player, + PonderableComponent::Type::freefalling); + + game_.getEntityManager().emplaceComponent(player); + game_.getEntityManager().emplaceComponent(player); + game_.getEntityManager().emplaceComponent(player); +} + +void PlayingSystem::changeMap( + size_t mapId, + double x, + double y) +{ + auto players = game_.getEntityManager().getEntitiesWithComponents< + PlayableComponent>(); + + for (id_type player : players) + { + auto& playable = game_.getEntityManager(). + getComponent(player); + + playable.changingMap = true; + playable.newMapId = mapId; + playable.newMapX = x; + playable.newMapY = y; + } +} diff --git a/src/systems/playing.h b/src/systems/playing.h new file mode 100644 index 0000000..c98a464 --- /dev/null +++ b/src/systems/playing.h @@ -0,0 +1,21 @@ +#ifndef PLAYING_H_70A54F7D +#define PLAYING_H_70A54F7D + +#include "system.h" + +class PlayingSystem : public System { +public: + + PlayingSystem(Game& game) : System(game) + { + } + + void tick(double dt); + + void initPlayer(); + + void changeMap(size_t mapId, double x, double y); + +}; + +#endif /* end of include guard: PLAYING_H_70A54F7D */ diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 4a165b1..2490dc9 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -1,11 +1,14 @@ #include "pondering.h" +#include #include "game.h" #include "components/ponderable.h" #include "components/transformable.h" #include "components/orientable.h" #include "components/mappable.h" #include "systems/orienting.h" +#include "systems/playing.h" #include "consts.h" +#include "collision.h" void PonderingSystem::tick(double dt) { @@ -42,6 +45,9 @@ void PonderingSystem::tick(double dt) bool oldGrounded = ponderable.isGrounded(); ponderable.setGrounded(false); + std::priority_queue collisions; + + // Find collisions for (id_type mapEntity : maps) { auto& mappable = game_.getEntityManager(). @@ -57,13 +63,13 @@ void PonderingSystem::tick(double dt) && (oldY < it->second.getUpper())) { // We have a collision! - processCollision( - entity, + collisions.emplace( + mapEntity, Direction::left, - newX, - newY, + it->second.getType(), it->first, - it->second.getType()); + it->second.getLower(), + it->second.getUpper()); } } } else if (newX > oldX) @@ -77,13 +83,13 @@ void PonderingSystem::tick(double dt) && (oldY < it->second.getUpper())) { // We have a collision! - processCollision( - entity, + collisions.emplace( + mapEntity, Direction::right, - newX, - newY, + it->second.getType(), it->first, - it->second.getType()); + it->second.getLower(), + it->second.getUpper()); } } } @@ -98,13 +104,13 @@ void PonderingSystem::tick(double dt) && (oldX < it->second.getUpper())) { // We have a collision! - processCollision( - entity, + collisions.emplace( + mapEntity, Direction::up, - newX, - newY, + it->second.getType(), it->first, - it->second.getType()); + it->second.getLower(), + it->second.getUpper()); } } } else if (newY > oldY) @@ -118,13 +124,221 @@ void PonderingSystem::tick(double dt) && (oldX < it->second.getUpper())) { // We have a collision! - processCollision( - entity, + collisions.emplace( + mapEntity, Direction::down, - newX, - newY, + it->second.getType(), it->first, - it->second.getType()); + it->second.getLower(), + it->second.getUpper()); + } + } + } + } + + // Process collisions in order of priority + while (!collisions.empty()) + { + Collision collision = collisions.top(); + collisions.pop(); + + // Make sure that they are still colliding + if (!collision.isColliding( + newX, + newY, + transformable.getW(), + transformable.getH())) + { + continue; + } + + bool touchedWall = false; + bool stopProcessing = false; + + switch (collision.getType()) + { + case Collision::Type::wall: + { + touchedWall = true; + + break; + } + + case Collision::Type::platform: + { + if (game_.getEntityManager(). + hasComponent(entity)) + { + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + if (orientable.getDropState() != + OrientableComponent::DropState::none) + { + orientable.setDropState(OrientableComponent::DropState::active); + } else { + touchedWall = true; + } + } else { + touchedWall = true; + } + + break; + } + + case Collision::Type::adjacency: + { + auto& mappable = game_.getEntityManager(). + getComponent(collision.getCollider()); + const Map& map = game_.getWorld().getMap(mappable.getMapId()); + auto& adj = [&] () -> const Map::Adjacent& { + switch (collision.getDirection()) + { + case Direction::left: return map.getLeftAdjacent(); + case Direction::right: return map.getRightAdjacent(); + case Direction::up: return map.getUpAdjacent(); + case Direction::down: return map.getDownAdjacent(); + } + }(); + + switch (adj.getType()) + { + case Map::Adjacent::Type::wall: + { + touchedWall = true; + + break; + } + + case Map::Adjacent::Type::wrap: + { + switch (collision.getDirection()) + { + case Direction::left: + { + newX = GAME_WIDTH + WALL_GAP - transformable.getW(); + + break; + } + + case Direction::right: + { + newX = -WALL_GAP; + + break; + } + + case Direction::up: + { + newY = MAP_HEIGHT * TILE_HEIGHT + WALL_GAP - + transformable.getH(); + + break; + } + + case Direction::down: + { + newY = -WALL_GAP; + + break; + } + } + } + + case Map::Adjacent::Type::warp: + { + double warpX = newX; + double warpY = newY; + + switch (collision.getDirection()) + { + case Direction::left: + { + warpX = GAME_WIDTH + WALL_GAP - transformable.getW(); + + break; + } + + case Direction::right: + { + warpX = -WALL_GAP; + + break; + } + + case Direction::up: + { + warpY = MAP_HEIGHT * TILE_HEIGHT - transformable.getH(); + + break; + } + + case Direction::down: + { + warpY = -WALL_GAP; + + break; + } + } + + game_.getSystemManager().getSystem(). + changeMap(adj.getMapId(), warpX, warpY); + + stopProcessing = true; + + break; + } + } + } + + default: + { + // Not yet implemented. + + break; + } + } + + if (stopProcessing) + { + break; + } + + if (touchedWall) + { + switch (collision.getDirection()) + { + case Direction::left: + { + newX = collision.getAxis(); + ponderable.setVelocityX(0.0); + + break; + } + + case Direction::right: + { + newX = collision.getAxis() - transformable.getW(); + ponderable.setVelocityX(0.0); + + break; + } + + case Direction::up: + { + newY = collision.getAxis(); + ponderable.setVelocityY(0.0); + + break; + } + + case Direction::down: + { + newY = collision.getAxis() - transformable.getH(); + ponderable.setVelocityY(0.0); + ponderable.setGrounded(true); + + break; } } } @@ -173,96 +387,3 @@ void PonderingSystem::initializeBody( ponderable.setAccelY(NORMAL_GRAVITY); } } - -void PonderingSystem::processCollision( - id_type entity, - Direction dir, - double& newX, - double& newY, - int axis, - MappableComponent::Boundary::Type type) -{ - auto& ponderable = game_.getEntityManager(). - getComponent(entity); - - auto& transformable = game_.getEntityManager(). - getComponent(entity); - - bool touchedGround = false; - - switch (type) - { - case MappableComponent::Boundary::Type::wall: - { - switch (dir) - { - case Direction::left: - { - newX = axis; - ponderable.setVelocityX(0.0); - - break; - } - - case Direction::right: - { - newX = axis - transformable.getW(); - ponderable.setVelocityX(0.0); - - break; - } - - case Direction::up: - { - newY = axis; - ponderable.setVelocityY(0.0); - - break; - } - - case Direction::down: - { - touchedGround = true; - - break; - } - } - - break; - } - - case MappableComponent::Boundary::Type::platform: - { - if (game_.getEntityManager().hasComponent(entity)) - { - auto& orientable = game_.getEntityManager(). - getComponent(entity); - - if (orientable.getDropState() != OrientableComponent::DropState::none) - { - orientable.setDropState(OrientableComponent::DropState::active); - } else { - touchedGround = true; - } - } else { - touchedGround = true; - } - - break; - } - - default: - { - // Not yet implemented. - - break; - } - } - - if (touchedGround) - { - newY = axis - transformable.getH(); - ponderable.setVelocityY(0.0); - ponderable.setGrounded(true); - } -} diff --git a/src/systems/pondering.h b/src/systems/pondering.h index a16622b..d70525b 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h @@ -2,7 +2,6 @@ #define PONDERING_H_F2530E0E #include "system.h" -#include "components/mappable.h" #include "components/ponderable.h" #include "direction.h" @@ -17,16 +16,6 @@ public: void initializeBody(id_type entity, PonderableComponent::Type type); -private: - - void processCollision( - id_type entity, - Direction dir, - double& newX, - double& newY, - int axis, - MappableComponent::Boundary::Type type); - }; #endif /* end of include guard: PONDERING_H_F2530E0E */ -- cgit 1.4.1