diff options
Diffstat (limited to 'src/systems/orienting.cpp')
-rw-r--r-- | src/systems/orienting.cpp | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp new file mode 100644 index 0000000..d73ddd2 --- /dev/null +++ b/src/systems/orienting.cpp | |||
@@ -0,0 +1,230 @@ | |||
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.vel.x() = 0.0; | ||
28 | |||
29 | break; | ||
30 | } | ||
31 | |||
32 | case OrientableComponent::WalkState::left: | ||
33 | { | ||
34 | ponderable.vel.x() = -WALK_SPEED; | ||
35 | |||
36 | break; | ||
37 | } | ||
38 | |||
39 | case OrientableComponent::WalkState::right: | ||
40 | { | ||
41 | ponderable.vel.x() = WALK_SPEED; | ||
42 | |||
43 | break; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | if (orientable.isJumping() && (ponderable.vel.y() > 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.grounded) | ||
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.grounded) | ||
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& orientable = game_.getEntityManager(). | ||
97 | getComponent<OrientableComponent>(entity); | ||
98 | |||
99 | orientable.setWalkState(OrientableComponent::WalkState::still); | ||
100 | |||
101 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
102 | |||
103 | if (orientable.isFacingRight()) | ||
104 | { | ||
105 | animating.startAnimation(entity, "stillRight"); | ||
106 | } else { | ||
107 | animating.startAnimation(entity, "stillLeft"); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | void OrientingSystem::jump(id_type entity) | ||
112 | { | ||
113 | auto& ponderable = game_.getEntityManager(). | ||
114 | getComponent<PonderableComponent>(entity); | ||
115 | |||
116 | if (ponderable.grounded) | ||
117 | { | ||
118 | auto& orientable = game_.getEntityManager(). | ||
119 | getComponent<OrientableComponent>(entity); | ||
120 | |||
121 | orientable.setJumping(true); | ||
122 | |||
123 | playSound("res/Randomize87.wav", 0.25); | ||
124 | |||
125 | ponderable.vel.y() = JUMP_VELOCITY; | ||
126 | ponderable.accel.y() = JUMP_GRAVITY; | ||
127 | |||
128 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
129 | if (orientable.isFacingRight()) | ||
130 | { | ||
131 | animating.startAnimation(entity, "stillRight"); | ||
132 | } else { | ||
133 | animating.startAnimation(entity, "stillLeft"); | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | |||
138 | void OrientingSystem::stopJumping(id_type entity) | ||
139 | { | ||
140 | auto& orientable = game_.getEntityManager(). | ||
141 | getComponent<OrientableComponent>(entity); | ||
142 | |||
143 | if (orientable.isJumping()) | ||
144 | { | ||
145 | orientable.setJumping(false); | ||
146 | |||
147 | auto& ponderable = game_.getEntityManager(). | ||
148 | getComponent<PonderableComponent>(entity); | ||
149 | |||
150 | ponderable.accel.y() = NORMAL_GRAVITY; | ||
151 | } | ||
152 | } | ||
153 | |||
154 | void OrientingSystem::land(id_type entity) | ||
155 | { | ||
156 | auto& orientable = game_.getEntityManager(). | ||
157 | getComponent<OrientableComponent>(entity); | ||
158 | |||
159 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
160 | |||
161 | switch (orientable.getWalkState()) | ||
162 | { | ||
163 | case OrientableComponent::WalkState::still: | ||
164 | { | ||
165 | if (orientable.isFacingRight()) | ||
166 | { | ||
167 | animating.startAnimation(entity, "stillRight"); | ||
168 | } else { | ||
169 | animating.startAnimation(entity, "stillLeft"); | ||
170 | } | ||
171 | |||
172 | break; | ||
173 | } | ||
174 | |||
175 | case OrientableComponent::WalkState::left: | ||
176 | { | ||
177 | animating.startAnimation(entity, "walkingLeft"); | ||
178 | |||
179 | break; | ||
180 | } | ||
181 | |||
182 | case OrientableComponent::WalkState::right: | ||
183 | { | ||
184 | animating.startAnimation(entity, "walkingRight"); | ||
185 | |||
186 | break; | ||
187 | } | ||
188 | } | ||
189 | } | ||
190 | |||
191 | void OrientingSystem::startFalling(id_type entity) | ||
192 | { | ||
193 | auto& orientable = game_.getEntityManager(). | ||
194 | getComponent<OrientableComponent>(entity); | ||
195 | |||
196 | auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); | ||
197 | |||
198 | if (orientable.isFacingRight()) | ||
199 | { | ||
200 | animating.startAnimation(entity, "stillRight"); | ||
201 | } else { | ||
202 | animating.startAnimation(entity, "stillLeft"); | ||
203 | } | ||
204 | } | ||
205 | |||
206 | void OrientingSystem::drop(id_type entity) | ||
207 | { | ||
208 | auto& orientable = game_.getEntityManager(). | ||
209 | getComponent<OrientableComponent>(entity); | ||
210 | |||
211 | auto& ponderable = game_.getEntityManager(). | ||
212 | getComponent<PonderableComponent>(entity); | ||
213 | |||
214 | if (ponderable.grounded | ||
215 | && (orientable.getDropState() == OrientableComponent::DropState::none)) | ||
216 | { | ||
217 | orientable.setDropState(OrientableComponent::DropState::ready); | ||
218 | } | ||
219 | } | ||
220 | |||
221 | void OrientingSystem::stopDropping(id_type entity) | ||
222 | { | ||
223 | auto& orientable = game_.getEntityManager(). | ||
224 | getComponent<OrientableComponent>(entity); | ||
225 | |||
226 | if (orientable.getDropState() == OrientableComponent::DropState::ready) | ||
227 | { | ||
228 | orientable.setDropState(OrientableComponent::DropState::none); | ||
229 | } | ||
230 | } | ||