From 4fc1eaed63dbdc43d111030f2a803e31b99b5c90 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 10 Dec 2022 09:59:01 +0000
Subject: [PATCH] hexagon class tidy up

---
 fggl/CMakeLists.txt           |  1 +
 fggl/grid/CMakeLists.txt      |  4 ++++
 fggl/grid/hexagon.cpp         | 32 ++++++++++++++++++++++++++++++++
 include/fggl/grid/hexagon.hpp | 34 ++++++++++++++++------------------
 include/fggl/math/vector.hpp  |  1 +
 5 files changed, 54 insertions(+), 18 deletions(-)
 create mode 100644 fggl/grid/CMakeLists.txt
 create mode 100644 fggl/grid/hexagon.cpp

diff --git a/fggl/CMakeLists.txt b/fggl/CMakeLists.txt
index e3385dd..9007ff0 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 0000000..87a036c
--- /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 0000000..25a5d30
--- /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 955961e..3d63a38 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 6c6b076..6eb6981 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>
-- 
GitLab