Thrive
entity_manager.h
1 #pragma once
2 
3 #include "engine/typedefs.h"
4 #include "util/make_unique.h"
5 
6 #include <memory>
7 #include <unordered_set>
8 
9 namespace thrive {
10 
11 class Component;
12 class ComponentCollection;
13 class ComponentFactory;
14 class GameState;
15 class StorageContainer;
16 
24 
25 public:
26 
30  EntityManager();
31 
36 
51  Component*
53  EntityId entityId,
54  std::unique_ptr<Component> component
55  );
56 
72  template<typename C>
73  C*
75  EntityId entityId,
76  std::unique_ptr<C> component
77  ) {
78  return static_cast<C*>(
79  this->addComponent(
80  entityId,
81  std::unique_ptr<Component>(std::move(component))
82  )
83  );
84  }
85 
91  void
92  clear();
93 
97  std::unordered_set<EntityId>
98  entities();
99 
107  EntityId
108  generateNewId();
109 
122  Component*
123  getComponent(
124  EntityId entityId,
125  ComponentTypeId typeId
126  );
127 
147  template<class ComponentType>
148  ComponentType*
150  EntityId entityId
151  ) {
152  Component* component = this->getComponent(
153  entityId,
154  ComponentType::TYPE_ID
155  );
156  return static_cast<ComponentType*>(component);
157  }
158 
168  ComponentTypeId typeId
169  );
170 
187  EntityId
188  getNamedId(
189  const std::string& name,
190  bool forceNew = false
191  );
192 
211  template<typename C, typename... Args>
212  C*
214  EntityId id,
215  Args&&... args
216  ) {
217  auto component = this->getComponent<C>(id);
218  if (not component) {
219  auto newComponent = make_unique<C>(args...);
220  component = newComponent.get();
221  this->addComponent(id, std::move(newComponent));
222  }
223  return component;
224  }
225 
234  bool
235  exists(
236  EntityId entityId
237  ) const;
238 
244  std::unordered_set<ComponentTypeId>
245  nonEmptyCollections() const;
246 
256  bool
257  isVolatile(
258  EntityId id
259  ) const;
260 
267  void
268  stealName(
269  EntityId entityId,
270  const std::string& name
271  );
272 
273 
284  storeEntity(
285  EntityId entityId
286  ) const;
287 
300  EntityId
301  loadEntity(
303  const ComponentFactory& componentFactory
304  );
305 
309  void
310  processRemovals();
311 
315  void
317 
331  void
333  EntityId entityId,
334  ComponentTypeId typeId
335  );
336 
348  void
349  removeEntity(
350  EntityId entityId
351  );
352 
366  void
367  addChild(
368  EntityId child,
369  EntityId parent
370  );
371 
379  bool
380  hasChildren(
381  EntityId entityId
382  ) const;
383 
393  const std::string*
395  EntityId entityId
396  );
397 
406  void
407  restore(
408  const StorageContainer& storage,
409  const ComponentFactory& factory
410  );
411 
418  void
419  setVolatile(
420  EntityId id,
421  bool isVolatile
422  );
423 
433  storage(
434  const ComponentFactory& factory
435  ) const;
436 
437 private:
438 
439  friend class Engine;
440 
457  void
458  transferEntity(
459  EntityId oldEntityId,
460  EntityId newEntityId,
461  EntityManager& newEntityManager,
462  const ComponentFactory& componentFactory
463  );
464 
465  struct Implementation;
466  std::unique_ptr<Implementation> m_impl;
467 };
468 
469 }
C * getOrCreateComponent(EntityId id, Args &&...args)
Retrieves a component, creating it if necessary.
Definition: entity_manager.h:213
void removeComponent(EntityId entityId, ComponentTypeId typeId)
Removes a component.
Definition: entity_manager.cpp:227
void processRemovals()
Removes all components queued for removal.
Definition: entity_manager.cpp:188
void setVolatile(EntityId id, bool isVolatile)
Sets the volatile flag for an entity.
Definition: entity_manager.cpp:375
EntityManager()
Constructor.
Definition: entity_manager.cpp:52
void removeEntity(EntityId entityId)
Removes all components of an entity.
Definition: entity_manager.cpp:235
Base class for components.
Definition: component.h:31
bool hasChildren(EntityId entityId) const
Returns whether an entity has any children.
Definition: entity_manager.cpp:354
ComponentCollection & getComponentCollection(ComponentTypeId typeId)
Returns a component collection.
Definition: entity_manager.cpp:128
Manages entities and their components.
Definition: entity_manager.h:23
EntityId getNamedId(const std::string &name, bool forceNew=false)
Returns the id of a named entity.
Definition: entity_manager.cpp:136
A factory for components.
Definition: component_factory.h:19
void processTransfers()
Transfers all components queued for transfer.
void clear()
Removes all components.
Definition: entity_manager.cpp:81
const std::string * getNameMappingFor(EntityId entityId)
Gets the name for a given entityId if it exists.
Definition: entity_manager.cpp:362
void restore(const StorageContainer &storage, const ComponentFactory &factory)
Restores the entity manager from a storage container.
Definition: entity_manager.cpp:298
std::unordered_set< EntityId > entities()
Returns a set of entity ids that have at least one components.
Definition: entity_manager.cpp:94
Component * getComponent(EntityId entityId, ComponentTypeId typeId)
Retrieves a component.
Definition: entity_manager.cpp:118
Definition: bullet_ogre_conversion.h:6
C * addComponent(EntityId entityId, std::unique_ptr< C > component)
Adds a component.
Definition: entity_manager.h:74
Component * addComponent(EntityId entityId, std::unique_ptr< Component > component)
Adds a component.
Definition: entity_manager.cpp:61
void stealName(EntityId entityId, const std::string &name)
Rebinds a name to this entity.
Definition: entity_manager.cpp:167
bool exists(EntityId entityId) const
Checks whether an entity exists.
Definition: entity_manager.cpp:104
ComponentType * getComponent(EntityId entityId)
Convenience template overload.
Definition: entity_manager.h:149
void addChild(EntityId child, EntityId parent)
Sets an entity as another entities child.
Definition: entity_manager.cpp:343
std::unordered_set< ComponentTypeId > nonEmptyCollections() const
Returns the set of non-empty collection ids.
Definition: entity_manager.cpp:176
EntityId generateNewId()
Generates a new, unique entity id.
Definition: entity_manager.cpp:112
The heart of the game.
Definition: engine.h:55
StorageContainer storage(const ComponentFactory &factory) const
Serializes the current non-volatile components into a storage container.
Definition: entity_manager.cpp:388
A key-value storage for serialization.
Definition: serialization.h:36
EntityId loadEntity(StorageContainer storage, const ComponentFactory &componentFactory)
Loads an entity into this entity manager.
Definition: entity_manager.cpp:284
Utility class for the EntityManager.
Definition: component_collection.h:23
~EntityManager()
Destructor.
Definition: entity_manager.cpp:57
bool isVolatile(EntityId id) const
Returns the volatile flag for an entity.
Definition: entity_manager.cpp:160
StorageContainer storeEntity(EntityId entityId) const
Stores a single entity.
Definition: entity_manager.cpp:265