GameKit  0.0.1a
C++ gamedev tools
HealthComponent.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: HealthComponent.hpp
5  *
6  * Description:
7  *
8  * Created: 19/01/2018 16:38:47
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_HEALTHCOMPONENT_HPP_
15 #define GK_HEALTHCOMPONENT_HPP_
16 
17 #include "gk/core/Timer.hpp"
18 
19 namespace gk {
20 
22  public:
23  HealthComponent(const u16 maxLife, const u16 life = 0, const u16 hurtTime = 0)
24  : m_maxLife(maxLife), m_life(life ? life : maxLife), m_hurtTime(hurtTime) {}
25 
26  void setLife(const u16 newLife) { m_life = (newLife > m_maxLife) ? m_maxLife : newLife; }
27  void addLife(const u16 lifeAdded) { setLife(m_life + lifeAdded); }
28  void removeLife(const u16 lifeRemoved) {
30  (lifeRemoved > m_life) ? setLife(0) : setLife(m_life - lifeRemoved);
31 
32  if (m_hurtTime) {
35  }
36  }
37  }
38 
39  u16 life() const { return m_life; }
40  u16 maxLife() const { return m_maxLife; }
41 
42  Timer hurtTimer() { return m_hurtTimer; }
43 
44  private:
45  const u16 m_maxLife = 0;
46  u16 m_life = 0;
47 
48  const u16 m_hurtTime = 0;
49 
51 };
52 
53 } // namespace gk
54 
55 #endif // GK_HEALTHCOMPONENT_HPP_
void start()
Start the timer.
Definition: Timer.cpp:31
unsigned short u16
Definition: IntTypes.hpp:22
void addLife(const u16 lifeAdded)
void reset()
Reset the timer.
Definition: Timer.cpp:38
void removeLife(const u16 lifeRemoved)
u32 time() const
Get time.
Definition: Timer.cpp:44
HealthComponent(const u16 maxLife, const u16 life=0, const u16 hurtTime=0)
bool isStarted() const
Check if the timer is started.
Definition: Timer.hpp:75
Very basic timer.
Definition: Timer.hpp:25
void setLife(const u16 newLife)