summary refs log tree commit diff stats
path: root/src/systems/pondering.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-02-11 12:34:52 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-02-11 12:34:52 -0500
commit77be863f4f15d2481a64e4e8dadb4060a6e4e590 (patch)
treeca571702d2148a75b5b847e77d26270257f54ebc /src/systems/pondering.cpp
parent1400ade977e13e3b535d3c2fddb6e15de3c9b5a5 (diff)
downloadtherapy-77be863f4f15d2481a64e4e8dadb4060a6e4e590.tar.gz
therapy-77be863f4f15d2481a64e4e8dadb4060a6e4e590.tar.bz2
therapy-77be863f4f15d2481a64e4e8dadb4060a6e4e590.zip
Implemented map rendering and basic collision
Only wall and platform collision currently works, and map edges are not currently implemented.
Diffstat (limited to 'src/systems/pondering.cpp')
-rw-r--r--src/systems/pondering.cpp248
1 files changed, 242 insertions, 6 deletions
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index e40db1d..26a6f56 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp
@@ -2,6 +2,8 @@
2#include "game.h" 2#include "game.h"
3#include "components/ponderable.h" 3#include "components/ponderable.h"
4#include "components/transformable.h" 4#include "components/transformable.h"
5#include "components/droppable.h"
6#include "consts.h"
5 7
6void PonderingSystem::tick(double dt) 8void PonderingSystem::tick(double dt)
7{ 9{
@@ -9,17 +11,251 @@ void PonderingSystem::tick(double dt)
9 PonderableComponent, 11 PonderableComponent,
10 TransformableComponent>(); 12 TransformableComponent>();
11 13
14 auto maps = game_.getEntityManager().getEntitiesWithComponents<
15 MappableComponent>();
16
12 for (id_type entity : entities) 17 for (id_type entity : entities)
13 { 18 {
14 auto& transformable = game_.getEntityManager().getComponent<TransformableComponent>(entity); 19 auto& transformable = game_.getEntityManager().
15 auto& ponderable = game_.getEntityManager().getComponent<PonderableComponent>(entity); 20 getComponent<TransformableComponent>(entity);
21
22 auto& ponderable = game_.getEntityManager().
23 getComponent<PonderableComponent>(entity);
16 24
17 // Accelerate 25 // Accelerate
18 ponderable.setVelocityX(ponderable.getVelocityX() + ponderable.getAccelX() * dt); 26 ponderable.setVelocityX(
19 ponderable.setVelocityY(ponderable.getVelocityY() + ponderable.getAccelY() * dt); 27 ponderable.getVelocityX() + ponderable.getAccelX() * dt);
28
29 ponderable.setVelocityY(
30 ponderable.getVelocityY() + ponderable.getAccelY() * dt);
31
32 const double oldX = transformable.getX();
33 const double oldY = transformable.getY();
34 const double oldRight = oldX + transformable.getW();
35 const double oldBottom = oldY + transformable.getH();
36
37 double newX = oldX + ponderable.getVelocityX() * dt;
38 double newY = oldY + ponderable.getVelocityY() * dt;
39
40 if (ponderable.getVelocityY() > 0.0)
41 {
42 ponderable.setState(PonderableComponent::State::falling);
43 }
44
45 for (id_type mapEntity : maps)
46 {
47 auto& mappable = game_.getEntityManager().
48 getComponent<MappableComponent>(mapEntity);
49
50 if (newX < oldX)
51 {
52 for (auto it = mappable.getLeftBoundaries().lower_bound(oldX);
53 (it != std::end(mappable.getLeftBoundaries())) && (it->first >= newX);
54 it++)
55 {
56 if ((oldBottom > it->second.getLower())
57 && (oldY < it->second.getUpper()))
58 {
59 // We have a collision!
60 processCollision(
61 entity,
62 Direction::left,
63 newX,
64 newY,
65 it->first,
66 it->second.getType());
67
68 break;
69 }
70 }
71 } else if (newX > oldX)
72 {
73 for (auto it = mappable.getRightBoundaries().lower_bound(oldRight);
74 (it != std::end(mappable.getRightBoundaries()))
75 && (it->first <= (newX + transformable.getW()));
76 it++)
77 {
78 if ((oldBottom > it->second.getLower())
79 && (oldY < it->second.getUpper()))
80 {
81 // We have a collision!
82 processCollision(
83 entity,
84 Direction::right,
85 newX,
86 newY,
87 it->first,
88 it->second.getType());
89
90 break;
91 }
92 }
93 }
94
95 if (newY < oldY)
96 {
97 for (auto it = mappable.getUpBoundaries().lower_bound(oldY);
98 (it != std::end(mappable.getUpBoundaries())) && (it->first >= newY);
99 it++)
100 {
101 if ((oldRight > it->second.getLower())
102 && (oldX < it->second.getUpper()))
103 {
104 // We have a collision!
105 processCollision(
106 entity,
107 Direction::up,
108 newX,
109 newY,
110 it->first,
111 it->second.getType());
112
113 break;
114 }
115 }
116 } else if (newY > oldY)
117 {
118 for (auto it = mappable.getDownBoundaries().lower_bound(oldBottom);
119 (it != std::end(mappable.getDownBoundaries()))
120 && (it->first <= (newY + transformable.getH()));
121 it++)
122 {
123 if ((oldRight > it->second.getLower())
124 && (oldX < it->second.getUpper()))
125 {
126 // We have a collision!
127 processCollision(
128 entity,
129 Direction::down,
130 newX,
131 newY,
132 it->first,
133 it->second.getType());
134
135 break;
136 }
137 }
138 }
139 }
20 140
21 // Move 141 // Move
22 transformable.setX(transformable.getX() + ponderable.getVelocityX() * dt); 142 transformable.setX(newX);
23 transformable.setY(transformable.getY() + ponderable.getVelocityY() * dt); 143 transformable.setY(newY);
144 }
145}
146
147void PonderingSystem::initializeBody(
148 id_type entity,
149 PonderableComponent::Type type)
150{
151 auto& ponderable = game_.getEntityManager().
152 emplaceComponent<PonderableComponent>(entity, type);
153
154 if (type == PonderableComponent::Type::freefalling)
155 {
156 ponderable.setAccelY(JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233));
157 ponderable.setState(PonderableComponent::State::falling);
158 }
159}
160
161void PonderingSystem::processCollision(
162 id_type entity,
163 Direction dir,
164 double& newX,
165 double& newY,
166 int axis,
167 MappableComponent::Boundary::Type type)
168{
169 auto& ponderable = game_.getEntityManager().
170 getComponent<PonderableComponent>(entity);
171
172 auto& transformable = game_.getEntityManager().
173 getComponent<TransformableComponent>(entity);
174
175 switch (type)
176 {
177 case MappableComponent::Boundary::Type::wall:
178 {
179 switch (dir)
180 {
181 case Direction::left:
182 {
183 newX = axis;
184 ponderable.setVelocityX(0.0);
185
186 break;
187 }
188
189 case Direction::right:
190 {
191 newX = axis - transformable.getW();
192 ponderable.setVelocityX(0.0);
193
194 break;
195 }
196
197 case Direction::up:
198 {
199 newY = axis;
200 ponderable.setVelocityY(0.0);
201
202 break;
203 }
204
205 case Direction::down:
206 {
207 newY = axis - transformable.getH();
208 ponderable.setVelocityY(0.0);
209
210 if (ponderable.getState() == PonderableComponent::State::falling)
211 {
212 ponderable.setState(PonderableComponent::State::grounded);
213 }
214
215 break;
216 }
217 }
218
219 break;
220 }
221
222 case MappableComponent::Boundary::Type::platform:
223 {
224 if (game_.getEntityManager().hasComponent<DroppableComponent>(entity))
225 {
226 auto& droppable = game_.getEntityManager().
227 getComponent<DroppableComponent>(entity);
228
229 if (droppable.isDroppable())
230 {
231 droppable.setDroppable(false);
232 } else {
233 newY = axis - transformable.getH();
234 ponderable.setVelocityY(0.0);
235
236 if (ponderable.getState() == PonderableComponent::State::falling)
237 {
238 ponderable.setState(PonderableComponent::State::grounded);
239 }
240 }
241 } else {
242 newY = axis - transformable.getH();
243 ponderable.setVelocityY(0.0);
244
245 if (ponderable.getState() == PonderableComponent::State::falling)
246 {
247 ponderable.setState(PonderableComponent::State::grounded);
248 }
249 }
250
251 break;
252 }
253
254 default:
255 {
256 // Not yet implemented.
257
258 break;
259 }
24 } 260 }
25} 261}