Skip to content
Snippets Groups Projects
Commit 4fc1eaed authored by Joseph Walton-Rivers's avatar Joseph Walton-Rivers
Browse files

hexagon class tidy up

parent 1e171319
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
target_sources( fggl
PRIVATE
hexagon.cpp
)
\ No newline at end of file
/*
* 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
......@@ -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
......
......@@ -20,6 +20,7 @@
#define FGGL_MATH_VECTOR_HPP
#include <ostream>
#include <tuple>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment