From 9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 16 Feb 2019 12:12:42 -0500 Subject: Pushing works now --- src/simulation.cpp | 166 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 143 insertions(+), 23 deletions(-) (limited to 'src/simulation.cpp') diff --git a/src/simulation.cpp b/src/simulation.cpp index 7dfc83c..328b4f4 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -16,40 +16,39 @@ void Simulation::tick( !entity.moving) { if (keystate[SDL_SCANCODE_LEFT] && - entity.gridPos.x() > 0 && - level_.getTileset().canPlayerMoveTo( - level_.at(entity.gridPos - vec2s { 1, 0 }))) + moveEntityOnGrid(id, Direction::left, true)) { - entity.moving = true; - entity.destPos = entity.gridPos - vec2s { 1, 0 }; + entity.shouldMoveTo = Direction::left; } else if (keystate[SDL_SCANCODE_UP] && - entity.gridPos.y() > 0 && - level_.getTileset().canPlayerMoveTo( - level_.at(entity.gridPos - vec2s { 0, 1 }))) + moveEntityOnGrid(id, Direction::up, true)) { - entity.moving = true; - entity.destPos = entity.gridPos - vec2s { 0, 1 }; - } else if (keystate[SDL_SCANCODE_RIGHT] && - entity.gridPos.x() < (level_.getSize().w() - 1) && - level_.getTileset().canPlayerMoveTo( - level_.at(entity.gridPos + vec2s { 1, 0 }))) + entity.shouldMoveTo = Direction::up; + } + else if (keystate[SDL_SCANCODE_RIGHT] && + moveEntityOnGrid(id, Direction::right, true)) { - entity.moving = true; - entity.destPos = entity.gridPos + vec2s { 1, 0 }; + entity.shouldMoveTo = Direction::right; } else if (keystate[SDL_SCANCODE_DOWN] && - entity.gridPos.y() < (level_.getSize().h() - 1) && - level_.getTileset().canPlayerMoveTo( - level_.at(entity.gridPos + vec2s { 0, 1 }))) + moveEntityOnGrid(id, Direction::down, true)) + { + entity.shouldMoveTo = Direction::down; + } + else + { + entity.shouldMoveTo = Direction::none; + } + + if (entity.shouldMoveTo != Direction::none) { - entity.moving = true; - entity.destPos = entity.gridPos + vec2s { 0, 1 }; + moveEntityOnGrid(id, entity.shouldMoveTo); } + if (entity.moving) { - entity.movementTween = 0.0; + } } @@ -69,7 +68,7 @@ void Simulation::tick( if (entity.movementTween >= 1.0) { entity.moving = false; - entity.gridPos = entity.destPos; + setGridPos(id, entity.destPos); } } @@ -113,3 +112,124 @@ void Simulation::deleteEntity(id_type id) available_.push_back(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, + bool validate) +{ + Entity& entity = entities_.at(id); + + vec2s shouldMoveTo = entity.gridPos; + + switch (moveDir) + { + case Direction::left: + { + if (entity.gridPos.x() == 0) + { + return false; + } + + shouldMoveTo -= vec2s { 1, 0 }; + + break; + } + + case Direction::right: + { + if (entity.gridPos.x() == level_.getSize().w() - 1) + { + return false; + } + + shouldMoveTo += vec2s { 1, 0 }; + + break; + } + + case Direction::up: + { + if (entity.gridPos.y() == 0) + { + return false; + } + + shouldMoveTo -= vec2s { 0, 1 }; + + break; + } + + case Direction::down: + { + if (entity.gridPos.y() == level_.getSize().h() - 1) + { + return false; + } + + shouldMoveTo += vec2s { 0, 1 }; + + break; + } + } + + if (entity.colliderType == ColliderType::player) + { + if (!level_.getTileset().canPlayerMoveTo(level_.at(shouldMoveTo))) + { + return false; + } + + for (id_type blockId : getGridEntities(shouldMoveTo)) + { + Entity& block = entities_.at(blockId); + + if (!block.playerCanPush) + { + return false; + } + + if (!moveEntityOnGrid(blockId, moveDir, validate)) + { + return false; + } + } + + } + + + + + + if (!validate) + { + entity.moving = true; + entity.destPos = shouldMoveTo; + entity.movementTween = 0.0; + } + + return true; +} -- cgit 1.4.1