diff --git a/include/fggl/entity/gridworld/zone.hpp b/include/fggl/entity/gridworld/zone.hpp index 9e184dd391b865fbcdc835723f3f6a2d014004e4..94ea20fbe14a9a6b5f460cb3a3a79a5bc1acdddf 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