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/main.cpp | 6 +- src/simulation.cpp | 166 +++++++++++++++++++++++++++++++++++++++++++++-------- src/simulation.h | 15 +++++ 3 files changed, 160 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 4dce6a5..c041cf8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,21 +15,19 @@ int main(int, char**) Simulation::id_type player = sim.emplaceEntity(); Entity& entity = sim.getEntity(player); - entity.gridPos.x() = 1; - entity.gridPos.y() = 5; entity.size = TILE_SIZE; entity.speed = 3.0; entity.controllable = true; entity.colliderType = ColliderType::player; entity.colorVal = 180; + sim.setGridPos(player, vec2s { 1, 5 } ); Simulation::id_type crateId = sim.emplaceEntity(); Entity& crate = sim.getEntity(crateId); - crate.gridPos.x() = 4; - crate.gridPos.y() = 5; crate.size = TILE_SIZE; crate.speed = 4.0; crate.playerCanPush = true; + sim.setGridPos(crateId, vec2s { 4, 5 } ); bool quit = false; 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; +} diff --git a/src/simulation.h b/src/simulation.h index bc47642..2f80f9f 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include class Level; @@ -45,13 +47,26 @@ public: return level_; } + void setGridPos(id_type id, vec2s pos); + private: + + + const std::unordered_set& getGridEntities(vec2s pos) const; + + bool moveEntityOnGrid( + id_type id, + Direction moveDir, + bool validate = false); + const Level& level_; std::vector entities_; std::deque available_; std::set active_; + + mutable std::unordered_map> gridCache_; }; #endif /* end of include guard: SIMULATION_H_7BF6EEA4 */ -- cgit 1.4.1