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