summary refs log tree commit diff stats
path: root/src/systems/controlling.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2015-06-26 19:59:28 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2015-06-26 19:59:28 -0400
commit428c401f9c1053f7e13ffe641758dfb72791d8dc (patch)
tree3b7c74e0346db3d08319e309c37e975e19395d74 /src/systems/controlling.cpp
parent55c8a14a7e2b2dadf0def3e09f970818164366f5 (diff)
downloadtherapy-428c401f9c1053f7e13ffe641758dfb72791d8dc.tar.gz
therapy-428c401f9c1053f7e13ffe641758dfb72791d8dc.tar.bz2
therapy-428c401f9c1053f7e13ffe641758dfb72791d8dc.zip
Player now moves
Diffstat (limited to 'src/systems/controlling.cpp')
-rw-r--r--src/systems/controlling.cpp168
1 files changed, 168 insertions, 0 deletions
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 @@
1#include "controlling.h"
2#include "game.h"
3#include "components/controllable.h"
4#include "components/ponderable.h"
5#include "components/animatable.h"
6#include "components/droppable.h"
7#include "direction.h"
8#include "muxer.h"
9#include "consts.h"
10
11void ControllingSystem::tick(double dt)
12{
13 while (!actions.empty())
14 {
15 int key = actions.front().first;
16 int action = actions.front().second;
17
18 auto entities = game.getEntityManager().getEntitiesWithComponents<ControllableComponent, PonderableComponent, AnimatableComponent, DroppableComponent>();
19 for (auto entity : entities)
20 {
21 auto& controllable = game.getEntityManager().getComponent<ControllableComponent>(entity);
22
23 if (action == GLFW_PRESS)
24 {
25 if (key == controllable.getLeftKey())
26 {
27 controllable.setHoldingLeft(true);
28
29 if (!controllable.isFrozen())
30 {
31 walkLeft(entity);
32 }
33 } else if (key == controllable.getRightKey())
34 {
35 controllable.setHoldingRight(true);
36
37 if (!controllable.isFrozen())
38 {
39 walkRight(entity);
40 }
41 } else if (key == controllable.getJumpKey())
42 {
43 if (!controllable.isFrozen())
44 {
45 jump(entity);
46 }
47 } else if (key == controllable.getDropKey())
48 {
49 if (!controllable.isFrozen())
50 {
51 drop(entity, true);
52 }
53 }
54 } else if (action == GLFW_RELEASE)
55 {
56 if (key == controllable.getLeftKey())
57 {
58 controllable.setHoldingLeft(false);
59
60 if (!controllable.isFrozen())
61 {
62 if (controllable.isHoldingRight())
63 {
64 walkRight(entity);
65 } else {
66 stopWalking(entity);
67 }
68 }
69 } else if (key == controllable.getRightKey())
70 {
71 controllable.setHoldingRight(false);
72
73 if (!controllable.isFrozen())
74 {
75 if (controllable.isHoldingRight())
76 {
77 walkLeft(entity);
78 } else {
79 stopWalking(entity);
80 }
81 }
82 } else if (key == controllable.getDropKey())
83 {
84 if (!controllable.isFrozen())
85 {
86 drop(entity, false);
87 }
88 } else if (key == controllable.getJumpKey())
89 {
90 if (!controllable.isFrozen())
91 {
92 stopJumping(entity);
93 }
94 }
95 }
96 }
97
98 actions.pop();
99 }
100}
101
102void ControllingSystem::input(int key, int action)
103{
104 actions.push(std::make_pair(key, action));
105}
106
107void ControllingSystem::walkLeft(int entity)
108{
109 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity);
110 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity);
111
112 ponderable.setVelocityX(-90);
113
114 animatable.setDirection(Direction::Left);
115 animatable.setWalking(true);
116}
117
118void ControllingSystem::walkRight(int entity)
119{
120 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity);
121 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity);
122
123 ponderable.setVelocityX(90);
124
125 animatable.setDirection(Direction::Right);
126 animatable.setWalking(true);
127}
128
129void ControllingSystem::stopWalking(int entity)
130{
131 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity);
132 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity);
133
134 ponderable.setVelocityX(0);
135
136 animatable.setWalking(false);
137}
138
139void ControllingSystem::jump(int entity)
140{
141 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity);
142 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity);
143
144 playSound("res/Randomize87.wav", 0.25);
145
146 ponderable.setVelocityY(JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3));
147 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3));
148
149 animatable.setJumping(true);
150}
151
152void ControllingSystem::stopJumping(int entity)
153{
154 auto& ponderable = game.getEntityManager().getComponent<PonderableComponent>(entity);
155 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity);
156
157 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233));
158 animatable.setJumping(false);
159}
160
161void ControllingSystem::drop(int entity, bool start)
162{
163 auto& animatable = game.getEntityManager().getComponent<AnimatableComponent>(entity);
164 auto& droppable = game.getEntityManager().getComponent<DroppableComponent>(entity);
165
166 droppable.setDroppable(start);
167 animatable.setCrouching(start);
168}