Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Cells.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21#ifndef GF_CELLS_H
22#define GF_CELLS_H
23
24#include <functional>
25#include <vector>
26
27#include "CellTypes.h"
28#include "CoreApi.h"
29#include "Flags.h"
30#include "Polyline.h"
31#include "Rect.h"
32#include "Vector.h"
33
34namespace gf {
35#ifndef DOXYGEN_SHOULD_SKIP_THIS
36inline namespace v1 {
37#endif
38
45 enum class CellNeighborQuery {
46 Valid = 0x01,
47 Diagonal = 0x02,
48 };
49
50
63 class GF_CORE_API Cells {
64 public:
68 virtual ~Cells();
69
76 virtual RectF computeBounds(Vector2i layerSize) const noexcept = 0;
77
84 virtual RectI computeVisibleArea(const RectF& local) const noexcept = 0;
85
92 virtual RectF computeCellBounds(Vector2i coords) const noexcept = 0;
93
100 virtual Vector2i computeCoordinates(Vector2f position) const noexcept = 0;
101
108 virtual Polyline computePolyline(Vector2i coords) const = 0;
109
118 virtual std::vector<Vector2i> computeNeighbors(Vector2i coords, Vector2i layerSize, Flags<CellNeighborQuery> flags = gf::None) const = 0;
119 };
120
125 class GF_CORE_API OrthogonalCells final : public Cells {
126 public:
133 : m_tileSize(tileSize)
134 {
135 }
136
137 RectF computeBounds(Vector2i layerSize) const noexcept override;
138
139 RectI computeVisibleArea(const RectF& local) const noexcept override;
140
141 RectF computeCellBounds(Vector2i coords) const noexcept override;
142
143 Vector2i computeCoordinates(Vector2f position) const noexcept override;
144
145 Polyline computePolyline(Vector2i coords) const override;
146
147 std::vector<Vector2i> computeNeighbors(Vector2i coords, Vector2i layerSize, Flags<CellNeighborQuery> flags = gf::None) const override;
148
149 private:
150 Vector2f m_tileSize;
151 };
152
153
158 class GF_CORE_API StaggeredCells final : public Cells {
159 public:
168 : m_tileSize(tileSize)
169 , m_axis(axis)
170 , m_index(index)
171 {
172 }
173
174 RectF computeBounds(Vector2i layerSize) const noexcept override;
175
176 RectI computeVisibleArea(const RectF& local) const noexcept override;
177
178 RectF computeCellBounds(Vector2i coords) const noexcept override;
179
180 Vector2i computeCoordinates(Vector2f position) const noexcept override;
181
182 Polyline computePolyline(Vector2i coords) const override;
183
184 std::vector<Vector2i> computeNeighbors(Vector2i coords, Vector2i layerSize, Flags<CellNeighborQuery> flags = gf::None) const override;
185
186 private:
187 Vector2f m_tileSize;
188 CellAxis m_axis;
189 CellIndex m_index;
190 };
191
192
197 class GF_CORE_API HexagonalCells final : public Cells {
198 public:
207 HexagonalCells(Vector2f tileSize, float sideLength, CellAxis axis, CellIndex index)
208 : m_tileSize(tileSize)
209 , m_sideLength(sideLength)
210 , m_axis(axis)
211 , m_index(index)
212 {
213 }
214
222 HexagonalCells(float radius, CellAxis axis, CellIndex index)
223 : m_tileSize(computeRegularSize(axis, radius))
224 , m_sideLength(radius)
225 , m_axis(axis)
226 , m_index(index)
227 {
228 }
229
230 RectF computeBounds(Vector2i layerSize) const noexcept override;
231
232 RectI computeVisibleArea(const RectF& local) const noexcept override;
233
234 RectF computeCellBounds(Vector2i coords) const noexcept override;
235
236 Vector2i computeCoordinates(Vector2f position) const noexcept override;
237
238 Polyline computePolyline(Vector2i coords) const override;
239
240 std::vector<Vector2i> computeNeighbors(Vector2i coords, Vector2i layerSize, Flags<CellNeighborQuery> flags = gf::None) const override;
241
250 static Vector2f computeRegularSize(CellAxis axis, float radius);
251
252 private:
253 Vector2f m_tileSize;
254 float m_sideLength;
255 CellAxis m_axis;
256 CellIndex m_index;
257 };
258
259#ifndef DOXYGEN_SHOULD_SKIP_THIS
260}
261
262template<>
263struct EnableBitmaskOperators<CellNeighborQuery> {
264 static constexpr bool value = true;
265};
266#endif
267}
268
269#endif // GF_TILE_PROPERTIES_H
The properties of cells.
Definition: Cells.h:63
virtual std::vector< Vector2i > computeNeighbors(Vector2i coords, Vector2i layerSize, Flags< CellNeighborQuery > flags=gf::None) const =0
Compute the neighbors of a cell.
virtual Polyline computePolyline(Vector2i coords) const =0
Compute the polyline representing a cell.
virtual RectI computeVisibleArea(const RectF &local) const noexcept=0
Compute the visible area in terms of coordinates.
virtual ~Cells()
Virtual destructor.
virtual RectF computeBounds(Vector2i layerSize) const noexcept=0
Compute the local bounds of the cells.
virtual RectF computeCellBounds(Vector2i coords) const noexcept=0
Compute the cell bounds.
virtual Vector2i computeCoordinates(Vector2f position) const noexcept=0
Compute the coordinates of a cell.
Bitfield relying on an enumeration.
Definition: Flags.h:48
Hexagonal cells.
Definition: Cells.h:197
std::vector< Vector2i > computeNeighbors(Vector2i coords, Vector2i layerSize, Flags< CellNeighborQuery > flags=gf::None) const override
Compute the neighbors of a cell.
RectF computeCellBounds(Vector2i coords) const noexcept override
Compute the cell bounds.
Polyline computePolyline(Vector2i coords) const override
Compute the polyline representing a cell.
RectF computeBounds(Vector2i layerSize) const noexcept override
Compute the local bounds of the cells.
static Vector2f computeRegularSize(CellAxis axis, float radius)
Get the size of a regular hexagon.
HexagonalCells(float radius, CellAxis axis, CellIndex index)
Make hexagonal cells of the specified size.
Definition: Cells.h:222
RectI computeVisibleArea(const RectF &local) const noexcept override
Compute the visible area in terms of coordinates.
HexagonalCells(Vector2f tileSize, float sideLength, CellAxis axis, CellIndex index)
Make hexagonal cells of the specified size.
Definition: Cells.h:207
Vector2i computeCoordinates(Vector2f position) const noexcept override
Compute the coordinates of a cell.
Orthogonal cells.
Definition: Cells.h:125
Polyline computePolyline(Vector2i coords) const override
Compute the polyline representing a cell.
std::vector< Vector2i > computeNeighbors(Vector2i coords, Vector2i layerSize, Flags< CellNeighborQuery > flags=gf::None) const override
Compute the neighbors of a cell.
Vector2i computeCoordinates(Vector2f position) const noexcept override
Compute the coordinates of a cell.
RectF computeBounds(Vector2i layerSize) const noexcept override
Compute the local bounds of the cells.
RectI computeVisibleArea(const RectF &local) const noexcept override
Compute the visible area in terms of coordinates.
RectF computeCellBounds(Vector2i coords) const noexcept override
Compute the cell bounds.
OrthogonalCells(Vector2f tileSize)
Make orthogonal cells of the specified size.
Definition: Cells.h:132
A polyline.
Definition: Polyline.h:47
Staggered cells.
Definition: Cells.h:158
RectI computeVisibleArea(const RectF &local) const noexcept override
Compute the visible area in terms of coordinates.
std::vector< Vector2i > computeNeighbors(Vector2i coords, Vector2i layerSize, Flags< CellNeighborQuery > flags=gf::None) const override
Compute the neighbors of a cell.
StaggeredCells(Vector2f tileSize, CellAxis axis, CellIndex index)
Make staggered cells of the specified size.
Definition: Cells.h:167
RectF computeBounds(Vector2i layerSize) const noexcept override
Compute the local bounds of the cells.
RectF computeCellBounds(Vector2i coords) const noexcept override
Compute the cell bounds.
Vector2i computeCoordinates(Vector2f position) const noexcept override
Compute the coordinates of a cell.
Polyline computePolyline(Vector2i coords) const override
Compute the polyline representing a cell.
CellAxis
Cell axis for staggered or hexagonal maps.
Definition: CellTypes.h:48
CellIndex
Cell index for staggered or hexagonal maps.
Definition: CellTypes.h:37
CellNeighborQuery
Specification of the query of neighborhood.
Definition: Cells.h:45
@ Valid
The neighbors must be valid cells.
@ Diagonal
The neighbors may be in diagonal.
constexpr NoneType None
Constant to represent "none".
Definition: Types.h:45
The namespace for gf classes.