From 0394e31524f0050969007949c82f85d89f11c650 Mon Sep 17 00:00:00 2001 From: Joseph Walton-Rivers <joseph@walton-rivers.uk> Date: Sun, 4 Sep 2022 18:21:29 +0100 Subject: [PATCH] add neghbour calculation --- include/fggl/entity/gridworld/zone.hpp | 56 +++++++++++++++----------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/include/fggl/entity/gridworld/zone.hpp b/include/fggl/entity/gridworld/zone.hpp index 9e184dd..94ea20f 100644 --- a/include/fggl/entity/gridworld/zone.hpp +++ b/include/fggl/entity/gridworld/zone.hpp @@ -34,7 +34,6 @@ namespace fggl::entity::grid { template<typename T, uint32_t width, uint32_t height> struct Grid { public: - constexpr static std::array<math::vec2i, 4> DIRECTIONS{{ {-1, 0}, {0, -1}, {1, 0}, {0, 1} }}; Grid() = default; @@ -98,6 +97,7 @@ namespace fggl::entity::grid { template<uint32_t width, uint32_t height> struct Area2D { public: + constexpr static std::array<math::vec2i, 4> DIRECTIONS{{ {-1, 0}, {0, -1}, {1, 0}, {0, 1} }}; inline explicit Area2D(TileSet& tiles) : m_tiles(tiles) { clear(); } @@ -155,33 +155,20 @@ namespace fggl::entity::grid { return canMove(pos + dir) && !blocked(pos, dir); } - inline bool blocked(GridPos pos, math::vec2i dir) const { - auto targetPos = pos; - if ( dir.x == 1 || dir.y == 1 ) { - targetPos = pos + dir; - } - - if ( !inBounds(targetPos) ) { - return true; - } - - auto& wallObj = m_walls.get(targetPos); - if ( dir.y != 0 ) { - return wallObj.north != 0; - } - - if (dir.x != 0) { - return wallObj.west != 0; - } - - return true; - } + inline bool blocked(GridPos pos, math::vec2i dir) const; EntityManager& entities() { return m_entities; } - void neighbours(math::vec2i pos, std::vector<math::vec2i> &neighbours) const; + inline void neighbours(math::vec2i pos, std::vector<math::vec2i> &neighbours) const { + for (auto direction : DIRECTIONS) { + if ( canMove(pos, direction) ) { + auto result = pos + direction; + neighbours.push_back(result); + } + } + } private: TileSet& m_tiles; Grid<uint32_t, width, height> m_floors; @@ -189,6 +176,29 @@ namespace fggl::entity::grid { EntityManager m_entities; }; + template<uint32_t width, uint32_t height> + bool Area2D<width, height>::blocked(GridPos pos, math::vec2i dir) const { + auto targetPos = pos; + if ( dir.x == 1 || dir.y == 1 ) { + targetPos = pos + dir; + } + + if ( !inBounds(targetPos) ) { + return true; + } + + auto& wallObj = m_walls.get(targetPos); + if ( dir.y != 0 ) { + return wallObj.north != 0; + } + + if (dir.x != 0) { + return wallObj.west != 0; + } + + return true; + } + } // namespace fggl::entity::gridworld #endif //FGGL_ENTITY_GRIDWORLD_ZONE_HPP -- GitLab