Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
Map.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
175  void setTransparent(Vector2i pos);
176 
184  bool isTransparent(Vector2i pos) const;
185 
193  void setWalkable(Vector2i pos);
194 
200  bool isWalkable(Vector2i pos) const;
201 
209  void setEmpty(Vector2i pos);
210 
227  void clearFieldOfVision();
228 
236  void clearExplored();
237 
254  void computeFieldOfVision(Vector2i pos, int maxRadius = 0, FieldOfVisionLimit limit = FieldOfVisionLimit::Included, FieldOfVision algorithm = FieldOfVision::Basic);
255 
274  void computeLocalFieldOfVision(Vector2i pos, int maxRadius = 0, FieldOfVisionLimit limit = FieldOfVisionLimit::Included, FieldOfVision algorithm = FieldOfVision::Basic);
275 
285  bool isInFieldOfVision(Vector2i pos) const;
286 
295  bool isExplored(Vector2i pos) const;
296 
319  std::vector<Vector2i> computeRoute(Vector2i origin, Vector2i target, float diagonalCost = Sqrt2, Route algorithm = Route::AStar);
320 
325  private:
326  Array2D<CellFlags, int> m_cells;
327  };
328 
329 #ifndef DOXYGEN_SHOULD_SKIP_THIS
330 }
331 
332 template<>
333 struct EnableBitmaskOperators<CellProperty> {
334  static constexpr bool value = true;
335 };
336 #endif
337 }
338 
339 
340 
341 #endif // GF_MAP_H
A two-dimensional array.
Definition: Array2D.h:65
Route
Algorithm for computing a route.
Definition: Map.h:94
Bitfield relying on an enumeration.
Definition: Flags.h:68
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:56
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:225
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