diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-22 17:25:59 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-22 17:25:59 -0500 |
| commit | 26fbd8c1edaf94513d9750681edbe449b699efe4 (patch) | |
| tree | 3356e61d3eca5eda067169a2c584616a49d8e5a5 /src/simulation.cpp | |
| parent | d9c201cbf2fbfe315137e141d886a9bbfa6794ba (diff) | |
| download | dispatcher-26fbd8c1edaf94513d9750681edbe449b699efe4.tar.gz dispatcher-26fbd8c1edaf94513d9750681edbe449b699efe4.tar.bz2 dispatcher-26fbd8c1edaf94513d9750681edbe449b699efe4.zip | |
Trains move on tracks to the beat
Small implementation changes in various places, biggest thing is now we're using ranges, which is experimental and will be included for real in C++20.
Diffstat (limited to 'src/simulation.cpp')
| -rw-r--r-- | src/simulation.cpp | 114 |
1 files changed, 87 insertions, 27 deletions
| diff --git a/src/simulation.cpp b/src/simulation.cpp index 1379c34..77a9a3e 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | #include "simulation.h" | 1 | #include "simulation.h" |
| 2 | 2 | ||
| 3 | #include <range/v3/all.hpp> | ||
| 3 | #include "consts.h" | 4 | #include "consts.h" |
| 4 | #include "level.h" | 5 | #include "level.h" |
| 5 | 6 | ||
| @@ -7,6 +8,59 @@ Simulation::Simulation( | |||
| 7 | const Level& level) : | 8 | const Level& level) : |
| 8 | level_(level) | 9 | level_(level) |
| 9 | { | 10 | { |
| 11 | |||
| 12 | |||
| 13 | id_type trackId = emplaceEntity(); | ||
| 14 | Entity& track = getEntity(trackId); | ||
| 15 | track.size = TILE_SIZE; | ||
| 16 | track.layer = Layer::track; | ||
| 17 | track.isTrack = true; | ||
| 18 | track.trackDir1 = Direction::right; | ||
| 19 | track.trackDir2 = Direction::down; | ||
| 20 | track.colorVal = 130; | ||
| 21 | track.gridPos = vec2s { 6, 1 }; | ||
| 22 | |||
| 23 | id_type trackId2 = emplaceEntity(); | ||
| 24 | Entity& track2 = getEntity(trackId2); | ||
| 25 | track2.size = TILE_SIZE; | ||
| 26 | track2.layer = Layer::track; | ||
| 27 | track2.isTrack = true; | ||
| 28 | track2.trackDir1 = Direction::right; | ||
| 29 | track2.trackDir2 = Direction::up; | ||
| 30 | track2.colorVal = 130; | ||
| 31 | track2.gridPos = vec2s { 6, 2 }; | ||
| 32 | |||
| 33 | id_type trackId3 = emplaceEntity(); | ||
| 34 | Entity& track3 = getEntity(trackId3); | ||
| 35 | track3.size = TILE_SIZE; | ||
| 36 | track3.layer = Layer::track; | ||
| 37 | track3.isTrack = true; | ||
| 38 | track3.trackDir1 = Direction::up; | ||
| 39 | track3.trackDir2 = Direction::left; | ||
| 40 | track3.colorVal = 130; | ||
| 41 | track3.gridPos = vec2s { 7, 2 }; | ||
| 42 | |||
| 43 | id_type trackId4 = emplaceEntity(); | ||
| 44 | Entity& track4 = getEntity(trackId4); | ||
| 45 | track4.size = TILE_SIZE; | ||
| 46 | track4.layer = Layer::track; | ||
| 47 | track4.isTrack = true; | ||
| 48 | track4.trackDir1 = Direction::left; | ||
| 49 | track4.trackDir2 = Direction::down; | ||
| 50 | track4.colorVal = 130; | ||
| 51 | track4.gridPos = vec2s { 7, 1 }; | ||
| 52 | |||
| 53 | |||
| 54 | id_type trainId = emplaceEntity(); | ||
| 55 | Entity& train = getEntity(trainId); | ||
| 56 | train.size = TILE_SIZE; | ||
| 57 | train.speed = schedule_.getBPS() * 2.0; | ||
| 58 | train.colliderType = ColliderType::train; | ||
| 59 | train.scheduled = true; | ||
| 60 | train.colorVal = 90; | ||
| 61 | train.gridPos = vec2s { 6, 1 }; | ||
| 62 | train.moveDir = Direction::left; | ||
| 63 | |||
| 10 | id_type player = emplaceEntity(); | 64 | id_type player = emplaceEntity(); |
| 11 | Entity& entity = getEntity(player); | 65 | Entity& entity = getEntity(player); |
| 12 | entity.size = TILE_SIZE; | 66 | entity.size = TILE_SIZE; |
| @@ -19,7 +73,7 @@ Simulation::Simulation( | |||
| 19 | id_type crateId = emplaceEntity(); | 73 | id_type crateId = emplaceEntity(); |
| 20 | Entity& crate = getEntity(crateId); | 74 | Entity& crate = getEntity(crateId); |
| 21 | crate.size = TILE_SIZE; | 75 | crate.size = TILE_SIZE; |
| 22 | crate.speed = static_cast<double>(schedule_.getBPM()) / 30.0; | 76 | crate.speed = schedule_.getBPS() * 2.0; |
| 23 | crate.colliderType = ColliderType::crate; | 77 | crate.colliderType = ColliderType::crate; |
| 24 | crate.canBePushedBy.insert(ColliderType::player); | 78 | crate.canBePushedBy.insert(ColliderType::player); |
| 25 | crate.canBePushedBy.insert(ColliderType::crate); | 79 | crate.canBePushedBy.insert(ColliderType::crate); |
| @@ -29,24 +83,13 @@ Simulation::Simulation( | |||
| 29 | id_type crateId2 = emplaceEntity(); | 83 | id_type crateId2 = emplaceEntity(); |
| 30 | Entity& crate2 = getEntity(crateId2); | 84 | Entity& crate2 = getEntity(crateId2); |
| 31 | crate2.size = TILE_SIZE; | 85 | crate2.size = TILE_SIZE; |
| 32 | crate2.speed = static_cast<double>(schedule_.getBPM()) / 30.0; | 86 | crate2.speed = schedule_.getBPS() * 2.0; |
| 33 | crate2.colliderType = ColliderType::crate; | 87 | crate2.colliderType = ColliderType::crate; |
| 34 | crate2.canBePushedBy.insert(ColliderType::player); | 88 | crate2.canBePushedBy.insert(ColliderType::player); |
| 35 | crate2.canBePushedBy.insert(ColliderType::crate); | 89 | crate2.canBePushedBy.insert(ColliderType::crate); |
| 36 | crate2.canBePushedBy.insert(ColliderType::train); | 90 | crate2.canBePushedBy.insert(ColliderType::train); |
| 37 | crate2.gridPos = vec2s { 6, 7 }; | 91 | crate2.gridPos = vec2s { 6, 7 }; |
| 38 | 92 | ||
| 39 | id_type trainId = emplaceEntity(); | ||
| 40 | Entity& train = getEntity(trainId); | ||
| 41 | train.size = TILE_SIZE; | ||
| 42 | train.speed = static_cast<double>(schedule_.getBPM()) / 30.0; | ||
| 43 | train.colliderType = ColliderType::train; | ||
| 44 | train.scheduled = true; | ||
| 45 | train.colorVal = 90; | ||
| 46 | train.gridPos = vec2s { 6, 1 }; | ||
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | 93 | ||
| 51 | for (id_type id : active_) | 94 | for (id_type id : active_) |
| 52 | { | 95 | { |
| @@ -145,7 +188,28 @@ void Simulation::tick( | |||
| 145 | 188 | ||
| 146 | if (entity.scheduled && !entity.moving) | 189 | if (entity.scheduled && !entity.moving) |
| 147 | { | 190 | { |
| 148 | moveEntityOnGrid(id, Direction::down); | 191 | auto tracks = |
| 192 | posCache_.at(entity.gridPos) | | ||
| 193 | ranges::view::transform([&] (id_type id) -> Entity& { return entities_.at(id); }) | | ||
| 194 | ranges::view::filter([&] (Entity& other) { return other.isTrack; }); | ||
| 195 | |||
| 196 | if (!ranges::empty(tracks)) | ||
| 197 | { | ||
| 198 | Entity& track = ranges::front(tracks); | ||
| 199 | |||
| 200 | Direction dir = Direction::none; | ||
| 201 | Direction from = oppositeDir(entity.moveDir); | ||
| 202 | |||
| 203 | if (from == track.trackDir1) | ||
| 204 | { | ||
| 205 | dir = track.trackDir2; | ||
| 206 | } else if (from == track.trackDir2) | ||
| 207 | { | ||
| 208 | dir = track.trackDir1; | ||
| 209 | } | ||
| 210 | |||
| 211 | moveEntityOnGrid(id, dir); | ||
| 212 | } | ||
| 149 | } | 213 | } |
| 150 | } | 214 | } |
| 151 | } | 215 | } |
| @@ -282,7 +346,10 @@ bool Simulation::moveEntityOnGrid( | |||
| 282 | } | 346 | } |
| 283 | 347 | ||
| 284 | // Can't move into a space that something else is already moving into. | 348 | // Can't move into a space that something else is already moving into. |
| 285 | if (!moveToCache_.at(shouldMoveTo).empty()) | 349 | if (!ranges::empty( |
| 350 | moveToCache_.at(shouldMoveTo) | | ||
| 351 | ranges::view::transform([&] (id_type id) -> Entity& { return entities_.at(id); }) | | ||
| 352 | ranges::view::filter([&] (Entity& other) { return other.layer == entity.layer; }))) | ||
| 286 | { | 353 | { |
| 287 | return false; | 354 | return false; |
| 288 | } | 355 | } |
| @@ -291,6 +358,11 @@ bool Simulation::moveEntityOnGrid( | |||
| 291 | { | 358 | { |
| 292 | Entity& block = entities_.at(blockId); | 359 | Entity& block = entities_.at(blockId); |
| 293 | 360 | ||
| 361 | if (block.layer != entity.layer) | ||
| 362 | { | ||
| 363 | continue; | ||
| 364 | } | ||
| 365 | |||
| 294 | if (!block.moving) | 366 | if (!block.moving) |
| 295 | { | 367 | { |
| 296 | if (!block.canBePushedBy.count(entity.colliderType)) | 368 | if (!block.canBePushedBy.count(entity.colliderType)) |
| @@ -334,15 +406,3 @@ bool Simulation::moveEntityOnGrid( | |||
| 334 | 406 | ||
| 335 | return true; | 407 | return true; |
| 336 | } | 408 | } |
| 337 | |||
| 338 | vec2s Simulation::posInDir(vec2s orig, Direction dir) | ||
| 339 | { | ||
| 340 | switch (dir) | ||
| 341 | { | ||
| 342 | case Direction::left: return orig - vec2s { 1, 0 }; | ||
| 343 | case Direction::right: return orig + vec2s { 1, 0 }; | ||
| 344 | case Direction::up: return orig - vec2s { 0, 1 }; | ||
| 345 | case Direction::down: return orig + vec2s { 0, 1 }; | ||
| 346 | case Direction::none: return orig; | ||
| 347 | } | ||
| 348 | } | ||
