diff options
Diffstat (limited to 'src/systems/controlling.cpp')
| -rw-r--r-- | src/systems/controlling.cpp | 138 |
1 files changed, 90 insertions, 48 deletions
| diff --git a/src/systems/controlling.cpp b/src/systems/controlling.cpp index ec62e9a..3647ff8 100644 --- a/src/systems/controlling.cpp +++ b/src/systems/controlling.cpp | |||
| @@ -4,21 +4,30 @@ | |||
| 4 | #include "components/ponderable.h" | 4 | #include "components/ponderable.h" |
| 5 | #include "components/animatable.h" | 5 | #include "components/animatable.h" |
| 6 | #include "components/droppable.h" | 6 | #include "components/droppable.h" |
| 7 | #include "components/orientable.h" | ||
| 8 | #include "systems/animating.h" | ||
| 7 | #include "direction.h" | 9 | #include "direction.h" |
| 8 | #include "muxer.h" | 10 | #include "muxer.h" |
| 9 | #include "consts.h" | 11 | #include "consts.h" |
| 10 | 12 | ||
| 11 | void ControllingSystem::tick(double dt) | 13 | void ControllingSystem::tick(double) |
| 12 | { | 14 | { |
| 13 | while (!actions.empty()) | 15 | while (!actions_.empty()) |
| 14 | { | 16 | { |
| 15 | int key = actions.front().first; | 17 | int key = actions_.front().first; |
| 16 | int action = actions.front().second; | 18 | int action = actions_.front().second; |
| 19 | |||
| 20 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
| 21 | ControllableComponent, | ||
| 22 | PonderableComponent, | ||
| 23 | AnimatableComponent, | ||
| 24 | DroppableComponent, | ||
| 25 | OrientableComponent>(); | ||
| 17 | 26 | ||
| 18 | auto entities = game.getEntityManager().getEntitiesWithComponents<ControllableComponent, PonderableComponent, AnimatableComponent, DroppableComponent>(); | ||
| 19 | for (auto entity : entities) | 27 | for (auto entity : entities) |
| 20 | { | 28 | { |
| 21 | auto& controllable = game.getEntityManager().getComponent<ControllableComponent>(entity); | 29 | auto& controllable = game_.getEntityManager(). |
| 30 | getComponent<ControllableComponent>(entity); | ||
| 22 | 31 | ||
| 23 | if (action == GLFW_PRESS) | 32 | if (action == GLFW_PRESS) |
| 24 | { | 33 | { |
| @@ -95,74 +104,107 @@ void ControllingSystem::tick(double dt) | |||
| 95 | } | 104 | } |
| 96 | } | 105 | } |
| 97 | 106 | ||
| 98 | actions.pop(); | 107 | actions_.pop(); |
| 99 | } | 108 | } |
| 100 | } | 109 | } |
| 101 | 110 | ||
| 102 | void ControllingSystem::input(int key, int action) | 111 | void ControllingSystem::input(int key, int action) |
| 103 | { | 112 | { |
| 104 | actions.push(std::make_pair(key, action)); | 113 | actions_.push(std::make_pair(key, action)); |
| 105 | } | 114 | } |
| 106 | 115 | ||
| 107 | void ControllingSystem::walkLeft(int entity) | 116 | void ControllingSystem::walkLeft(id_type entity) |
| 108 | { | 117 | { |
| 109 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 118 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
| 110 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 119 | auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity); |
| 111 | 120 | ||
| 121 | orientable.setFacingRight(false); | ||
| 112 | ponderable.setVelocityX(-90); | 122 | ponderable.setVelocityX(-90); |
| 113 | 123 | ||
| 114 | animatable.setDirection(Direction::Left); | 124 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); |
| 115 | animatable.setWalking(true); | 125 | |
| 126 | if (ponderable.getState() == PonderableComponent::state::grounded) | ||
| 127 | { | ||
| 128 | animating.startAnimation(entity, "walkingLeft"); | ||
| 129 | } else { | ||
| 130 | animating.startAnimation(entity, "stillLeft"); | ||
| 131 | } | ||
| 116 | } | 132 | } |
| 117 | 133 | ||
| 118 | void ControllingSystem::walkRight(int entity) | 134 | void ControllingSystem::walkRight(id_type entity) |
| 119 | { | 135 | { |
| 120 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 136 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
| 121 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 137 | auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity); |
| 122 | 138 | ||
| 139 | orientable.setFacingRight(true); | ||
| 123 | ponderable.setVelocityX(90); | 140 | ponderable.setVelocityX(90); |
| 124 | 141 | ||
| 125 | animatable.setDirection(Direction::Right); | 142 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); |
| 126 | animatable.setWalking(true); | 143 | |
| 144 | if (ponderable.getState() == PonderableComponent::state::grounded) | ||
| 145 | { | ||
| 146 | animating.startAnimation(entity, "walkingRight"); | ||
| 147 | } else { | ||
| 148 | animating.startAnimation(entity, "stillRight"); | ||
| 149 | } | ||
| 127 | } | 150 | } |
| 128 | 151 | ||
| 129 | void ControllingSystem::stopWalking(int entity) | 152 | void ControllingSystem::stopWalking(id_type entity) |
| 130 | { | 153 | { |
| 131 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 154 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
| 132 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 155 | auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity); |
| 133 | 156 | ||
| 134 | ponderable.setVelocityX(0); | 157 | ponderable.setVelocityX(0); |
| 135 | 158 | ||
| 136 | animatable.setWalking(false); | 159 | if (ponderable.getState() == PonderableComponent::state::grounded) |
| 160 | { | ||
| 161 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
| 162 | |||
| 163 | if (orientable.isFacingRight()) | ||
| 164 | { | ||
| 165 | animating.startAnimation(entity, "stillRight"); | ||
| 166 | } else { | ||
| 167 | animating.startAnimation(entity, "stillLeft"); | ||
| 168 | } | ||
| 169 | } | ||
| 137 | } | 170 | } |
| 138 | 171 | ||
| 139 | void ControllingSystem::jump(int entity) | 172 | void ControllingSystem::jump(id_type entity) |
| 140 | { | 173 | { |
| 141 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 174 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
| 142 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 175 | |
| 143 | 176 | if (ponderable.getState() == PonderableComponent::state::grounded) | |
| 144 | playSound("res/Randomize87.wav", 0.25); | 177 | { |
| 145 | 178 | playSound("res/Randomize87.wav", 0.25); | |
| 146 | ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); | 179 | |
| 147 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); | 180 | ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3)); |
| 148 | 181 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3)); | |
| 149 | animatable.setJumping(true); | 182 | ponderable.setState(PonderableComponent::state::jumping); |
| 183 | } | ||
| 150 | } | 184 | } |
| 151 | 185 | ||
| 152 | void ControllingSystem::stopJumping(int entity) | 186 | void ControllingSystem::stopJumping(id_type entity) |
| 153 | { | 187 | { |
| 154 | auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity); | 188 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
| 155 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 189 | |
| 156 | 190 | if (ponderable.getState() == PonderableComponent::state::jumping) | |
| 157 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); | 191 | { |
| 158 | animatable.setJumping(false); | 192 | ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233)); |
| 193 | ponderable.setState(PonderableComponent::state::falling); | ||
| 194 | } | ||
| 159 | } | 195 | } |
| 160 | 196 | ||
| 161 | void ControllingSystem::drop(int entity, bool start) | 197 | void ControllingSystem::drop(id_type entity, bool start) |
| 162 | { | 198 | { |
| 163 | auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity); | 199 | auto& droppable = game_.getEntityManager().getComponent<DroppableComponent>(entity); |
| 164 | auto& droppable = game.getEntityManager().getComponent<DroppableComponent>(entity); | 200 | auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); |
| 165 | 201 | ||
| 202 | if (start && (ponderable.getState() == PonderableComponent::state::grounded)) | ||
| 203 | { | ||
| 204 | ponderable.setState(PonderableComponent::state::dropping); | ||
| 205 | } else if ((!start) && (ponderable.getState() == PonderableComponent::state::dropping)) | ||
| 206 | { | ||
| 207 | ponderable.setState(PonderableComponent::state::grounded); | ||
| 208 | } | ||
| 166 | droppable.setDroppable(start); | 209 | droppable.setDroppable(start); |
| 167 | animatable.setCrouching(start); | ||
| 168 | } | 210 | } |
