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

cleanup some of the hexagon code

parent 34e44037
No related branches found
No related tags found
No related merge requests found
Pipeline #3662 passed
...@@ -35,10 +35,17 @@ namespace demo::hexboard { ...@@ -35,10 +35,17 @@ namespace demo::hexboard {
grass.data->name = "grass"; grass.data->name = "grass";
grass.data->colour = {0.0F, 1.0F, 0.0}; grass.data->colour = {0.0F, 1.0F, 0.0};
fggl::grid::IntHex islandPoint{3,3}; std::array<fggl::grid::IntHex, 4> islands {{
auto island = islandPoint.hexesInRange(2); {3, 3},
for ( auto& hex : island) { {7, 7},
m_board->setTerrain(hex, grass); {10, 10},
{7, 3}
}};
for (auto islandPoint : islands){
auto island = islandPoint.hexesInRange(2);
for (auto &hex : island) {
m_board->setTerrain(hex, grass);
}
} }
} }
...@@ -59,12 +66,10 @@ namespace demo::hexboard { ...@@ -59,12 +66,10 @@ namespace demo::hexboard {
// check if a button was pressed // check if a button was pressed
auto& input = this->input(); auto& input = this->input();
{ {
fggl::math::vec2 screenPos( const fggl::math::vec2 screenPos(
input.mouse.axis(fggl::input::MouseAxis::X), fggl::math::rescale_ndc(input.mouse.axis(fggl::input::MouseAxis::X), 0, 1920),
input.mouse.axis(fggl::input::MouseAxis::Y) fggl::math::rescale_ndc(input.mouse.axis(fggl::input::MouseAxis::Y), 0, 1080)
); );
screenPos.x = fggl::math::rescale_ndc(screenPos.x, 0, 1920);
screenPos.y = fggl::math::rescale_ndc(screenPos.y, 0, 1080);
m_selections->hover = fggl::grid::round2( m_layout->toGrid(screenPos) ); m_selections->hover = fggl::grid::round2( m_layout->toGrid(screenPos) );
if (input.mouse.pressed(fggl::input::MouseButton::LEFT)) { if (input.mouse.pressed(fggl::input::MouseButton::LEFT)) {
...@@ -74,29 +79,13 @@ namespace demo::hexboard { ...@@ -74,29 +79,13 @@ namespace demo::hexboard {
} }
void Scene::drawGrid(fggl::gfx::Paint& paint) { void Scene::drawGrid(fggl::gfx::Paint& paint) {
const float hexRadius = 64.0F;
const auto gridWidth = (int)( (1920 - hexRadius) / (hexRadius * std::sqrt(3.0F)) );
const auto gridHeight = (int)( (1080 - hexRadius) / (hexRadius * (3.0F / 2.0F) ));
auto tiles = m_board->getAllTiles(); auto tiles = m_board->getAllTiles();
for ( const auto& tile : tiles ) {
fggl::grid::IntHex hexPos{0, 0}; auto terrain = m_board->getTerrain(tile);
auto rowBasis = hexPos; if ( terrain.has_value() ) {
for(auto i=0; i<gridHeight; ++i) { const auto& terrainData = terrain.value();
for (auto j=0; j<gridWidth; ++j) { m_layout->paintHex(paint, tile, terrainData.colour);
auto terrain = m_board->getTerrain(hexPos);
if ( terrain.has_value() ) {
const auto& value = terrain.value();
m_layout->paintHex(paint, hexPos, value.colour);
}
// next hexagon
hexPos = hexPos.neighbour(fggl::grid::HexDirPointy::RIGHT);
} }
rowBasis = i % 2 == 0 ? rowBasis.neighbour(fggl::grid::HexDirPointy::BOTTOM_RIGHT) : rowBasis.neighbour(fggl::grid::HexDirPointy::BOTTOM_LEFT);
hexPos = rowBasis;
} }
} }
......
...@@ -38,14 +38,18 @@ ...@@ -38,14 +38,18 @@
namespace fggl::grid { namespace fggl::grid {
const math::mat2 MAT_HEX_POINTY { // factor out the call to sqrt so the matrices can be constexpr
std::sqrt(3.0F), 0.0F, constexpr float M_SQRT_3 = 1.73205080757F;
std::sqrt(3.0F) / 2.0F, 3.0F/2.0F constexpr float M_SQRT_3_OVER_2 = M_SQRT_3 / 2.0F;
constexpr math::mat2 MAT_HEX_POINTY{
M_SQRT_3, 0.0F,
M_SQRT_3_OVER_2, 3.F / 2.F
}; };
const math::mat2 MAT_HEX_FLAT { constexpr math::mat2 MAT_HEX_FLAT{
3.F/2.F, std::sqrt(3.F)/2.F, 3.F / 2.F, M_SQRT_3_OVER_2,
0.0F, std::sqrt(3.F) 0.0F, M_SQRT_3
}; };
struct Orientation { struct Orientation {
...@@ -73,6 +77,11 @@ namespace fggl::grid { ...@@ -73,6 +77,11 @@ namespace fggl::grid {
Layout(Orientation orientation, math::vec2 size, math::vec2 origin) : m_orientation(orientation), m_size(size), m_origin(origin){} Layout(Orientation orientation, math::vec2 size, math::vec2 origin) : m_orientation(orientation), m_size(size), m_origin(origin){}
Layout(Orientation orientation, float size) : m_orientation(orientation), m_size(size, size), m_origin() {} Layout(Orientation orientation, float size) : m_orientation(orientation), m_size(size, size), m_origin() {}
inline void translate(float dx, float dy){
m_origin.x += dx;
m_origin.y += dy;
}
[[nodiscard]] [[nodiscard]]
inline math::vec2 origin() const { inline math::vec2 origin() const {
return m_origin; return m_origin;
......
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