Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
TileGrid.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
7#pragma once
8
9#include "../core/Types.hpp"
10#include <glm/glm.hpp>
11#include <algorithm>
12#include <cmath>
13#include <initializer_list>
14
15namespace esengine::tilemap {
16
23
24class TileGrid {
25public:
26 TileGrid(GridType type, f32 width, f32 height, f32 side, bool axisX, bool even)
27 : type_(type), width_(width), height_(height), hw_(width * .5f), hh_(height * .5f),
28 side_(type == GridType::Hexagonal
29 ? std::min(side > 0 ? side : height * .5f, axisX ? width : height) : 0),
30 step_(((axisX ? width : height) + side_) * .5f), axis_x_(axisX), even_(even) {}
31
32 // Orthogonal/staggered anchors are top-left; isometric anchors are cell centers.
33 glm::vec2 anchor(i32 x, i32 y) const {
34 if (type_ == GridType::Isometric) return {(x - y) * hw_, -(x + y) * hh_};
35 if (!staggered()) return {x * width_, -y * height_};
36 if (axis_x_) return {x * step_, -y * height_ - (shifted(x) ? hh_ : 0)};
37 return {x * width_ + (shifted(y) ? hw_ : 0), -y * step_};
38 }
39
40 glm::vec2 center(i32 x, i32 y) const {
41 const auto a = anchor(x, y);
42 return type_ == GridType::Isometric ? a : a + glm::vec2{hw_, -hh_};
43 }
44
45 glm::ivec2 cellAt(f32 x, f32 y) const {
46 if (type_ == GridType::Isometric) return {floor(x / width_ - y / height_ + .5f),
47 floor(-y / height_ - x / width_ + .5f)};
48 if (!staggered()) return {floor(x / width_), floor(-y / height_)};
49 const f32 down = -y;
50 const i32 line = floor(((axis_x_ ? x - hw_ : down - hh_) / step_) + .5f);
51 glm::ivec2 fallback{0, 0};
52 // At most three stagger lines can overlap a point; tile contents are irrelevant.
53 for (const i32 delta : {0, -1, 1}) {
54 const i32 candidate = line + delta;
55 const f32 shift = shifted(candidate) ? (axis_x_ ? hh_ : hw_) : 0;
56 const i32 across = floor(((axis_x_ ? down : x) - shift) / (axis_x_ ? height_ : width_));
57 const glm::ivec2 cell = axis_x_ ? glm::ivec2{candidate, across} : glm::ivec2{across, candidate};
58 if (delta == 0) fallback = cell;
59 const auto c = center(cell.x, cell.y);
60 if (contains(x - c.x, y - c.y)) return cell;
61 }
62 return fallback;
63 }
64
65private:
66 static i32 floor(f32 value) { return static_cast<i32>(std::floor(value)); }
67 bool staggered() const { return type_ == GridType::StaggeredIsometric || type_ == GridType::Hexagonal; }
68 bool shifted(i32 line) const { return ((line & 1) != 0) != even_; }
69 bool contains(f32 x, f32 y) const {
70 x = std::abs(x); y = std::abs(y);
71 if (x > hw_ || y > hh_) return false;
72 const f32 halfSide = side_ * .5f;
73 if (axis_x_) return y * (hw_ - halfSide) + hh_ * std::max(0.0f, x - halfSide)
74 <= hh_ * (hw_ - halfSide);
75 return x * (hh_ - halfSide) + hw_ * std::max(0.0f, y - halfSide)
76 <= hw_ * (hh_ - halfSide);
77 }
78 GridType type_;
79 f32 width_, height_, hw_, hh_, side_, step_;
80 bool axis_x_, even_;
81};
82
83} // namespace esengine::tilemap
Core type definitions and utilities for ESEngine.
TileGrid(GridType type, f32 width, f32 height, f32 side, bool axisX, bool even)
Definition TileGrid.hpp:26
glm::ivec2 cellAt(f32 x, f32 y) const
Definition TileGrid.hpp:45
glm::vec2 center(i32 x, i32 y) const
Definition TileGrid.hpp:40
glm::vec2 anchor(i32 x, i32 y) const
Definition TileGrid.hpp:33
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
float f32
32-bit floating point
Definition Types.hpp:59