1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
#include "simulation.h"
#include "consts.h"
#include "level.h"
#include "views.h"
Simulation::Simulation(
const Level& level) :
level_(level)
{
id_type trainId = emplaceEntity();
Entity& train = getEntity(trainId);
train.size = TILE_SIZE;
train.speed = schedule_.getBPS() * 2.0;
train.colliderType = ColliderType::train;
train.scheduled = true;
train.colorVal = 90;
train.gridPos = vec2s { 6, 1 };
train.moveDir = Direction::left;
id_type playerId = emplaceEntity();
Entity& player = getEntity(playerId);
player.size = TILE_SIZE;
player.speed = 3.0;
player.controllable = true;
player.colliderType = ColliderType::player;
player.colorVal = 180;
player.gridPos = vec2s { 1, 5 };
id_type crateId = emplaceEntity();
Entity& crate = getEntity(crateId);
crate.size = TILE_SIZE;
crate.speed = schedule_.getBPS() * 2.0;
crate.colliderType = ColliderType::crate;
crate.canBePushedBy.insert(ColliderType::player);
crate.canBePushedBy.insert(ColliderType::crate);
crate.canBePushedBy.insert(ColliderType::train);
crate.gridPos = vec2s { 4, 5 };
id_type crateId2 = emplaceEntity();
Entity& crate2 = getEntity(crateId2);
crate2.size = TILE_SIZE;
crate2.speed = schedule_.getBPS() * 2.0;
crate2.colliderType = ColliderType::crate;
crate2.canBePushedBy.insert(ColliderType::player);
crate2.canBePushedBy.insert(ColliderType::crate);
crate2.canBePushedBy.insert(ColliderType::train);
crate2.gridPos = vec2s { 6, 7 };
id_type trackId = emplaceEntity();
Entity& track = getEntity(trackId);
track.size = TILE_SIZE;
track.layer = Layer::track;
track.isTrack = true;
track.trackDir1 = Direction::right;
track.trackDir2 = Direction::down;
track.colorVal = 130;
track.gridPos = vec2s { 6, 1 };
id_type trackId2 = emplaceEntity();
Entity& track2 = getEntity(trackId2);
track2.size = TILE_SIZE;
track2.layer = Layer::track;
track2.isTrack = true;
track2.trackDir1 = Direction::right;
track2.trackDir2 = Direction::up;
track2.colorVal = 130;
track2.gridPos = vec2s { 6, 2 };
id_type trackId3 = emplaceEntity();
Entity& track3 = getEntity(trackId3);
track3.size = TILE_SIZE;
track3.layer = Layer::track;
track3.isTrack = true;
track3.trackDir1 = Direction::up;
track3.trackDir2 = Direction::left;
track3.colorVal = 130;
track3.gridPos = vec2s { 7, 2 };
id_type trackId4 = emplaceEntity();
Entity& track4 = getEntity(trackId4);
track4.size = TILE_SIZE;
track4.layer = Layer::track;
track4.isTrack = true;
track4.trackDir1 = Direction::left;
track4.trackDir2 = Direction::down;
track4.colorVal = 130;
track4.gridPos = vec2s { 7, 1 };
}
void Simulation::tick(
double dt,
const Uint8* keystate)
{
// Control
for (Entity& entity :
entityRange() |
views::isControllable() |
views::isNotMoving())
{
if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT])
{
Direction lookDir = Direction::none;
if (keystate[SDL_SCANCODE_LEFT])
{
lookDir = Direction::left;
}
else if (keystate[SDL_SCANCODE_UP])
{
lookDir = Direction::up;
}
else if (keystate[SDL_SCANCODE_RIGHT])
{
lookDir = Direction::right;
}
else if (keystate[SDL_SCANCODE_DOWN])
{
lookDir = Direction::down;
}
vec2s lookPos = posInDir(entity.gridPos, lookDir);
for (Entity& block :
entityRange() |
views::atGridPos(lookPos) |
views::isNotMoving() |
views::canBePushedBy(entity.colliderType) |
views::isOnLayer(entity.layer))
{
block.shouldMoveDir.push_back(lookDir);
}
} else {
if (keystate[SDL_SCANCODE_LEFT])
{
entity.shouldMoveDir.push_back(Direction::left);
}
if (keystate[SDL_SCANCODE_UP])
{
entity.shouldMoveDir.push_back(Direction::up);
}
if (keystate[SDL_SCANCODE_RIGHT])
{
entity.shouldMoveDir.push_back(Direction::right);
}
if (keystate[SDL_SCANCODE_DOWN])
{
entity.shouldMoveDir.push_back(Direction::down);
}
}
}
// Schedule
schedule_.accumulate(dt);
if (schedule_.step())
{
for (Entity& entity :
entityRange() |
views::isScheduled() |
views::isNotMoving())
{
auto tracks =
entityRange() |
views::atGridPos(entity.gridPos) |
views::isTrack();
if (!ranges::empty(tracks))
{
Entity& track = ranges::front(tracks);
Direction from = oppositeDir(entity.moveDir);
if (from == track.trackDir1)
{
entity.shouldMoveDir.push_back(track.trackDir2);
} else if (from == track.trackDir2)
{
entity.shouldMoveDir.push_back(track.trackDir1);
}
}
}
}
// Collision
// Movement
for (Entity& entity : entityRange())
{
while (!entity.moving && !entity.shouldMoveDir.empty())
{
moveEntityOnGrid(entity, entity.shouldMoveDir.front());
entity.shouldMoveDir.pop_front();
}
entity.shouldMoveDir.clear();
if (entity.moving)
{
entity.movementTween += entity.speed * dt;
if (entity.movementTween >= 1.0)
{
entity.moving = false;
entity.gridPos = entity.destPos;
}
}
if (entity.moving)
{
entity.pos.x() =
TILE_SIZE.x() * entity.destPos.x() * entity.movementTween +
TILE_SIZE.x() * entity.gridPos.x() * (1.0 - entity.movementTween);
entity.pos.y() =
TILE_SIZE.y() * entity.destPos.y() * entity.movementTween +
TILE_SIZE.y() * entity.gridPos.y() * (1.0 - entity.movementTween);
} else {
entity.pos = TILE_SIZE * entity.gridPos;
}
}
}
void Simulation::render(SDL_Renderer* ren)
{
if (!renderedMap_)
{
renderedMap_ = level_.render(ren, Layer::map);
}
SDL_RenderCopy(ren, renderedMap_.get(), nullptr, nullptr);
constexpr Layer renderOrder[] = { Layer::track, Layer::object };
ranges::for_each(
ranges::view::for_each(
renderOrder,
[&] (Layer layer) {
return entityRange() | views::isOnLayer(layer);
}),
[&] (const Entity& entity) {
SDL_SetRenderDrawColor(ren, entity.colorVal, entity.colorVal, 65, 255);
SDL_Rect rect {
static_cast<int>(entity.pos.x()),
static_cast<int>(entity.pos.y()),
static_cast<int>(entity.size.w()),
static_cast<int>(entity.size.h())
};
SDL_RenderFillRect(ren, &rect);
});
}
id_type Simulation::emplaceEntity()
{
id_type nextId;
if (!available_.empty())
{
nextId = available_.front();
available_.pop_front();
entities_.at(nextId) = Entity(nextId);
} else {
nextId = entities_.size();
entities_.emplace_back(nextId);
}
active_.insert(nextId);
return nextId;
}
void Simulation::deleteEntity(id_type id)
{
available_.push_back(id);
active_.erase(id);
}
bool Simulation::moveEntityOnGrid(
Entity& entity,
Direction moveDir,
bool validate)
{
bool actuallyMove = true;
vec2s shouldMoveTo = posInDir(entity.gridPos, moveDir);
switch (moveDir)
{
case Direction::left:
{
if (entity.gridPos.x() == 0)
{
return false;
}
break;
}
case Direction::right:
{
if (entity.gridPos.x() == level_.getSize().w() - 1)
{
return false;
}
break;
}
case Direction::up:
{
if (entity.gridPos.y() == 0)
{
return false;
}
break;
}
case Direction::down:
{
if (entity.gridPos.y() == level_.getSize().h() - 1)
{
return false;
}
break;
}
}
if (!level_.getTileset(Layer::map).canEntityMoveTo(
entity.colliderType,
level_.at(shouldMoveTo)))
{
return false;
}
// Can't move into a space that something else is already moving into.
if (!ranges::empty(
entityRange() |
views::isMovingTo(shouldMoveTo) |
views::isOnLayer(entity.layer)))
{
return false;
}
for (Entity& block :
entityRange() |
views::atGridPos(shouldMoveTo) |
views::isOnLayer(entity.layer))
{
if (!block.moving)
{
if (!block.canBePushedBy.count(entity.colliderType))
{
return false;
}
if (!moveEntityOnGrid(block, moveDir, validate))
{
return false;
}
} else if (block.moveDir != moveDir)
{
// Can't move perpendicularly into a space that something else is moving
// out of.
return false;
}
double entityTimeLeft = 1.0 / entity.speed;
double blockTimeLeft = (1.0 - block.movementTween) / block.speed;
if (entityTimeLeft < blockTimeLeft)
{
actuallyMove = false;
}
}
if (!validate && actuallyMove)
{
entity.moving = true;
entity.destPos = shouldMoveTo;
entity.movementTween = 0.0;
entity.moveDir = moveDir;
}
return true;
}
|