summary refs log tree commit diff stats
path: root/src/systems/controlling.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-02-12 16:39:49 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-02-12 16:39:49 -0500
commit5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b (patch)
tree283cef0e95f237e70892e8b90c841f971cb61fd9 /src/systems/controlling.cpp
parent77be863f4f15d2481a64e4e8dadb4060a6e4e590 (diff)
downloadtherapy-5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b.tar.gz
therapy-5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b.tar.bz2
therapy-5cc80ec58ea5bd66456f6f5286fa5f26d3fe702b.zip
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.
Diffstat (limited to 'src/systems/controlling.cpp')
-rw-r--r--src/systems/controlling.cpp129
1 files changed, 13 insertions, 116 deletions
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 @@
1#include "controlling.h" 1#include "controlling.h"
2#include "game.h" 2#include "game.h"
3#include "components/controllable.h" 3#include "components/controllable.h"
4#include "components/ponderable.h"
5#include "components/animatable.h"
6#include "components/droppable.h"
7#include "components/orientable.h" 4#include "components/orientable.h"
8#include "systems/animating.h" 5#include "systems/orienting.h"
9#include "direction.h"
10#include "muxer.h"
11#include "consts.h"
12 6
13void ControllingSystem::tick(double) 7void ControllingSystem::tick(double)
14{ 8{
@@ -19,9 +13,6 @@ void ControllingSystem::tick(double)
19 13
20 auto entities = game_.getEntityManager().getEntitiesWithComponents< 14 auto entities = game_.getEntityManager().getEntitiesWithComponents<
21 ControllableComponent, 15 ControllableComponent,
22 PonderableComponent,
23 AnimatableComponent,
24 DroppableComponent,
25 OrientableComponent>(); 16 OrientableComponent>();
26 17
27 for (auto entity : entities) 18 for (auto entity : entities)
@@ -29,6 +20,8 @@ void ControllingSystem::tick(double)
29 auto& controllable = game_.getEntityManager(). 20 auto& controllable = game_.getEntityManager().
30 getComponent<ControllableComponent>(entity); 21 getComponent<ControllableComponent>(entity);
31 22
23 auto& orienting = game_.getSystemManager().getSystem<OrientingSystem>();
24
32 if (action == GLFW_PRESS) 25 if (action == GLFW_PRESS)
33 { 26 {
34 if (key == controllable.getLeftKey()) 27 if (key == controllable.getLeftKey())
@@ -37,7 +30,7 @@ void ControllingSystem::tick(double)
37 30
38 if (!controllable.isFrozen()) 31 if (!controllable.isFrozen())
39 { 32 {
40 walkLeft(entity); 33 orienting.moveLeft(entity);
41 } 34 }
42 } else if (key == controllable.getRightKey()) 35 } else if (key == controllable.getRightKey())
43 { 36 {
@@ -45,19 +38,19 @@ void ControllingSystem::tick(double)
45 38
46 if (!controllable.isFrozen()) 39 if (!controllable.isFrozen())
47 { 40 {
48 walkRight(entity); 41 orienting.moveRight(entity);
49 } 42 }
50 } else if (key == controllable.getJumpKey()) 43 } else if (key == controllable.getJumpKey())
51 { 44 {
52 if (!controllable.isFrozen()) 45 if (!controllable.isFrozen())
53 { 46 {
54 jump(entity); 47 orienting.jump(entity);
55 } 48 }
56 } else if (key == controllable.getDropKey()) 49 } else if (key == controllable.getDropKey())
57 { 50 {
58 if (!controllable.isFrozen()) 51 if (!controllable.isFrozen())
59 { 52 {
60 drop(entity, true); 53 orienting.drop(entity);
61 } 54 }
62 } 55 }
63 } else if (action == GLFW_RELEASE) 56 } else if (action == GLFW_RELEASE)
@@ -70,9 +63,9 @@ void ControllingSystem::tick(double)
70 { 63 {
71 if (controllable.isHoldingRight()) 64 if (controllable.isHoldingRight())
72 { 65 {
73 walkRight(entity); 66 orienting.moveRight(entity);
74 } else { 67 } else {
75 stopWalking(entity); 68 orienting.stopWalking(entity);
76 } 69 }
77 } 70 }
78 } else if (key == controllable.getRightKey()) 71 } else if (key == controllable.getRightKey())
@@ -83,22 +76,22 @@ void ControllingSystem::tick(double)
83 { 76 {
84 if (controllable.isHoldingLeft()) 77 if (controllable.isHoldingLeft())
85 { 78 {
86 walkLeft(entity); 79 orienting.moveLeft(entity);
87 } else { 80 } else {
88 stopWalking(entity); 81 orienting.stopWalking(entity);
89 } 82 }
90 } 83 }
91 } else if (key == controllable.getDropKey()) 84 } else if (key == controllable.getDropKey())
92 { 85 {
93 if (!controllable.isFrozen()) 86 if (!controllable.isFrozen())
94 { 87 {
95 drop(entity, false); 88 orienting.stopDropping(entity);
96 } 89 }
97 } else if (key == controllable.getJumpKey()) 90 } else if (key == controllable.getJumpKey())
98 { 91 {
99 if (!controllable.isFrozen()) 92 if (!controllable.isFrozen())
100 { 93 {
101 stopJumping(entity); 94 orienting.stopJumping(entity);
102 } 95 }
103 } 96 }
104 } 97 }
@@ -112,99 +105,3 @@ void ControllingSystem::input(int key, int action)
112{ 105{
113 actions_.push(std::make_pair(key, action)); 106 actions_.push(std::make_pair(key, action));
114} 107}
115
116void ControllingSystem::walkLeft(id_type entity)
117{
118 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
119 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
120
121 orientable.setFacingRight(false);
122 ponderable.setVelocityX(-90);
123
124 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
125
126 if (ponderable.getState() == PonderableComponent::State::grounded)
127 {
128 animating.startAnimation(entity, "walkingLeft");
129 } else {
130 animating.startAnimation(entity, "stillLeft");
131 }
132}
133
134void ControllingSystem::walkRight(id_type entity)
135{
136 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
137 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
138
139 orientable.setFacingRight(true);
140 ponderable.setVelocityX(90);
141
142 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
143
144 if (ponderable.getState() == PonderableComponent::State::grounded)
145 {
146 animating.startAnimation(entity, "walkingRight");
147 } else {
148 animating.startAnimation(entity, "stillRight");
149 }
150}
151
152void ControllingSystem::stopWalking(id_type entity)
153{
154 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
155 auto& orientable = game_.getEntityManager().getComponent<OrientableComponent>(entity);
156
157 ponderable.setVelocityX(0);
158
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 }
170}
171
172void ControllingSystem::jump(id_type entity)
173{
174 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
175
176 if (ponderable.getState() == PonderableComponent::State::grounded)
177 {
178 playSound("res/Randomize87.wav", 0.25);
179
180 ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3));
181 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3));
182 ponderable.setState(PonderableComponent::State::jumping);
183 }
184}
185
186void ControllingSystem::stopJumping(id_type entity)
187{
188 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
189
190 if (ponderable.getState() == PonderableComponent::State::jumping)
191 {
192 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233));
193 ponderable.setState(PonderableComponent::State::falling);
194 }
195}
196
197void ControllingSystem::drop(id_type entity, bool start)
198{
199 auto& droppable = game_.getEntityManager().getComponent<DroppableComponent>(entity);
200 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity);
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 }
209 droppable.setDroppable(start);
210}