hx3d  1
2D/3D Simple Game Framework
engine.hpp
1 /*
2  Entity Component System: Base Engine.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #ifndef HX3D_ECS_ENGINE
22 #define HX3D_ECS_ENGINE
23 
24 #include "hx3d/ecs/system.hpp"
25 #include "hx3d/ecs/entity.hpp"
26 #include "hx3d/ecs/component.hpp"
27 
28 #include "hx3d/utils/log.hpp"
29 #include "hx3d/utils/ptr.hpp"
30 
31 #include <map>
32 #include <typeindex>
33 #include <functional>
34 
35 namespace hx3d {
36 namespace ecs {
37 
41 class Engine {
42 
43 public:
44  Engine();
45 
46  // ENTITY ///////////////////////
47 
54 
60  void registerEntity(const Ptr<Entity>& entity);
61 
69  void removeEntity(const Ptr<Entity>& entity);
70 
71  // COMPONENTS /////////////////////
72 
80  template <class T>
81  Ptr<T> getComponent(const Ptr<Entity>& entity);
82 
89  template <class T>
90  void addComponent(const Ptr<Entity>& entity, const Ptr<Component>& component);
91 
98  template <class T, class... Args>
99  void createComponent(const Ptr<Entity>& entity, Args... args);
100 
101  // SYSTEM /////////////////////////
102 
108  template <class T>
109  void addSystem(const Ptr<System>& sys);
110 
116  template <class T, class... Args>
117  void createSystem(Args... args);
118 
119  // UTILS //////////////////////////
120 
128  unsigned int getComponentSize(const Ptr<Entity>& entity);
129 
135  unsigned int getEntityCount();
136 
144  unsigned int getBits(const Ptr<Entity>& entity);
145 
151  template <class T>
152  void registerComponentAdded(std::function<void(const Ptr<Component>&, const Ptr<Entity>&)> callback);
153 
159  template <class T>
160  void registerComponentRemoved(std::function<void(const Ptr<Component>&, const Ptr<Entity>&)> callback);
161 
167  void update(const float delta);
168 
172  void clear();
173 
174 private:
176  std::map<unsigned int, Ptr<Entity>> _entities;
178  std::map<unsigned int, std::map<std::type_index, Ptr<Component>>> _components;
180  std::map<unsigned int, Bitset> _bits;
181 
183  std::map<std::type_index, Ptr<System>> _systems;
185  std::map<std::type_index, std::function<void(Ptr<Component>, Ptr<Entity>)>> _onComponentAdded;
187  std::map<std::type_index, std::function<void(Ptr<Component>, Ptr<Entity>)>> _onComponentRemoved;
188 
190  std::vector<unsigned int> _toRemove;
191 
195  void cleanEntities();
196 
202  unsigned int lastEntityAvailable();
203 
210  template <class T>
211  void addInternalComponent(const unsigned int entityId, const Ptr<Component>& component);
212 
218  void removeComponents(const unsigned int entityId);
219 };
220 
221 } /* ecs */
222 } /* hx3d */
223 
224 #include "hx3d/ecs/_inline/engine.inl.hpp"
225 
226 #endif
Manages entity in a world.
Definition: engine.hpp:41
void createSystem(Args...args)
Create a system into the engine.
Definition: engine.inl.hpp:88
hx3d framework namespace
Definition: audio.hpp:26
void registerComponentAdded(std::function< void(const Ptr< Component > &, const Ptr< Entity > &)> callback)
Register a callback for a certain component type when it&#39;s added.
Definition: engine.inl.hpp:96
void clear()
Remove everything from the engine.
Definition: engine.cpp:122
void addComponent(const Ptr< Entity > &entity, const Ptr< Component > &component)
Add a component for an entity.
Definition: engine.inl.hpp:42
void update(const float delta)
Update the engine.
Definition: engine.cpp:72
void createComponent(const Ptr< Entity > &entity, Args...args)
Create a component for an entity with variable args.
Definition: engine.inl.hpp:47
Ptr< Entity > createEntity()
Create a new entity with the last entity id available.
Definition: engine.cpp:28
void removeEntity(const Ptr< Entity > &entity)
Mark the entity for deletion from the engine.
Definition: engine.cpp:49
unsigned int getComponentSize(const Ptr< Entity > &entity)
Get the number of components for an entity.
Definition: engine.cpp:53
unsigned int getEntityCount()
Get the number of entities.
Definition: engine.cpp:63
void registerComponentRemoved(std::function< void(const Ptr< Component > &, const Ptr< Entity > &)> callback)
Register a callback for a certain component type when it&#39;s removed.
Definition: engine.inl.hpp:101
unsigned int getBits(const Ptr< Entity > &entity)
Get the bits corresponding to the entity components.
Definition: engine.cpp:67
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
void registerEntity(const Ptr< Entity > &entity)
Affect an ID to a uninitialized entity.
Definition: engine.cpp:37
Ptr< T > getComponent(const Ptr< Entity > &entity)
Get the component for an entity.
Definition: engine.inl.hpp:25
void addSystem(const Ptr< System > &sys)
Add a system to the engine.
Definition: engine.inl.hpp:80