GameKit  0.0.1a
C++ gamedev tools
HitboxComponent.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: HitboxComponent.hpp
5  *
6  * Description:
7  *
8  * Created: 17/01/2018 19:53:26
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_HITBOXCOMPONENT_HPP_
15 #define GK_HITBOXCOMPONENT_HPP_
16 
17 #include <vector>
18 
19 #include "gk/core/Rect.hpp"
20 #include "gk/core/Exception.hpp"
21 
22 namespace gk {
23 
25  public:
26  HitboxComponent(s8 currentHitboxID = 0)
27  : m_currentHitboxID(currentHitboxID) {}
28 
29  HitboxComponent(s16 x, s16 y, u16 width, u16 height) : HitboxComponent(0) {
30  addHitbox(x, y, width, height);
31  }
32 
33  void addHitbox(s16 x, s16 y, u16 width, u16 height) {
34  m_hitboxes.emplace_back(x, y, width, height);
35  }
36 
37  const FloatRect *currentHitbox() const {
38  if(m_currentHitboxID >= 0 && m_currentHitboxID < (s16)m_hitboxes.size()) {
40  } else {
41  return nullptr;
42  }
43  }
44 
45  void setCurrentHitbox(u8 id) {
46  if(id < m_hitboxes.size()) {
47  m_currentHitboxID = id;
48  } else {
49  throw EXCEPTION("Hitbox ID out of range:", (s16)id, "| Array size:", m_hitboxes.size());
50  }
51  }
52 
54 
55  private:
57 
58  std::vector<FloatRect> m_hitboxes;
59 };
60 
61 } // namespace gk
62 
63 #endif // GK_HITBOXCOMPONENT_HPP_
const FloatRect * currentHitbox() const
HitboxComponent(s8 currentHitboxID=0)
HitboxComponent(s16 x, s16 y, u16 width, u16 height)
void addHitbox(s16 x, s16 y, u16 width, u16 height)
std::vector< FloatRect > m_hitboxes
unsigned short u16
Definition: IntTypes.hpp:22
unsigned char u8
Definition: IntTypes.hpp:21
#define EXCEPTION(args...)
Definition: Exception.hpp:22
Utility class for manipulating 2D axis aligned rectangles.
Definition: Rect.hpp:28
signed short s16
Definition: IntTypes.hpp:18
signed char s8
Definition: IntTypes.hpp:17
void setCurrentHitbox(u8 id)