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/playing.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/systems/playing.cpp (limited to 'src/systems/playing.cpp') 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; + } +} -- cgit 1.4.1