Gamedev Framework (gf)  0.14.0
A C++14 framework for 2D games
Map.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Flags.h"
26 #include "Portability.h"
27 #include "Vector.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline 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 
54 
64 
71  enum class FieldOfVision {
72  Basic,
73  };
74 
83  enum class FieldOfVisionLimit {
84  Included,
85  Excluded,
86  };
87 
94  enum class Route {
95  AStar,
96  Dijkstra,
97  };
98 
116  class GF_API SquareMap {
117  public:
118 
119 
125  SquareMap(Vector2i size);
126 
132  Vector2i getSize() const;
133 
141  PositionRange<int> getRange() const;
142 
159  void setCell(Vector2i pos, CellFlags flags);
160 
166  void reset(CellFlags flags);
167 
176  void setTransparent(Vector2i pos, bool transparent = true);
177 
185  bool isTransparent(Vector2i pos) const;
186 
195  void setWalkable(Vector2i pos, bool walkable = true);
196 
202  bool isWalkable(Vector2i pos) const;
203 
211  void setEmpty(Vector2i pos);
212 
229  void clearFieldOfVision();
230 
238  void clearExplored();
239 
256  void computeFieldOfVision(Vector2i pos, int maxRadius = 0, FieldOfVisionLimit limit = FieldOfVisionLimit::Included, FieldOfVision algorithm = FieldOfVision::Basic);
257 
276  void computeLocalFieldOfVision(Vector2i pos, int maxRadius = 0, FieldOfVisionLimit limit = FieldOfVisionLimit::Included, FieldOfVision algorithm = FieldOfVision::Basic);
277 
287  bool isInFieldOfVision(Vector2i pos) const;
288 
297  bool isExplored(Vector2i pos) const;
298 
321  std::vector<Vector2i> computeRoute(Vector2i origin, Vector2i target, float diagonalCost = Sqrt2, Route algorithm = Route::AStar);
322 
327  private:
328  Array2D<CellFlags, int> m_cells;
329  };
330 
331 #ifndef DOXYGEN_SHOULD_SKIP_THIS
332 }
333 
334 template<>
335 struct EnableBitmaskOperators<CellProperty> {
336  static constexpr bool value = true;
337 };
338 #endif
339 }
340 
341 
342 
343 #endif // GF_MAP_H
A two-dimensional array.
Definition: Array2D.h:306
Route
Algorithm for computing a route.
Definition: Map.h:94
Bitfield relying on an enumeration.
Definition: Flags.h:46
The limits are included in the field of vision.
CellProperty
A property of a cell.
Definition: Map.h:40
The cell is visible (computed by FoV)
A basic algorithm based on ray casting.
constexpr float Sqrt2
The constant.
Definition: Math.h:66
FieldOfVisionLimit
Constant to indicate if the limit is part of the field of vision.
Definition: Map.h:83
The Dijkstra algorithm.
The namespace for gf classes.
Definition: Action.h:35
FieldOfVision
Algorithm for computing a field of vision.
Definition: Map.h:71
A square map.
Definition: Map.h:116
The A* algorithm.
A 2D range.
Definition: Range.h:277
The cell is walkable.
The cell is transparent.
The cell has been explored (computed by FoV)
The limits are not included in the field of vision.
constexpr CellFlags EmptyCell
An empty cell.
Definition: Map.h:63