diff options
Diffstat (limited to 'src/systems/controlling.cpp')
| -rw-r--r-- | src/systems/controlling.cpp | 107 |
1 files changed, 107 insertions, 0 deletions
| diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp new file mode 100644 index 0000000..e1609bd --- /dev/null +++ b/src/systems/controlling.cpp | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | #include "controlling.h" | ||
| 2 | #include "game.h" | ||
| 3 | #include "components/controllable.h" | ||
| 4 | #include "components/orientable.h" | ||
| 5 | #include "systems/orienting.h" | ||
| 6 | |||
| 7 | void ControllingSystem::tick(double) | ||
| 8 | { | ||
| 9 | while (!actions_.empty()) | ||
| 10 | { | ||
| 11 | int key = actions_.front().first; | ||
| 12 | int action = actions_.front().second; | ||
| 13 | |||
| 14 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
| 15 | ControllableComponent, | ||
| 16 | OrientableComponent>(); | ||
| 17 | |||
| 18 | for (auto entity : entities) | ||
| 19 | { | ||
| 20 | auto& controllable = game_.getEntityManager(). | ||
| 21 | getComponent<ControllableComponent>(entity); | ||
| 22 | |||
| 23 | auto& orienting = game_.getSystemManager().getSystem<OrientingSystem>(); | ||
| 24 | |||
| 25 | if (action == GLFW_PRESS) | ||
| 26 | { | ||
| 27 | if (key == controllable.getLeftKey()) | ||
| 28 | { | ||
| 29 | controllable.setHoldingLeft(true); | ||
| 30 | |||
| 31 | if (!controllable.isFrozen()) | ||
| 32 | { | ||
| 33 | orienting.moveLeft(entity); | ||
| 34 | } | ||
| 35 | } else if (key == controllable.getRightKey()) | ||
| 36 | { | ||
| 37 | controllable.setHoldingRight(true); | ||
| 38 | |||
| 39 | if (!controllable.isFrozen()) | ||
| 40 | { | ||
| 41 | orienting.moveRight(entity); | ||
| 42 | } | ||
| 43 | } else if (key == controllable.getJumpKey()) | ||
| 44 | { | ||
| 45 | if (!controllable.isFrozen()) | ||
| 46 | { | ||
| 47 | orienting.jump(entity); | ||
| 48 | } | ||
| 49 | } else if (key == controllable.getDropKey()) | ||
| 50 | { | ||
| 51 | if (!controllable.isFrozen()) | ||
| 52 | { | ||
| 53 | orienting.drop(entity); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } else if (action == GLFW_RELEASE) | ||
| 57 | { | ||
| 58 | if (key == controllable.getLeftKey()) | ||
| 59 | { | ||
| 60 | controllable.setHoldingLeft(false); | ||
| 61 | |||
| 62 | if (!controllable.isFrozen()) | ||
| 63 | { | ||
| 64 | if (controllable.isHoldingRight()) | ||
| 65 | { | ||
| 66 | orienting.moveRight(entity); | ||
| 67 | } else { | ||
| 68 | orienting.stopWalking(entity); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } else if (key == controllable.getRightKey()) | ||
| 72 | { | ||
| 73 | controllable.setHoldingRight(false); | ||
| 74 | |||
| 75 | if (!controllable.isFrozen()) | ||
| 76 | { | ||
| 77 | if (controllable.isHoldingLeft()) | ||
| 78 | { | ||
| 79 | orienting.moveLeft(entity); | ||
| 80 | } else { | ||
| 81 | orienting.stopWalking(entity); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } else if (key == controllable.getDropKey()) | ||
| 85 | { | ||
| 86 | if (!controllable.isFrozen()) | ||
| 87 | { | ||
| 88 | orienting.stopDropping(entity); | ||
| 89 | } | ||
| 90 | } else if (key == controllable.getJumpKey()) | ||
| 91 | { | ||
| 92 | if (!controllable.isFrozen()) | ||
| 93 | { | ||
| 94 | orienting.stopJumping(entity); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | actions_.pop(); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | void ControllingSystem::input(int key, int action) | ||
| 105 | { | ||
| 106 | actions_.push(std::make_pair(key, action)); | ||
| 107 | } | ||
