Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
Direction.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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_DIRECTION_H
22 #define GF_DIRECTION_H
23 
24 #include "Portability.h"
25 #include "Vector.h"
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
32  /**
33  * @ingroup core
34  * @brief Cardinal and ordinal directions
35  *
36  * gf::Direction represents one the four cardinal directions and the four
37  * ordinal (or intercardinal) directions. A special value is added to
38  * represent the center, it indicates no direction.
39  *
40  */
41  class GF_API Direction {
42  public:
43  /**
44  * @brief Constructor
45  *
46  * By default, no direction, i.e. center (see gf::Direction::Center)
47  */
49  : m_dir(-1)
50  {
51  }
52 
53  /**
54  * @brief Get a unit vector from a direction
55  *
56  * @returns A unit vector representing the direction
57  */
58  Vector2f getUnit() const;
59 
60  /**
61  * @brief Get a vector from a direction
62  *
63  * The vector has its coordinates at -1, 0 or 1 depending on the direction.
64  * It can be used to represent the displacement on a grid in the given
65  * direction.
66  *
67  * @returns A vector representing the direction
68  */
69  Vector2i getVector() const;
70 
71  /**
72  * @brief Get an angle from a direction
73  *
74  * North is at angle 0 and angle grows clockwise.
75  *
76  * @returns an angle (in radians) representing the direction
77  */
78  float getAngle() const;
79 
80  /**
81  * @brief Get the opposite direction
82  *
83  * @return The opposite direction
84  */
86 
87  /**
88  * @brief Get the orthogonal direction clockwise
89  *
90  * @return The orthogonal direction clockwise
91  */
93 
94  /**
95  * @brief Get the orthogonal direction counter-clockwise
96  *
97  * @return The orthogonal direction counter-clockwise
98  */
100 
101  /**
102  * @brief Get the next direction clockwise
103  *
104  * @return The next direction clockwise
105  */
107 
108  /**
109  * @brief Get the next direction counter-clockwise
110  *
111  * @return The next direction counter-clockwise
112  */
114 
115  static const Direction Center; ///< The center, indicates no direction
116  static const Direction North; ///< The north direction
117  static const Direction NorthEast; ///< The north-east direction
118  static const Direction East; ///< The east direction
119  static const Direction SouthEast; ///< The south-east direction
120  static const Direction South; ///< The south direction
121  static const Direction SouthWest; ///< The south-west direction
122  static const Direction West; ///< The west direction
123  static const Direction NorthWest; ///< The north-west direction
124 
125  private:
126  constexpr Direction(int dir)
127  : m_dir(dir)
128  {
129 
130  }
131 
132  int m_dir;
133  };
134 
135 #ifndef DOXYGEN_SHOULD_SKIP_THIS
136 }
137 #endif
138 }
139 
140 
141 #endif // GF_DIRECTION_H
Vector< float, 2 > Vector2f
A float vector with 2 components.
Definition: Vector.h:741
Vector< int, 2 > Vector2i
A int vector with 2 components.
Definition: Vector.h:777
Direction getNextDirectionCCW() const
Get the next direction counter-clockwise.
static const Direction North
The north direction.
Definition: Direction.h:116
Direction getOppositeDirection() const
Get the opposite direction.
Direction getNextDirectionCW() const
Get the next direction clockwise.
Direction()
Constructor.
Definition: Direction.h:48
static const Direction South
The south direction.
Definition: Direction.h:120
static const Direction SouthWest
The south-west direction.
Definition: Direction.h:121
Cardinal and ordinal directions.
Definition: Direction.h:41
Vector2i getVector() const
Get a vector from a direction.
static const Direction West
The west direction.
Definition: Direction.h:122
Definition: Action.h:34
static const Direction SouthEast
The south-east direction.
Definition: Direction.h:119
Direction getOrthogonalDirectionCCW() const
Get the orthogonal direction counter-clockwise.
Direction getOrthogonalDirectionCW() const
Get the orthogonal direction clockwise.
float getAngle() const
Get an angle from a direction.
#define GF_API
Definition: Portability.h:35
Vector2f getUnit() const
Get a unit vector from a direction.
static const Direction NorthEast
The north-east direction.
Definition: Direction.h:117
static const Direction East
The east direction.
Definition: Direction.h:118
static const Direction NorthWest
The north-west direction.
Definition: Direction.h:123
static const Direction Center
The center, indicates no direction.
Definition: Direction.h:115