From d9c201cbf2fbfe315137e141d886a9bbfa6794ba Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 20 Feb 2019 16:44:09 -0500 Subject: Started implementing schedule Scheduled entities move downward every beat, although they have a speed that is twice the bpm, so essentially they move every other beat? Broke the simulation loop such that different parts run independently -- made it horizontal rather than vertical. Encapsulated the grid cache so that more than one position field could be cached. This is used to make sure that an entity can't move into a space that something else is already moving into. Fixed issue where an entity could move perpendicularly into the space an entity was moving out of. --- src/simulation.cpp | 128 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 99 insertions(+), 29 deletions(-) (limited to 'src/simulation.cpp') diff --git a/src/simulation.cpp b/src/simulation.cpp index 912f7f0..1379c34 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -3,15 +3,68 @@ #include "consts.h" #include "level.h" +Simulation::Simulation( + const Level& level) : + level_(level) +{ + id_type player = emplaceEntity(); + Entity& entity = getEntity(player); + entity.size = TILE_SIZE; + entity.speed = 3.0; + entity.controllable = true; + entity.colliderType = ColliderType::player; + entity.colorVal = 180; + entity.gridPos = vec2s { 1, 5 }; + + id_type crateId = emplaceEntity(); + Entity& crate = getEntity(crateId); + crate.size = TILE_SIZE; + crate.speed = static_cast(schedule_.getBPM()) / 30.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 = static_cast(schedule_.getBPM()) / 30.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_) + { + Entity& entity = entities_.at(id); + + posCache_.set(id, entity.gridPos); + } +} + void Simulation::tick( double dt, const Uint8* keystate) { + // Control for (id_type id : active_) { Entity& entity = entities_.at(id); - // Control if (entity.controllable && !entity.moving) { @@ -38,7 +91,7 @@ void Simulation::tick( vec2s lookPos = posInDir(entity.gridPos, lookDir); - for (id_type blockId : getGridEntities(lookPos)) + for (id_type blockId : posCache_.at(lookPos)) { Entity& block = entities_.at(blockId); @@ -79,16 +132,40 @@ void Simulation::tick( } } } + } + + // Schedule + schedule_.accumulate(dt); + + if (schedule_.step()) + { + for (id_type id : active_) + { + Entity& entity = entities_.at(id); + + if (entity.scheduled && !entity.moving) + { + moveEntityOnGrid(id, Direction::down); + } + } + } + - // Collision + // Collision - // Movement + + + // Movement + for (id_type id : active_) + { + Entity& entity = entities_.at(id); + if (entity.moving) { entity.movementTween += entity.speed * dt; @@ -96,7 +173,9 @@ void Simulation::tick( if (entity.movementTween >= 1.0) { entity.moving = false; - setGridPos(id, entity.destPos); + entity.gridPos = entity.destPos; + posCache_.set(id, entity.gridPos); + moveToCache_.remove(id); } } @@ -141,29 +220,6 @@ void Simulation::deleteEntity(id_type id) active_.erase(id); } -void Simulation::setGridPos(id_type id, vec2s pos) -{ - Entity& entity = entities_.at(id); - - size_t oldPosIndex = - entity.gridPos.x() + entity.gridPos.y() * level_.getSize().w(); - gridCache_[oldPosIndex].erase(id); - - entity.gridPos = pos; - - size_t newPosIndex = - entity.gridPos.x() + entity.gridPos.y() * level_.getSize().w(); - gridCache_[newPosIndex].insert(id); -} - -const std::unordered_set& - Simulation::getGridEntities(vec2s pos) const -{ - size_t posIndex = pos.x() + pos.y() * level_.getSize().w(); - - return gridCache_[posIndex]; -} - bool Simulation::moveEntityOnGrid( id_type id, Direction moveDir, @@ -225,7 +281,13 @@ bool Simulation::moveEntityOnGrid( return false; } - for (id_type blockId : getGridEntities(shouldMoveTo)) + // Can't move into a space that something else is already moving into. + if (!moveToCache_.at(shouldMoveTo).empty()) + { + return false; + } + + for (id_type blockId : posCache_.at(shouldMoveTo)) { Entity& block = entities_.at(blockId); @@ -240,6 +302,11 @@ bool Simulation::moveEntityOnGrid( { 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; @@ -260,6 +327,9 @@ bool Simulation::moveEntityOnGrid( entity.moving = true; entity.destPos = shouldMoveTo; entity.movementTween = 0.0; + entity.moveDir = moveDir; + + moveToCache_.set(id, entity.destPos); } return true; -- cgit 1.4.1