diff --git a/fggl/CMakeLists.txt b/fggl/CMakeLists.txt index e3385dd8d68f532e210c6ba2a2665063d9f8cb97..9007ff0d49d0f172ea7070313964b20e5240723e 100644 --- a/fggl/CMakeLists.txt +++ b/fggl/CMakeLists.txt @@ -36,6 +36,7 @@ endif () # the important stuff everything else uses add_subdirectory(math) add_subdirectory(util) +add_subdirectory(grid) add_subdirectory(assets) add_subdirectory(phys) diff --git a/fggl/grid/CMakeLists.txt b/fggl/grid/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..87a036c6439930d674b1073b8cb58987c3a1588b --- /dev/null +++ b/fggl/grid/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources( fggl + PRIVATE + hexagon.cpp +) \ No newline at end of file diff --git a/fggl/grid/hexagon.cpp b/fggl/grid/hexagon.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25a5d30f7b2d9a71c8ded733998c15aa416cd408 --- /dev/null +++ b/fggl/grid/hexagon.cpp @@ -0,0 +1,32 @@ +/* + * This file is part of FGGL. + * + * FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with FGGL. + * If not, see <https://www.gnu.org/licenses/>. + */ + +// +// Created by webpigeon on 10/12/22. +// + +#include "fggl/grid/hexagon.hpp" + +namespace fggl::grid { + + std::vector<IntHex> lineTo(const IntHex& start, const IntHex& end) { + int distance = start.distance(end); + std::vector<IntHex> line; + for (auto i=0; i < distance; ++i) { + line.push_back( round(hexLerp(start, end, 1.0F/distance * i)) ); + } + return line; + } + +} // namespace fggl:grid diff --git a/include/fggl/grid/hexagon.hpp b/include/fggl/grid/hexagon.hpp index 955961e41434531cd5bdf9d9c12cc4ab0453e55b..3d63a38fb25c7fc2bc6165df2666dda9531252ef 100644 --- a/include/fggl/grid/hexagon.hpp +++ b/include/fggl/grid/hexagon.hpp @@ -53,27 +53,30 @@ namespace fggl::grid { template<typename T> struct HexPointT { - constexpr HexPointT(T q, T r) : m_pos({q, r}) {} + constexpr HexPointT(T posQ, T posR) : m_pos({posQ, posR}) {} constexpr explicit HexPointT(const std::array<int, 2>& pos) : m_pos(pos[0], pos[1]) {} - inline T q() const { + [[nodiscard]] + constexpr auto q() const -> T { return m_pos[0]; } - inline T r() const { + [[nodiscard]] + constexpr auto r() const -> T{ return m_pos[1]; } - inline T s() const { + [[nodiscard]] + constexpr auto s() const -> T { return -m_pos[0]-m_pos[1]; } inline HexPointT neighbour(HexDirPointy dir) { - return this + HexPointT<T>(HEX_DIRECTIONS[(int)dir]); + return this + HexPointT<T>(HEX_DIRECTIONS.at((int)dir)); } inline HexPointT neighbour(HexDirFlat dir) { - return this + HexPointT<T>(HEX_DIAGONALS[(int)dir]); + return this + HexPointT<T>(HEX_DIAGONALS.at((int)dir)); } HexPointT operator+(const HexPointT<T>& other) const { @@ -90,7 +93,8 @@ namespace fggl::grid { auto operator<=>(const HexPointT<T>& other) const = default; - T distance(const HexPointT& other) const { + [[nodiscard]] + auto distance(const HexPointT& other) const -> T { auto vec = *this - other; return ( ::abs(vec.q()) @@ -99,7 +103,8 @@ namespace fggl::grid { ); } - std::vector<HexPointT<T>> hexesInRange(int range) const { + [[nodiscard]] + auto hexesInRange(int range) const -> std::vector<HexPointT<T>> { std::vector<HexPointT<T>> results; for ( auto q = -range; q <= range; ++q ) { auto stopCount = std::min(range, -q+range); @@ -131,19 +136,12 @@ namespace fggl::grid { float yGrid = std::round( hex.q() ); float x = hex.q() - xGrid; float y = hex.r() - yGrid; - auto dx = std::round(x + 0.5F*y) * (x*x >= y*y); - auto dy = std::round(y + 0.5F*x) * (x*x < y*y); + auto dx = std::round(x + 0.5F*y) * (float)(x*x >= y*y); + auto dy = std::round(y + 0.5F*x) * (float)(x*x < y*y); return { (int)(xGrid + dx), (int)(yGrid + dy) }; } - std::vector<IntHex> lineTo(const IntHex& start, const IntHex& end) { - int distance = start.distance(end); - std::vector<IntHex> line; - for (auto i=0; i < distance; ++i) { - line.push_back( round(hexLerp(start, end, 1.0F/distance * i)) ); - } - return line; - } + std::vector<IntHex> lineTo(const IntHex& start, const IntHex& end); } // namespace fggl::grid diff --git a/include/fggl/math/vector.hpp b/include/fggl/math/vector.hpp index 6c6b07695926f1eab2ef2aafabf33ae05f6ae649..6eb698149e3c55bb98c645e41639277719e6a531 100644 --- a/include/fggl/math/vector.hpp +++ b/include/fggl/math/vector.hpp @@ -20,6 +20,7 @@ #define FGGL_MATH_VECTOR_HPP #include <ostream> +#include <tuple> #define GLM_ENABLE_EXPERIMENTAL #include <glm/glm.hpp>