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. --- 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 +++++++++-------- 6 files changed, 332 insertions(+), 167 deletions(-) create mode 100644 src/systems/orienting.cpp create mode 100644 src/systems/orienting.h (limited to 'src/systems') 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