Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Map.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_MAP_H
22#define GF_MAP_H
23
24#include "Array2D.h"
25#include "CoreApi.h"
26#include "Flags.h"
27#include "Vector.h"
28
29namespace gf {
30#ifndef DOXYGEN_SHOULD_SKIP_THIS
31inline namespace v1 {
32#endif
33
40 enum class CellProperty : uint8_t {
41 Transparent = 0x01,
42 Walkable = 0x02,
43 Visible = 0x10,
44 Explored = 0x20,
45 };
46
56
63 enum class FieldOfVision {
64 Basic,
65 };
66
75 enum class FieldOfVisionLimit {
76 Included,
77 Excluded,
78 };
79
86 enum class Route {
87 AStar,
88 Dijkstra,
89 };
90
108 class GF_CORE_API SquareMap {
109 public:
110
111
118
125
134
152
159
168 void setTransparent(Vector2i pos, bool transparent = true);
169
177 bool isTransparent(Vector2i pos) const;
178
187 void setWalkable(Vector2i pos, bool walkable = true);
188
194 bool isWalkable(Vector2i pos) const;
195
204
222
231
249
269
280
289 bool isExplored(Vector2i pos) const;
290
313 std::vector<Vector2i> computeRoute(Vector2i origin, Vector2i target, float diagonalCost = Sqrt2, Route algorithm = Route::AStar);
314
319 private:
320 Array2D<Flags<CellProperty>, int> m_cells;
321 };
322
323#ifndef DOXYGEN_SHOULD_SKIP_THIS
324}
325
326template<>
327struct EnableBitmaskOperators<CellProperty> {
328 static constexpr bool value = true;
329};
330#endif
331}
332
333
334
335#endif // GF_MAP_H
A two-dimensional array.
Definition: Array2D.h:304
A square map.
Definition: Map.h:108
void setCell(Vector2i pos, Flags< CellProperty > flags)
Set the properties of a cell.
void setEmpty(Vector2i pos)
Make a cell empty.
Vector2i getSize() const
Get the size of the map.
bool isInFieldOfVision(Vector2i pos) const
Check if a cell is visible.
void clearFieldOfVision()
Make the whole map not visible.
void reset(Flags< CellProperty > flags)
Initialize the cells with some properties.
bool isWalkable(Vector2i pos) const
Check if a cell is walkable.
PositionRange< int > getRange() const
Get a range of the positions of the map.
void setTransparent(Vector2i pos, bool transparent=true)
Make a cell transparent.
SquareMap(Vector2i size)
Constructor.
void computeFieldOfVision(Vector2i pos, int maxRadius=0, FieldOfVisionLimit limit=FieldOfVisionLimit::Included, FieldOfVision algorithm=FieldOfVision::Basic)
Compute a field of vision.
bool isTransparent(Vector2i pos) const
Check if a cell is transparent.
std::vector< Vector2i > computeRoute(Vector2i origin, Vector2i target, float diagonalCost=Sqrt2, Route algorithm=Route::AStar)
Compute a route between two points.
void clearExplored()
Make the whole map not explored.
void computeLocalFieldOfVision(Vector2i pos, int maxRadius=0, FieldOfVisionLimit limit=FieldOfVisionLimit::Included, FieldOfVision algorithm=FieldOfVision::Basic)
Compute a local field of vision.
void setWalkable(Vector2i pos, bool walkable=true)
Make a cell walkable.
bool isExplored(Vector2i pos) const
Check if a cell is explored.
constexpr float Sqrt2
The constant.
Definition: Math.h:76
FieldOfVision
Algorithm for computing a field of vision.
Definition: Map.h:63
Route
Algorithm for computing a route.
Definition: Map.h:86
constexpr Flags< CellProperty > EmptyCell
An empty cell.
Definition: Map.h:55
CellProperty
A property of a cell.
Definition: Map.h:40
FieldOfVisionLimit
Constant to indicate if the limit is part of the field of vision.
Definition: Map.h:75
@ Basic
A basic algorithm based on ray casting.
@ Dijkstra
The Dijkstra algorithm.
@ AStar
The A* algorithm.
@ Transparent
The cell is transparent.
@ Walkable
The cell is walkable.
@ Explored
The cell has been explored (computed by FoV)
@ Visible
The cell is visible (computed by FoV)
@ Excluded
The limits are not included in the field of vision.
@ Included
The limits are included in the field of vision.
The namespace for gf classes.
A 2D range.
Definition: Range.h:278