From 5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 12 Feb 2018 16:39:49 -0500 Subject: Abstracted behavior related to "orientable" entities A lot of the stuff that ControllingSystem did to control the player character was moved into the new OrientingSystem. This is so that the player, or any player-like entities, can also be controlled by AI, with the underlying behavior being delegated in the same way as if the player were being controlled by the user. Fixed the issue where, if the player were blocked while moving horizontally, they would remain blocked even if vertical movement were to remove the collision. Fixed cases of the player animating incorrectly after performing certain movements. --- CMakeLists.txt | 1 + src/components/droppable.h | 24 ----- src/components/orientable.h | 45 +++++++++ src/components/ponderable.h | 17 +--- src/consts.h | 10 +- src/game.cpp | 4 +- src/systems/controlling.cpp | 129 +++--------------------- src/systems/controlling.h | 8 -- src/systems/mapping.cpp | 2 - src/systems/orienting.cpp | 236 ++++++++++++++++++++++++++++++++++++++++++++ src/systems/orienting.h | 35 +++++++ src/systems/pondering.cpp | 89 +++++++++-------- 12 files changed, 393 insertions(+), 207 deletions(-) delete mode 100644 src/components/droppable.h create mode 100644 src/systems/orienting.cpp create mode 100644 src/systems/orienting.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f918156..39b1bbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ add_executable(Aromatherapy src/systems/pondering.cpp src/systems/animating.cpp src/systems/mapping.cpp + src/systems/orienting.cpp ) target_link_libraries(Aromatherapy ${ALL_LIBS}) install(TARGETS Aromatherapy RUNTIME DESTINATION ${BIN_DIR}) diff --git a/src/components/droppable.h b/src/components/droppable.h deleted file mode 100644 index 722c139..0000000 --- a/src/components/droppable.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef DROPPABLE_H_5DB254EF -#define DROPPABLE_H_5DB254EF - -#include "component.h" - -class DroppableComponent : public Component { -public: - - inline bool isDroppable() const - { - return droppable_; - } - - inline void setDroppable(bool can) - { - droppable_ = can; - } - -private: - - bool droppable_ = false; -}; - -#endif /* end of include guard: DROPPABLE_H_5DB254EF */ diff --git a/src/components/orientable.h b/src/components/orientable.h index 8f56912..e356b78 100644 --- a/src/components/orientable.h +++ b/src/components/orientable.h @@ -6,6 +6,18 @@ class OrientableComponent : public Component { public: + enum class WalkState { + still, + left, + right + }; + + enum class DropState { + none, + ready, + active + }; + inline bool isFacingRight() const { return facingRight_; @@ -16,9 +28,42 @@ public: facingRight_ = v; } + inline WalkState getWalkState() const + { + return walkState_; + } + + inline void setWalkState(WalkState v) + { + walkState_ = v; + } + + inline bool isJumping() const + { + return jumping_; + } + + inline void setJumping(bool v) + { + jumping_ = v; + } + + inline DropState getDropState() const + { + return dropState_; + } + + inline void setDropState(DropState v) + { + dropState_ = v; + } + private: bool facingRight_ = false; + WalkState walkState_ = WalkState::still; + bool jumping_ = false; + DropState dropState_ = DropState::none; }; #endif /* end of include guard: ORIENTABLE_H_EDB6C4A1 */ diff --git a/src/components/ponderable.h b/src/components/ponderable.h index ac759b6..e21cbab 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -11,13 +11,6 @@ public: freefalling }; - enum class State { - grounded, - jumping, - falling, - dropping - }; - PonderableComponent(Type type) : type_(type) { } @@ -67,14 +60,14 @@ public: accelY_ = v; } - inline State getState() const + inline bool isGrounded() const { - return state_; + return grounded_; } - inline void setState(State arg) + inline void setGrounded(bool v) { - state_ = arg; + grounded_ = v; } private: @@ -84,7 +77,7 @@ private: double accelX_ = 0.0; double accelY_ = 0.0; Type type_ = Type::vacuumed; - State state_ = State::grounded; + bool grounded_ = false; }; #endif /* end of include guard: TANGIBLE_H_746DB3EE */ diff --git a/src/consts.h b/src/consts.h index a6c9985..581018d 100644 --- a/src/consts.h +++ b/src/consts.h @@ -14,7 +14,13 @@ const int FONT_COLS = 16; const int FRAMES_PER_SECOND = 60; const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; -#define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) -#define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) +#define CALC_VELOCITY(h, l) (-2 * (h) / (l)) +#define CALC_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) + +const double NORMAL_GRAVITY = CALC_GRAVITY(TILE_HEIGHT*3.5, 0.233); +const double JUMP_GRAVITY = CALC_GRAVITY(TILE_HEIGHT*4.5, 0.3); +const double JUMP_VELOCITY = CALC_VELOCITY(TILE_HEIGHT*4.5, 0.3); + +const double WALK_SPEED = 90; #endif diff --git a/src/game.cpp b/src/game.cpp index 7cbe7e0..39bb3f1 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,13 +2,13 @@ #include "components/animatable.h" #include "components/transformable.h" #include "components/controllable.h" -#include "components/droppable.h" #include "components/ponderable.h" #include "components/orientable.h" #include "systems/controlling.h" #include "systems/pondering.h" #include "systems/animating.h" #include "systems/mapping.h" +#include "systems/orienting.h" #include "animation.h" #include "renderer.h" #include "consts.h" @@ -33,6 +33,7 @@ Game::Game( world_("res/maps.xml") { systemManager_.emplaceSystem(*this); + systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); systemManager_.emplaceSystem(*this); @@ -58,7 +59,6 @@ Game::Game( player, PonderableComponent::Type::freefalling); - entityManager_.emplaceComponent(player); entityManager_.emplaceComponent(player); entityManager_.emplaceComponent(player); diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index fa09d11..e1609bd 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp @@ -1,14 +1,8 @@ #include "controlling.h" #include "game.h" #include "components/controllable.h" -#include "components/ponderable.h" -#include "components/animatable.h" -#include "components/droppable.h" #include "components/orientable.h" -#include "systems/animating.h" -#include "direction.h" -#include "muxer.h" -#include "consts.h" +#include "systems/orienting.h" void ControllingSystem::tick(double) { @@ -19,9 +13,6 @@ void ControllingSystem::tick(double) auto entities = game_.getEntityManager().getEntitiesWithComponents< ControllableComponent, - PonderableComponent, - AnimatableComponent, - DroppableComponent, OrientableComponent>(); for (auto entity : entities) @@ -29,6 +20,8 @@ void ControllingSystem::tick(double) auto& controllable = game_.getEntityManager(). getComponent(entity); + auto& orienting = game_.getSystemManager().getSystem(); + if (action == GLFW_PRESS) { if (key == controllable.getLeftKey()) @@ -37,7 +30,7 @@ void ControllingSystem::tick(double) if (!controllable.isFrozen()) { - walkLeft(entity); + orienting.moveLeft(entity); } } else if (key == controllable.getRightKey()) { @@ -45,19 +38,19 @@ void ControllingSystem::tick(double) if (!controllable.isFrozen()) { - walkRight(entity); + orienting.moveRight(entity); } } else if (key == controllable.getJumpKey()) { if (!controllable.isFrozen()) { - jump(entity); + orienting.jump(entity); } } else if (key == controllable.getDropKey()) { if (!controllable.isFrozen()) { - drop(entity, true); + orienting.drop(entity); } } } else if (action == GLFW_RELEASE) @@ -70,9 +63,9 @@ void ControllingSystem::tick(double) { if (controllable.isHoldingRight()) { - walkRight(entity); + orienting.moveRight(entity); } else { - stopWalking(entity); + orienting.stopWalking(entity); } } } else if (key == controllable.getRightKey()) @@ -83,22 +76,22 @@ void ControllingSystem::tick(double) { if (controllable.isHoldingLeft()) { - walkLeft(entity); + orienting.moveLeft(entity); } else { - stopWalking(entity); + orienting.stopWalking(entity); } } } else if (key == controllable.getDropKey()) { if (!controllable.isFrozen()) { - drop(entity, false); + orienting.stopDropping(entity); } } else if (key == controllable.getJumpKey()) { if (!controllable.isFrozen()) { - stopJumping(entity); + orienting.stopJumping(entity); } } } @@ -112,99 +105,3 @@ void ControllingSystem::input(int key, int action) { actions_.push(std::make_pair(key, action)); } - -void ControllingSystem::walkLeft(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - auto& orientable = game_.getEntityManager().getComponent(entity); - - orientable.setFacingRight(false); - ponderable.setVelocityX(-90); - - auto& animating = game_.getSystemManager().getSystem(); - - if (ponderable.getState() == PonderableComponent::State::grounded) - { - animating.startAnimation(entity, "walkingLeft"); - } else { - animating.startAnimation(entity, "stillLeft"); - } -} - -void ControllingSystem::walkRight(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - auto& orientable = game_.getEntityManager().getComponent(entity); - - orientable.setFacingRight(true); - ponderable.setVelocityX(90); - - auto& animating = game_.getSystemManager().getSystem(); - - if (ponderable.getState() == PonderableComponent::State::grounded) - { - animating.startAnimation(entity, "walkingRight"); - } else { - animating.startAnimation(entity, "stillRight"); - } -} - -void ControllingSystem::stopWalking(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - auto& orientable = game_.getEntityManager().getComponent(entity); - - ponderable.setVelocityX(0); - - if (ponderable.getState() == PonderableComponent::State::grounded) - { - auto& animating = game_.getSystemManager().getSystem(); - - if (orientable.isFacingRight()) - { - animating.startAnimation(entity, "stillRight"); - } else { - animating.startAnimation(entity, "stillLeft"); - } - } -} - -void ControllingSystem::jump(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - - if (ponderable.getState() == PonderableComponent::State::grounded) - { - playSound("res/Randomize87.wav", 0.25); - - ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); - ponderable.setState(PonderableComponent::State::jumping); - } -} - -void ControllingSystem::stopJumping(id_type entity) -{ - auto& ponderable = game_.getEntityManager().getComponent(entity); - - if (ponderable.getState() == PonderableComponent::State::jumping) - { - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); - ponderable.setState(PonderableComponent::State::falling); - } -} - -void ControllingSystem::drop(id_type entity, bool start) -{ - auto& droppable = game_.getEntityManager().getComponent(entity); - auto& ponderable = game_.getEntityManager().getComponent(entity); - - if (start && (ponderable.getState() == PonderableComponent::State::grounded)) - { - ponderable.setState(PonderableComponent::State::dropping); - } else if ((!start) && (ponderable.getState() == PonderableComponent::State::dropping)) - { - ponderable.setState(PonderableComponent::State::grounded); - } - droppable.setDroppable(start); -} diff --git a/src/systems/controlling.h b/src/systems/controlling.h index 1f1e8a0..01ed7a0 100644 --- a/src/systems/controlling.h +++ b/src/systems/controlling.h @@ -3,7 +3,6 @@ #include "system.h" #include -#include "entity_manager.h" class ControllingSystem : public System { public: @@ -17,13 +16,6 @@ public: private: - void walkLeft(id_type entity); - void walkRight(id_type entity); - void stopWalking(id_type entity); - void jump(id_type entity); - void stopJumping(id_type entity); - void drop(id_type entity, bool start); - std::queue> actions_; }; diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index 8723e16..5b63ded 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp @@ -3,8 +3,6 @@ #include "game.h" #include "consts.h" -#include - template inline void addBoundary( Storage& boundaries, diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp new file mode 100644 index 0000000..187bebc --- /dev/null +++ b/src/systems/orienting.cpp @@ -0,0 +1,236 @@ +#include "orienting.h" +#include "game.h" +#include "components/orientable.h" +#include "components/ponderable.h" +#include "systems/animating.h" +#include "consts.h" +#include "muxer.h" + +void OrientingSystem::tick(double) +{ + auto entities = game_.getEntityManager().getEntitiesWithComponents< + OrientableComponent, + PonderableComponent>(); + + for (id_type entity : entities) + { + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + switch (orientable.getWalkState()) + { + case OrientableComponent::WalkState::still: + { + ponderable.setVelocityX(0); + + break; + } + + case OrientableComponent::WalkState::left: + { + ponderable.setVelocityX(-WALK_SPEED); + + break; + } + + case OrientableComponent::WalkState::right: + { + ponderable.setVelocityX(WALK_SPEED); + + break; + } + } + + if (orientable.isJumping() && (ponderable.getVelocityY() > 0)) + { + orientable.setJumping(false); + } + } +} + +void OrientingSystem::moveLeft(id_type entity) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + orientable.setFacingRight(false); + orientable.setWalkState(OrientableComponent::WalkState::left); + + auto& animating = game_.getSystemManager().getSystem(); + if (ponderable.isGrounded()) + { + animating.startAnimation(entity, "walkingLeft"); + } else { + animating.startAnimation(entity, "stillLeft"); + } +} + +void OrientingSystem::moveRight(id_type entity) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + orientable.setFacingRight(true); + orientable.setWalkState(OrientableComponent::WalkState::right); + + auto& animating = game_.getSystemManager().getSystem(); + if (ponderable.isGrounded()) + { + animating.startAnimation(entity, "walkingRight"); + } else { + animating.startAnimation(entity, "stillRight"); + } +} + +void OrientingSystem::stopWalking(id_type entity) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + orientable.setWalkState(OrientableComponent::WalkState::still); + + if (ponderable.isGrounded()) + { + auto& animating = game_.getSystemManager().getSystem(); + + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } + } +} + +void OrientingSystem::jump(id_type entity) +{ + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + if (ponderable.isGrounded()) + { + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + orientable.setJumping(true); + + playSound("res/Randomize87.wav", 0.25); + + ponderable.setVelocityY(JUMP_VELOCITY); + ponderable.setAccelY(JUMP_GRAVITY); + + auto& animating = game_.getSystemManager().getSystem(); + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } + } +} + +void OrientingSystem::stopJumping(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + if (orientable.isJumping()) + { + orientable.setJumping(false); + + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + ponderable.setAccelY(NORMAL_GRAVITY); + } +} + +void OrientingSystem::land(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + auto& animating = game_.getSystemManager().getSystem(); + + switch (orientable.getWalkState()) + { + case OrientableComponent::WalkState::still: + { + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } + + break; + } + + case OrientableComponent::WalkState::left: + { + animating.startAnimation(entity, "walkingLeft"); + + break; + } + + case OrientableComponent::WalkState::right: + { + animating.startAnimation(entity, "walkingRight"); + + break; + } + } +} + +void OrientingSystem::startFalling(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + auto& animating = game_.getSystemManager().getSystem(); + + if (orientable.isFacingRight()) + { + animating.startAnimation(entity, "stillRight"); + } else { + animating.startAnimation(entity, "stillLeft"); + } +} + +void OrientingSystem::drop(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + auto& ponderable = game_.getEntityManager(). + getComponent(entity); + + if (ponderable.isGrounded() + && (orientable.getDropState() == OrientableComponent::DropState::none)) + { + orientable.setDropState(OrientableComponent::DropState::ready); + } +} + +void OrientingSystem::stopDropping(id_type entity) +{ + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + if (orientable.getDropState() == OrientableComponent::DropState::ready) + { + orientable.setDropState(OrientableComponent::DropState::none); + } +} diff --git a/src/systems/orienting.h b/src/systems/orienting.h new file mode 100644 index 0000000..4ded612 --- /dev/null +++ b/src/systems/orienting.h @@ -0,0 +1,35 @@ +#ifndef ORIENTING_H_099F0C23 +#define ORIENTING_H_099F0C23 + +#include "system.h" + +class OrientingSystem : public System { +public: + + OrientingSystem(Game& game) : System(game) + { + } + + void tick(double dt); + + void moveLeft(id_type entity); + + void moveRight(id_type entity); + + void stopWalking(id_type entity); + + void jump(id_type entity); + + void stopJumping(id_type entity); + + void land(id_type entity); + + void startFalling(id_type entity); + + void drop(id_type entity); + + void stopDropping(id_type entity); + +}; + +#endif /* end of include guard: ORIENTING_H_099F0C23 */ diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 26a6f56..4a165b1 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -2,7 +2,9 @@ #include "game.h" #include "components/ponderable.h" #include "components/transformable.h" -#include "components/droppable.h" +#include "components/orientable.h" +#include "components/mappable.h" +#include "systems/orienting.h" #include "consts.h" void PonderingSystem::tick(double dt) @@ -37,10 +39,8 @@ void PonderingSystem::tick(double dt) double newX = oldX + ponderable.getVelocityX() * dt; double newY = oldY + ponderable.getVelocityY() * dt; - if (ponderable.getVelocityY() > 0.0) - { - ponderable.setState(PonderableComponent::State::falling); - } + bool oldGrounded = ponderable.isGrounded(); + ponderable.setGrounded(false); for (id_type mapEntity : maps) { @@ -64,8 +64,6 @@ void PonderingSystem::tick(double dt) newY, it->first, it->second.getType()); - - break; } } } else if (newX > oldX) @@ -86,8 +84,6 @@ void PonderingSystem::tick(double dt) newY, it->first, it->second.getType()); - - break; } } } @@ -109,8 +105,6 @@ void PonderingSystem::tick(double dt) newY, it->first, it->second.getType()); - - break; } } } else if (newY > oldY) @@ -131,8 +125,6 @@ void PonderingSystem::tick(double dt) newY, it->first, it->second.getType()); - - break; } } } @@ -141,6 +133,31 @@ void PonderingSystem::tick(double dt) // Move transformable.setX(newX); transformable.setY(newY); + + // Perform cleanup for orientable entites + if (game_.getEntityManager().hasComponent(entity)) + { + auto& orientable = game_.getEntityManager(). + getComponent(entity); + + // Handle changes in groundedness + if (ponderable.isGrounded() != oldGrounded) + { + if (ponderable.isGrounded()) + { + game_.getSystemManager().getSystem().land(entity); + } else { + game_.getSystemManager(). + getSystem().startFalling(entity); + } + } + + // Complete dropping, if necessary + if (orientable.getDropState() == OrientableComponent::DropState::active) + { + orientable.setDropState(OrientableComponent::DropState::none); + } + } } } @@ -153,8 +170,7 @@ void PonderingSystem::initializeBody( if (type == PonderableComponent::Type::freefalling) { - ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); - ponderable.setState(PonderableComponent::State::falling); + ponderable.setAccelY(NORMAL_GRAVITY); } } @@ -172,6 +188,8 @@ void PonderingSystem::processCollision( auto& transformable = game_.getEntityManager(). getComponent(entity); + bool touchedGround = false; + switch (type) { case MappableComponent::Boundary::Type::wall: @@ -204,13 +222,7 @@ void PonderingSystem::processCollision( case Direction::down: { - newY = axis - transformable.getH(); - ponderable.setVelocityY(0.0); - - if (ponderable.getState() == PonderableComponent::State::falling) - { - ponderable.setState(PonderableComponent::State::grounded); - } + touchedGround = true; break; } @@ -221,31 +233,19 @@ void PonderingSystem::processCollision( case MappableComponent::Boundary::Type::platform: { - if (game_.getEntityManager().hasComponent(entity)) + if (game_.getEntityManager().hasComponent(entity)) { - auto& droppable = game_.getEntityManager(). - getComponent(entity); + auto& orientable = game_.getEntityManager(). + getComponent(entity); - if (droppable.isDroppable()) + if (orientable.getDropState() != OrientableComponent::DropState::none) { - droppable.setDroppable(false); + orientable.setDropState(OrientableComponent::DropState::active); } else { - newY = axis - transformable.getH(); - ponderable.setVelocityY(0.0); - - if (ponderable.getState() == PonderableComponent::State::falling) - { - ponderable.setState(PonderableComponent::State::grounded); - } + touchedGround = true; } } else { - newY = axis - transformable.getH(); - ponderable.setVelocityY(0.0); - - if (ponderable.getState() == PonderableComponent::State::falling) - { - ponderable.setState(PonderableComponent::State::grounded); - } + touchedGround = true; } break; @@ -258,4 +258,11 @@ void PonderingSystem::processCollision( break; } } + + if (touchedGround) + { + newY = axis - transformable.getH(); + ponderable.setVelocityY(0.0); + ponderable.setGrounded(true); + } } -- cgit 1.4.1