Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
Entity.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_ENTITY_H
22 #define GF_ENTITY_H
23 
24 #include "Portability.h"
25 
26 namespace gf {
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
28 inline namespace v1 {
29 #endif
30 
31  class RenderTarget;
32 
33  /**
34  * @ingroup game
35  * @brief A game entity
36  *
37  * gf::Entity represents a game entity, i.e. an object that is updated
38  * and rendered every frame.
39  *
40  * Entities are ordered by priority when they are renderer, with lower
41  * priority being rendered first and higher priority being renderered
42  * last. The default priority is @f$ 0 @f$.
43  *
44  * Entities can become dead, in which case they are neither updated nor
45  * rendered anymore.
46  *
47  * Entities can be grouped in an gf::EntityContainer.
48  *
49  * @sa gf::EntityContainer
50  */
51  class GF_API Entity {
52  public:
53  /**
54  * @brief Contructor
55  *
56  * By default, the entity is alive.
57  *
58  * @param priority the priority of the entity (default: 0)
59  */
60  Entity(int priority = 0)
61  : m_priority(priority)
62  , m_liveness(Liveness::ALIVE)
63  {
64  }
65 
66  /**
67  * @brief Destructor
68  */
69  virtual ~Entity();
70 
71  /**
72  * @brief Get the priority of the entity
73  *
74  * @return The priority
75  */
76  int getPriority() const {
77  return m_priority;
78  }
79 
80  /**
81  * @name Entity liveness
82  * @{
83  */
84 
85  /**
86  * @brief Check if the entity is alive
87  *
88  * @return True if the entity is alive
89  * @sa setAlive(), kill();
90  */
91  bool isAlive() const {
92  return m_liveness == Liveness::ALIVE;
93  }
94 
95  /**
96  * @brief Set the entity alive (again)
97  *
98  * This function can be called to revive an entity that has been killed.
99  */
100  void setAlive() {
101  m_liveness = Liveness::ALIVE;
102  }
103 
104  /**
105  * @brief Kill an entity
106  *
107  * This function kills the entity. After being killed, the entity must
108  * not be updated or rendered.
109  */
110  void kill() {
111  m_liveness = Liveness::DEAD;
112  }
113 
114  /** @} */
115 
116  /**
117  * @brief Update the entity's state
118  *
119  * Entities are updated each frame. The time between two frames is
120  * given as a parameter to help in the update. This time is in
121  * seconds.
122  *
123  * @param dt The time (in seconds) since the last update
124  * @sa gf::EntityContainer::update()
125  */
126  virtual void update(float dt);
127 
128  /**
129  * @brief Render the entity
130  *
131  * @param target The render target
132  * @sa gf::EntityContainer::render()
133  */
134  virtual void render(RenderTarget& target);
135 
136  private:
137  enum class Liveness : int {
138  ALIVE,
139  DEAD,
140  };
141 
142  int m_priority;
143  Liveness m_liveness;
144  };
145 
146 #ifndef DOXYGEN_SHOULD_SKIP_THIS
147 }
148 #endif
149 }
150 
151 #endif // GF_ENTITY_H
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
bool isAlive() const
Check if the entity is alive.
Definition: Entity.h:91
virtual void update(float dt)
Update the entity's state.
Entity(int priority=0)
Contructor.
Definition: Entity.h:60
A game entity.
Definition: Entity.h:51
virtual ~Entity()
Destructor.
Definition: Action.h:34
void setAlive()
Set the entity alive (again)
Definition: Entity.h:100
#define GF_API
Definition: Portability.h:35
virtual void render(RenderTarget &target)
Render the entity.
void kill()
Kill an entity.
Definition: Entity.h:110
int getPriority() const
Get the priority of the entity.
Definition: Entity.h:76