Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
EntityContainer.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_CONTAINER_H
22 #define GF_ENTITY_CONTAINER_H
23 
24 #include <type_traits>
25 #include <vector>
26 
27 #include "Portability.h"
28 #include "RenderStates.h"
29 #include "Time.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
36  class Entity;
37  class RenderTarget;
38 
39  /**
40  * @ingroup game
41  * @brief A collection of entities
42  *
43  * gf::EntityContainer represents a collection of entities that are
44  * updated and rendered automatically. The entity manager takes care of
45  * the liveness of the entities and remove the dead entities from the
46  * collection.
47  *
48  * The entity manager is *not* responsible for the memory of the entities.
49  * The entities must be allocated by the user and not deleted while they
50  * are handled by the entity manager.
51  *
52  * Generally, you only need one entity manager in your game. You create it
53  * at the beginning of the game and put all your entities in it. Then you
54  * can call gf::EntityContainer::update() and gf::EntityContainer::render() in your
55  * game loop.
56  *
57  * @sa gf::Entity
58  */
60  public:
61 
62  /**
63  * @brief Update the entities
64  *
65  * This function first eliminates the dead entities, then sort them
66  * by priority. Finally, each entity is updated.
67  *
68  * @param time The time since the last update
69  * @sa gf::Entity::update()
70  */
71  void update(Time time);
72 
73  /**
74  * @brief Render the entities on the target
75  *
76  * The entities are rendered by priority: lower priority first and
77  * higher priority last.
78  *
79  * @param target The render target
80  * @param states The render states to use for drawing
81  * @sa gf::Entity::render()
82  */
83  void render(RenderTarget& target, const RenderStates& states = RenderStates());
84 
85  /**
86  * @name Entities management
87  * @{
88  */
89 
90  /**
91  * @brief Add an entity to the collection
92  *
93  * The entity must not be deleted while it is handled by the entity
94  * manager.
95  *
96  * @param entity An entity
97  * @sa removeEntity()
98  */
99  void addEntity(Entity& entity);
100 
101  /**
102  * @brief Remove an entity from the collection
103  *
104  * @param entity The entity to remove
105  * @return The removed entity or `nullptr` if the entity was not present
106  * @sa addEntity()
107  */
108  Entity *removeEntity(Entity *entity);
109 
110  /**
111  * @brief Remove a typed entity from the collection
112  *
113  * This function is a shortcut to avoid typecasting.
114  *
115  * Example:
116  *
117  * ~~~{.cc}
118  *
119  * class Foo : public Entity {
120  * ...
121  * };
122  *
123  * Foo foo;
124  * manager.addEntity(foo);
125  *
126  * ...
127  *
128  * Foo *removed = manager.removeTypedEntity<Foo>(&foo);
129  * removed->doSomething();
130  * ~~~
131  *
132  * @sa removeEntity()
133  */
134  template<typename E>
135  E *removeTypedEntity(E *entity) {
136  static_assert(std::is_base_of<Entity, E>::value, "E must be an Entity");
137  return static_cast<E*>(removeEntity(entity));
138  }
139 
140  /** @} */
141 
142  private:
143  std::vector<Entity *> m_entities;
144  };
145 
146 #ifndef DOXYGEN_SHOULD_SKIP_THIS
147 }
148 #endif
149 }
150 
151 #endif // GF_ENTITY_CONTAINER_H
void update(Time time)
Update the entities.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Represents a time value.
Definition: Time.h:73
A game entity.
Definition: Entity.h:53
The namespace for gf classes.
Definition: Action.h:34
void render(RenderTarget &target, const RenderStates &states=RenderStates())
Render the entities on the target.
void addEntity(Entity &entity)
Add an entity to the collection.
Entity * removeEntity(Entity *entity)
Remove an entity from the collection.
#define GF_API
Definition: Portability.h:35
E * removeTypedEntity(E *entity)
Remove a typed entity from the collection.
Definition: EntityContainer.h:135
A collection of entities.
Definition: EntityContainer.h:59