From 26fbd8c1edaf94513d9750681edbe449b699efe4 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 22 Feb 2019 17:25:59 -0500 Subject: 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. --- src/simulation.cpp | 114 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 87 insertions(+), 27 deletions(-) (limited to 'src/simulation.cpp') 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 @@ #include "simulation.h" +#include #include "consts.h" #include "level.h" @@ -7,6 +8,59 @@ Simulation::Simulation( const Level& level) : level_(level) { + + + 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 }; + + + 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 player = emplaceEntity(); Entity& entity = getEntity(player); entity.size = TILE_SIZE; @@ -19,7 +73,7 @@ Simulation::Simulation( id_type crateId = emplaceEntity(); Entity& crate = getEntity(crateId); crate.size = TILE_SIZE; - crate.speed = static_cast(schedule_.getBPM()) / 30.0; + crate.speed = schedule_.getBPS() * 2.0; crate.colliderType = ColliderType::crate; crate.canBePushedBy.insert(ColliderType::player); crate.canBePushedBy.insert(ColliderType::crate); @@ -29,24 +83,13 @@ Simulation::Simulation( id_type crateId2 = emplaceEntity(); Entity& crate2 = getEntity(crateId2); crate2.size = TILE_SIZE; - crate2.speed = static_cast(schedule_.getBPM()) / 30.0; + 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 trainId = emplaceEntity(); - Entity& train = getEntity(trainId); - train.size = TILE_SIZE; - train.speed = static_cast(schedule_.getBPM()) / 30.0; - train.colliderType = ColliderType::train; - train.scheduled = true; - train.colorVal = 90; - train.gridPos = vec2s { 6, 1 }; - - - for (id_type id : active_) { @@ -145,7 +188,28 @@ void Simulation::tick( if (entity.scheduled && !entity.moving) { - moveEntityOnGrid(id, Direction::down); + auto tracks = + posCache_.at(entity.gridPos) | + ranges::view::transform([&] (id_type id) -> Entity& { return entities_.at(id); }) | + ranges::view::filter([&] (Entity& other) { return other.isTrack; }); + + if (!ranges::empty(tracks)) + { + Entity& track = ranges::front(tracks); + + Direction dir = Direction::none; + Direction from = oppositeDir(entity.moveDir); + + if (from == track.trackDir1) + { + dir = track.trackDir2; + } else if (from == track.trackDir2) + { + dir = track.trackDir1; + } + + moveEntityOnGrid(id, dir); + } } } } @@ -282,7 +346,10 @@ bool Simulation::moveEntityOnGrid( } // Can't move into a space that something else is already moving into. - if (!moveToCache_.at(shouldMoveTo).empty()) + if (!ranges::empty( + moveToCache_.at(shouldMoveTo) | + ranges::view::transform([&] (id_type id) -> Entity& { return entities_.at(id); }) | + ranges::view::filter([&] (Entity& other) { return other.layer == entity.layer; }))) { return false; } @@ -291,6 +358,11 @@ bool Simulation::moveEntityOnGrid( { Entity& block = entities_.at(blockId); + if (block.layer != entity.layer) + { + continue; + } + if (!block.moving) { if (!block.canBePushedBy.count(entity.colliderType)) @@ -334,15 +406,3 @@ bool Simulation::moveEntityOnGrid( return true; } - -vec2s Simulation::posInDir(vec2s orig, Direction dir) -{ - switch (dir) - { - case Direction::left: return orig - vec2s { 1, 0 }; - case Direction::right: return orig + vec2s { 1, 0 }; - case Direction::up: return orig - vec2s { 0, 1 }; - case Direction::down: return orig + vec2s { 0, 1 }; - case Direction::none: return orig; - } -} -- cgit 1.4.1