Gamedev Framework (gf)  0.17.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 
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_API SquareMap {
109  public:
110 
111 
117  SquareMap(Vector2i size);
118 
124  Vector2i getSize() const;
125 
133  PositionRange<int> getRange() const;
134 
151  void setCell(Vector2i pos, Flags<CellProperty> flags);
152 
158  void reset(Flags<CellProperty> flags);
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 
203  void setEmpty(Vector2i pos);
204 
221  void clearFieldOfVision();
222 
230  void clearExplored();
231 
248  void computeFieldOfVision(Vector2i pos, int maxRadius = 0, FieldOfVisionLimit limit = FieldOfVisionLimit::Included, FieldOfVision algorithm = FieldOfVision::Basic);
249 
268  void computeLocalFieldOfVision(Vector2i pos, int maxRadius = 0, FieldOfVisionLimit limit = FieldOfVisionLimit::Included, FieldOfVision algorithm = FieldOfVision::Basic);
269 
279  bool isInFieldOfVision(Vector2i pos) const;
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 
326 template<>
327 struct 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:306
constexpr Flags< CellProperty > EmptyCell
An empty cell.
Definition: Map.h:55
Route
Algorithm for computing a route.
Definition: Map.h:86
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:78
FieldOfVisionLimit
Constant to indicate if the limit is part of the field of vision.
Definition: Map.h:75
The Dijkstra algorithm.
The namespace for gf classes.
Definition: Action.h:35
FieldOfVision
Algorithm for computing a field of vision.
Definition: Map.h:63
A square map.
Definition: Map.h:108
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.