diff options
Diffstat (limited to 'src/systems/orienting.cpp')
-rw-r--r-- | src/systems/orienting.cpp | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp new file mode 100644 index 0000000..187bebc --- /dev/null +++ b/src/systems/orienting.cpp | |||
@@ -0,0 +1,236 @@ | |||
1 | #include "orienting.h" | ||
2 | #include "game.h" | ||
3 | #include "components/orientable.h" | ||
4 | #include "components/ponderable.h" | ||
5 | #include "systems/animating.h" | ||
6 | #include "consts.h" | ||
7 | #include "muxer.h" | ||
8 | |||
9 | void OrientingSystem::tick(double) | ||
10 | { | ||
11 | auto entities = game_.getEntityManager().getEntitiesWithComponents< | ||
12 | OrientableComponent, | ||
13 | PonderableComponent>(); | ||
14 | |||
15 | for (id_type entity : entities) | ||
16 | { | ||
17 | auto& orientable = game_.getEntityManager(). | ||
18 | getComponent<OrientableComponent>(entity); | ||
19 | |||
20 | auto& ponderable = game_.getEntityManager(). | ||
21 | getComponent<PonderableComponent>(entity); | ||
22 | |||
23 | switch (orientable.getWalkState()) | ||
24 | { | ||
25 | case OrientableComponent::WalkState::still: | ||
26 | { | ||
27 | ponderable.setVelocityX(0); | ||
28 | |||
29 | break; | ||
30 | } | ||
31 | |||
32 | case OrientableComponent::WalkState::left: | ||
33 | { | ||
34 | ponderable.setVelocityX(-WALK_SPEED); | ||
35 | |||
36 | break; | ||
37 | } | ||
38 | |||
39 | case OrientableComponent::WalkState::right: | ||
40 | { | ||
41 | ponderable.setVelocityX(WALK_SPEED); | ||
42 | |||
43 | break; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | if (orientable.isJumping() && (ponderable.getVelocityY() > 0)) | ||
48 | { | ||
49 | orientable.setJumping(false); | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | |||
54 | void OrientingSystem::moveLeft(id_type entity) | ||
55 | { | ||
56 | auto& ponderable = game_.getEntityManager(). | ||
57 | getComponent<PonderableComponent>(entity); | ||
58 | |||
59 | auto& orientable = game_.getEntityManager(). | ||
60 | getComponent<OrientableComponent>(entity); | ||
61 | |||
62 | orientable.setFacingRight(false); | ||
63 | orientable.setWalkState(OrientableComponent::WalkState::left); | ||
64 | |||
65 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
66 | if (ponderable.isGrounded()) | ||
67 | { | ||
68 | animating.startAnimation(entity, "walkingLeft"); | ||
69 | } else { | ||
70 | animating.startAnimation(entity, "stillLeft"); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | void OrientingSystem::moveRight(id_type entity) | ||
75 | { | ||
76 | auto& ponderable = game_.getEntityManager(). | ||
77 | getComponent<PonderableComponent>(entity); | ||
78 | |||
79 | auto& orientable = game_.getEntityManager(). | ||
80 | getComponent<OrientableComponent>(entity); | ||
81 | |||
82 | orientable.setFacingRight(true); | ||
83 | orientable.setWalkState(OrientableComponent::WalkState::right); | ||
84 | |||
85 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
86 | if (ponderable.isGrounded()) | ||
87 | { | ||
88 | animating.startAnimation(entity, "walkingRight"); | ||
89 | } else { | ||
90 | animating.startAnimation(entity, "stillRight"); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | void OrientingSystem::stopWalking(id_type entity) | ||
95 | { | ||
96 | auto& ponderable = game_.getEntityManager(). | ||
97 | getComponent<PonderableComponent>(entity); | ||
98 | |||
99 | auto& orientable = game_.getEntityManager(). | ||
100 | getComponent<OrientableComponent>(entity); | ||
101 | |||
102 | orientable.setWalkState(OrientableComponent::WalkState::still); | ||
103 | |||
104 | if (ponderable.isGrounded()) | ||
105 | { | ||
106 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
107 | |||
108 | if (orientable.isFacingRight()) | ||
109 | { | ||
110 | animating.startAnimation(entity, "stillRight"); | ||
111 | } else { | ||
112 | animating.startAnimation(entity, "stillLeft"); | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | |||
117 | void OrientingSystem::jump(id_type entity) | ||
118 | { | ||
119 | auto& ponderable = game_.getEntityManager(). | ||
120 | getComponent<PonderableComponent>(entity); | ||
121 | |||
122 | if (ponderable.isGrounded()) | ||
123 | { | ||
124 | auto& orientable = game_.getEntityManager(). | ||
125 | getComponent<OrientableComponent>(entity); | ||
126 | |||
127 | orientable.setJumping(true); | ||
128 | |||
129 | playSound("res/Randomize87.wav", 0.25); | ||
130 | |||
131 | ponderable.setVelocityY(JUMP_VELOCITY); | ||
132 | ponderable.setAccelY(JUMP_GRAVITY); | ||
133 | |||
134 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
135 | if (orientable.isFacingRight()) | ||
136 | { | ||
137 | animating.startAnimation(entity, "stillRight"); | ||
138 | } else { | ||
139 | animating.startAnimation(entity, "stillLeft"); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | |||
144 | void OrientingSystem::stopJumping(id_type entity) | ||
145 | { | ||
146 | auto& orientable = game_.getEntityManager(). | ||
147 | getComponent<OrientableComponent>(entity); | ||
148 | |||
149 | if (orientable.isJumping()) | ||
150 | { | ||
151 | orientable.setJumping(false); | ||
152 | |||
153 | auto& ponderable = game_.getEntityManager(). | ||
154 | getComponent<PonderableComponent>(entity); | ||
155 | |||
156 | ponderable.setAccelY(NORMAL_GRAVITY); | ||
157 | } | ||
158 | } | ||
159 | |||
160 | void OrientingSystem::land(id_type entity) | ||
161 | { | ||
162 | auto& orientable = game_.getEntityManager(). | ||
163 | getComponent<OrientableComponent>(entity); | ||
164 | |||
165 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
166 | |||
167 | switch (orientable.getWalkState()) | ||
168 | { | ||
169 | case OrientableComponent::WalkState::still: | ||
170 | { | ||
171 | if (orientable.isFacingRight()) | ||
172 | { | ||
173 | animating.startAnimation(entity, "stillRight"); | ||
174 | } else { | ||
175 | animating.startAnimation(entity, "stillLeft"); | ||
176 | } | ||
177 | |||
178 | break; | ||
179 | } | ||
180 | |||
181 | case OrientableComponent::WalkState::left: | ||
182 | { | ||
183 | animating.startAnimation(entity, "walkingLeft"); | ||
184 | |||
185 | break; | ||
186 | } | ||
187 | |||
188 | case OrientableComponent::WalkState::right: | ||
189 | { | ||
190 | animating.startAnimation(entity, "walkingRight"); | ||
191 | |||
192 | break; | ||
193 | } | ||
194 | } | ||
195 | } | ||
196 | |||
197 | void OrientingSystem::startFalling(id_type entity) | ||
198 | { | ||
199 | auto& orientable = game_.getEntityManager(). | ||
200 | getComponent<OrientableComponent>(entity); | ||
201 | |||
202 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
203 | |||
204 | if (orientable.isFacingRight()) | ||
205 | { | ||
206 | animating.startAnimation(entity, "stillRight"); | ||
207 | } else { | ||
208 | animating.startAnimation(entity, "stillLeft"); | ||
209 | } | ||
210 | } | ||
211 | |||
212 | void OrientingSystem::drop(id_type entity) | ||
213 | { | ||
214 | auto& orientable = game_.getEntityManager(). | ||
215 | getComponent<OrientableComponent>(entity); | ||
216 | |||
217 | auto& ponderable = game_.getEntityManager(). | ||
218 | getComponent<PonderableComponent>(entity); | ||
219 | |||
220 | if (ponderable.isGrounded() | ||
221 | && (orientable.getDropState() == OrientableComponent::DropState::none)) | ||
222 | { | ||
223 | orientable.setDropState(OrientableComponent::DropState::ready); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | void OrientingSystem::stopDropping(id_type entity) | ||
228 | { | ||
229 | auto& orientable = game_.getEntityManager(). | ||
230 | getComponent<OrientableComponent>(entity); | ||
231 | |||
232 | if (orientable.getDropState() == OrientableComponent::DropState::ready) | ||
233 | { | ||
234 | orientable.setDropState(OrientableComponent::DropState::none); | ||
235 | } | ||
236 | } | ||