Thrive
component_collection.h
1 #pragma once
2 
3 #include "engine/component.h"
4 #include "engine/typedefs.h"
5 
6 #include <memory>
7 
8 namespace thrive {
9 
10 class EntityManager;
11 
24 
25 public:
26 
30  using ChangeCallback = std::function<void(EntityId, Component&)>;
31 
36 
40  Component*
42  EntityId entityId
43  ) const;
44 
48  void
49  clear();
50 
55  const std::unordered_map<EntityId, std::unique_ptr<Component>>&
56  components() const;
57 
63  bool
64  empty() const;
65 
76  Component*
77  get(
78  EntityId entityId
79  ) const;
80 
94  unsigned int
96  ChangeCallback onComponentAdded,
97  ChangeCallback onComponentRemoved
98  );
99 
103  ComponentTypeId
104  type() const;
105 
114  void
116  unsigned int id
117  );
118 
119 private:
120 
124  friend class EntityManager;
125 
132  ComponentTypeId type
133  );
134 
149  bool
150  addComponent(
151  EntityId entityId,
152  std::unique_ptr<Component> component
153  );
154 
167  bool
168  removeComponent(
169  EntityId entityId
170  );
171 
172  struct Implementation;
173  std::unique_ptr<Implementation> m_impl;
174 
175 };
176 
177 }
178 
unsigned int registerChangeCallbacks(ChangeCallback onComponentAdded, ChangeCallback onComponentRemoved)
Registers callbacks for when components are added or removed.
Definition: component_collection.cpp:121
Base class for components.
Definition: component.h:31
Manages entities and their components.
Definition: entity_manager.h:23
void clear()
Empties the component collection.
Definition: component_collection.cpp:80
void unregisterChangeCallbacks(unsigned int id)
Unregisters change callbacks.
Definition: component_collection.cpp:158
Component * operator[](EntityId entityId) const
Definition: component_collection.cpp:45
Definition: bullet_ogre_conversion.h:6
bool empty() const
Checks whether this collection is empty.
Definition: component_collection.cpp:101
std::function< void(EntityId, Component &)> ChangeCallback
Callback for when a component has been added or removed.
Definition: component_collection.h:30
~ComponentCollection()
Destructor.
Definition: component_collection.cpp:41
ComponentTypeId type() const
The type id of the collection's components.
Definition: component_collection.cpp:152
const std::unordered_map< EntityId, std::unique_ptr< Component > > & components() const
Returns a reference to the internal component map.
Definition: component_collection.cpp:95
Utility class for the EntityManager.
Definition: component_collection.h:23