From 428c401f9c1053f7e13ffe641758dfb72791d8dc Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 26 Jun 2015 19:59:28 -0400 Subject: Player now moves --- src/systems/controlling.cpp | 168 ++++++++++++++++++++++++++++++++++++++++++++ src/systems/controlling.h | 26 +++++++ src/systems/pondering.cpp | 23 ++++++ src/systems/pondering.h | 14 ++++ src/systems/rendering.cpp | 12 ++-- src/systems/rendering.h | 5 +- 6 files changed, 241 insertions(+), 7 deletions(-) create mode 100644 src/systems/controlling.cpp create mode 100644 src/systems/controlling.h create mode 100644 src/systems/pondering.cpp create mode 100644 src/systems/pondering.h (limited to 'src/systems') diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp new file mode 100644 index 0000000..b1e73ad --- /dev/null +++ b/src/systems/controlling.cpp @@ -0,0 +1,168 @@ +#include "controlling.h" +#include "game.h" +#include "components/controllable.h" +#include "components/ponderable.h" +#include "components/animatable.h" +#include "components/droppable.h" +#include "direction.h" +#include "muxer.h" +#include "consts.h" + +void ControllingSystem::tick(double dt) +{ + while (!actions.empty()) + { + int key = actions.front().first; + int action = actions.front().second; + + auto entities = game.getEntityManager().getEntitiesWithComponents(); + for (auto entity : entities) + { + auto& controllable = game.getEntityManager().getComponent(entity); + + if (action == GLFW_PRESS) + { + if (key == controllable.getLeftKey()) + { + controllable.setHoldingLeft(true); + + if (!controllable.isFrozen()) + { + walkLeft(entity); + } + } else if (key == controllable.getRightKey()) + { + controllable.setHoldingRight(true); + + if (!controllable.isFrozen()) + { + walkRight(entity); + } + } else if (key == controllable.getJumpKey()) + { + if (!controllable.isFrozen()) + { + jump(entity); + } + } else if (key == controllable.getDropKey()) + { + if (!controllable.isFrozen()) + { + drop(entity, true); + } + } + } else if (action == GLFW_RELEASE) + { + if (key == controllable.getLeftKey()) + { + controllable.setHoldingLeft(false); + + if (!controllable.isFrozen()) + { + if (controllable.isHoldingRight()) + { + walkRight(entity); + } else { + stopWalking(entity); + } + } + } else if (key == controllable.getRightKey()) + { + controllable.setHoldingRight(false); + + if (!controllable.isFrozen()) + { + if (controllable.isHoldingRight()) + { + walkLeft(entity); + } else { + stopWalking(entity); + } + } + } else if (key == controllable.getDropKey()) + { + if (!controllable.isFrozen()) + { + drop(entity, false); + } + } else if (key == controllable.getJumpKey()) + { + if (!controllable.isFrozen()) + { + stopJumping(entity); + } + } + } + } + + actions.pop(); + } +} + +void ControllingSystem::input(int key, int action) +{ + actions.push(std::make_pair(key, action)); +} + +void ControllingSystem::walkLeft(int entity) +{ + auto& ponderable = game.getEntityManager().getComponent(entity); + auto& animatable = game.getEntityManager().getComponent(entity); + + ponderable.setVelocityX(-90); + + animatable.setDirection(Direction::Left); + animatable.setWalking(true); +} + +void ControllingSystem::walkRight(int entity) +{ + auto& ponderable = game.getEntityManager().getComponent(entity); + auto& animatable = game.getEntityManager().getComponent(entity); + + ponderable.setVelocityX(90); + + animatable.setDirection(Direction::Right); + animatable.setWalking(true); +} + +void ControllingSystem::stopWalking(int entity) +{ + auto& ponderable = game.getEntityManager().getComponent(entity); + auto& animatable = game.getEntityManager().getComponent(entity); + + ponderable.setVelocityX(0); + + animatable.setWalking(false); +} + +void ControllingSystem::jump(int entity) +{ + auto& ponderable = game.getEntityManager().getComponent(entity); + auto& animatable = game.getEntityManager().getComponent(entity); + + 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)); + + animatable.setJumping(true); +} + +void ControllingSystem::stopJumping(int entity) +{ + auto& ponderable = game.getEntityManager().getComponent(entity); + auto& animatable = game.getEntityManager().getComponent(entity); + + ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); + animatable.setJumping(false); +} + +void ControllingSystem::drop(int entity, bool start) +{ + auto& animatable = game.getEntityManager().getComponent(entity); + auto& droppable = game.getEntityManager().getComponent(entity); + + droppable.setDroppable(start); + animatable.setCrouching(start); +} diff --git a/src/systems/controlling.h b/src/systems/controlling.h new file mode 100644 index 0000000..61f86eb --- /dev/null +++ b/src/systems/controlling.h @@ -0,0 +1,26 @@ +#ifndef CONTROLLING_H_80B1BB8D +#define CONTROLLING_H_80B1BB8D + +#include "system.h" +#include + +class ControllingSystem : public System { + public: + ControllingSystem(Game& game) + : System(game) {} + + void tick(double dt); + void input(int key, int action); + + private: + void walkLeft(int entity); + void walkRight(int entity); + void stopWalking(int entity); + void jump(int entity); + void stopJumping(int entity); + void drop(int entity, bool start); + + std::queue> actions; +}; + +#endif /* end of include guard: CONTROLLING_H_80B1BB8D */ diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp new file mode 100644 index 0000000..96775d0 --- /dev/null +++ b/src/systems/pondering.cpp @@ -0,0 +1,23 @@ +#include "pondering.h" +#include "game.h" +#include "components/ponderable.h" +#include "components/transformable.h" + +void PonderingSystem::tick(double dt) +{ + auto entities = game.getEntityManager().getEntitiesWithComponents(); + + for (auto entity : entities) + { + auto& transformable = game.getEntityManager().getComponent(entity); + auto& ponderable = game.getEntityManager().getComponent(entity); + + // Accelerate + ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); + ponderable.setVelocityY(ponderable.getVelocityY() + ponderable.getAccelY() * dt); + + // Move + transformable.setX(transformable.getX() + ponderable.getVelocityX() * dt); + transformable.setY(transformable.getY() + ponderable.getVelocityY() * dt); + } +} diff --git a/src/systems/pondering.h b/src/systems/pondering.h new file mode 100644 index 0000000..ad01a22 --- /dev/null +++ b/src/systems/pondering.h @@ -0,0 +1,14 @@ +#ifndef PONDERING_H_F2530E0E +#define PONDERING_H_F2530E0E + +#include "system.h" + +class PonderingSystem : public System { + public: + PonderingSystem(Game& game) + : System(game) {} + + void tick(double dt); +}; + +#endif /* end of include guard: PONDERING_H_F2530E0E */ diff --git a/src/systems/rendering.cpp b/src/systems/rendering.cpp index 0034dc3..251c2bc 100644 --- a/src/systems/rendering.cpp +++ b/src/systems/rendering.cpp @@ -1,17 +1,17 @@ #include "rendering.h" -#include "entity_manager.h" -#include "components/sprite_renderable.h" +#include "game.h" +#include "components/animatable.h" #include "components/transformable.h" -void RenderingSystem::tick(EntityManager& manager, float dt) +void RenderingSystem::tick(double dt) { texture.fill(texture.entirety(), 0, 0, 0); - std::set spriteEntities = manager.getEntitiesWithComponents(); + std::set spriteEntities = game.getEntityManager().getEntitiesWithComponents(); for (int entity : spriteEntities) { - auto& sprite = manager.getComponent(entity); - auto& transform = manager.getComponent(entity); + auto& sprite = game.getEntityManager().getComponent(entity); + auto& transform = game.getEntityManager().getComponent(entity); Rectangle dstrect {(int) transform.getX(), (int) transform.getY(), transform.getW(), transform.getH()}; texture.blit(sprite.getTexture(), sprite.getFrameRect(), dstrect); diff --git a/src/systems/rendering.h b/src/systems/rendering.h index 80ea79e..9b6e27e 100644 --- a/src/systems/rendering.h +++ b/src/systems/rendering.h @@ -7,7 +7,10 @@ class RenderingSystem : public System { public: - void tick(EntityManager& manager, float dt); + RenderingSystem(Game& game) + : System(game) {} + + void tick(double dt); private: Texture texture {GAME_WIDTH, GAME_HEIGHT}; -- cgit 1.4.1