GameKit  0.0.1a
C++ gamedev tools
LifetimeComponent.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: LifetimeComponent.hpp
5  *
6  * Description:
7  *
8  * Created: 19/01/2018 04:13:13
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_LIFETIMECOMPONENT_HPP_
15 #define GK_LIFETIMECOMPONENT_HPP_
16 
17 #include <functional>
18 
19 #include "gk/core/Timer.hpp"
20 #include "gk/scene/SceneObject.hpp"
21 
22 namespace gk {
23 
25  using DeathChecker = std::function<bool(const SceneObject &)>;
26 
27  public:
28  LifetimeComponent() = default;
29  LifetimeComponent(u32 lifetime) : m_lifetime(lifetime) { m_timer.start(); }
30  LifetimeComponent(DeathChecker deathChecker) : m_deathChecker(deathChecker) {}
31 
32  void kill() { m_dead = true; }
33 
34  bool almostDead() { return m_lifetime != 0 && m_timer.time() > m_lifetime / 4 * 3; }
35 
36  bool dead(const SceneObject &object) const {
37  return m_dead
38  || (m_lifetime != 0 && m_timer.time() > m_lifetime)
39  || (m_deathChecker && m_deathChecker(object));
40  }
41 
42  u32 aliveTime() { return m_timer.time(); }
43 
44  bool areClientsNotified() const { return m_areClientsNotified; }
45 
46  void setDeathChecker(DeathChecker deathChecker) { m_deathChecker = deathChecker; }
48 
49  private:
51 
52  bool m_dead = false;
53  bool m_areClientsNotified = false;
54 
56 
58 };
59 
60 } // namespace gk
61 
62 #endif // GK_LIFETIMECOMPONENT_HPP_
void start()
Start the timer.
Definition: Timer.cpp:31
LifetimeComponent(u32 lifetime)
void setClientsNotified(bool areClientsNotified)
LifetimeComponent()=default
LifetimeComponent(DeathChecker deathChecker)
std::function< bool(const SceneObject &)> DeathChecker
unsigned int u32
Definition: IntTypes.hpp:23
void setDeathChecker(DeathChecker deathChecker)
u32 time() const
Get time.
Definition: Timer.cpp:44
Very basic timer.
Definition: Timer.hpp:25
bool dead(const SceneObject &object) const