12#ifndef ECS__COMPONENT_MANAGER_H__
13#define ECS__COMPONENT_MANAGER_H__
75 static const char* COMPONENT_TYPE_NAME{
typeid(T).name() };
76 return COMPONENT_TYPE_NAME;
82 object->~IComponent();
84 this->DestroyObject(
object);
142 template<
class T,
class ...ARGS>
146 static constexpr eastl::hash<ComponentId> ENTITY_COMPONENT_ID_HASHER { eastl::hash<ComponentId>() };
151 void* pObjectMemory = GetComponentContainer<T>()->CreateObject();
153 const ComponentId componentId = this->AqcuireComponentId((T*)pObjectMemory);
154 ((T*)pObjectMemory)->m_ComponentID = componentId;
157 IComponent* component =
new (pObjectMemory)T(std::forward<ARGS>(args)...);
160 component->
m_HashValue = ENTITY_COMPONENT_ID_HASHER(entityId) ^ (ENTITY_COMPONENT_ID_HASHER(componentId) << 1);
163 MapEntityComponent(entityId, componentId, CTID);
167 return static_cast<T*
>(component);
190 const ComponentId componentId = this->m_EntityComponentMap[entityId.
index][CTID];
191 assert(componentId != INVALID_COMPONENT_ID &&
"FATAL: Trying to remove a component which is not used by this entity!");
194 IComponent* component = this->m_ComponentLUT[componentId];
196 assert(component !=
nullptr &&
"FATAL: Trying to remove a component which is not used by this entity!");
199 GetComponentContainer<T>()->DestroyObject(component);
202 UnmapEntityComponent(entityId, componentId, CTID);
209 static const size_t NUM_COMPONENTS = this->m_EntityComponentMap[0].size();
211 for (
ComponentTypeId componentTypeId = 0; componentTypeId < NUM_COMPONENTS; ++componentTypeId)
213 const ComponentId componentId = this->m_EntityComponentMap[entityId.
index][componentTypeId];
214 if (componentId == INVALID_COMPONENT_ID)
217 IComponent* component = this->m_ComponentLUT[componentId];
218 if (component !=
nullptr)
221 const auto it = this->m_ComponentContainerRegistry.find(componentTypeId);
222 if (it != this->m_ComponentContainerRegistry.end())
223 it->second->DestroyComponent(component);
225 assert(
false &&
"Trying to release a component that wasn't created by ComponentManager!");
228 UnmapEntityComponent(entityId, componentId, componentTypeId);
234 static const size_t NUM_COMPONENTS = this->m_EntityComponentMap[0].size();
236 for (
ComponentTypeId componentTypeId = 0; componentTypeId < NUM_COMPONENTS; ++componentTypeId)
238 const ComponentId componentId = this->m_EntityComponentMap[entityId.
index][componentTypeId];
239 if (componentId == INVALID_COMPONENT_ID)
242 if (
IComponent* component = this->m_ComponentLUT[componentId]) {
243 component->OnData(data);
270 const ComponentId componentId = this->m_EntityComponentMap[entityId.
index][CTID];
273 if (componentId == INVALID_COMPONENT_ID)
276 return static_cast<T*
>(this->m_ComponentLUT[componentId]);
298 const auto it = this->m_ComponentContainerRegistry.find(CID);
301 if (it == this->m_ComponentContainerRegistry.end())
304 this->m_ComponentContainerRegistry[CID] = cc;
309 assert(cc !=
nullptr &&
"Failed to create ComponentContainer<T>!");
332 return GetComponentContainer<T>()->begin();
354 return GetComponentContainer<T>()->end();
358 inline size_t size() const noexcept {
359 return GetComponentContainer<T>()->size();
371 return mgr->
AddComponent<T>( id, std::forward<P>( param )... );
#define COMPONENT_T_CHUNK_SIZE
ComponentContainer(const ComponentContainer &)=delete
virtual void DestroyComponent(IComponent *object) override
virtual const char * GetComponentContainerTypeName() const override
virtual ~ComponentContainer()
ComponentContainer & operator=(ComponentContainer &)=delete
virtual const char * GetComponentContainerTypeName() const =0
virtual ~IComponentContainer()
virtual void DestroyComponent(IComponent *object)=0
TComponentIterator< T > begin()
void PassDataToAllComponents(const EntityId entityId, const CustomEvent &data)
T * AddComponent(const EntityId entityId, ARGS &&... args)
ComponentManager & operator=(ComponentManager &)=delete
ComponentContainerRegistry m_ComponentContainerRegistry
size_t size() const noexcept
EntityComponentMap m_EntityComponentMap
eastl::vector< eastl::vector< ComponentId > > EntityComponentMap
eastl::vector< IComponent * > ComponentLookupTable
ComponentContainer< T > * GetComponentContainer()
typename ComponentContainer< T >::iterator TComponentIterator
ComponentLookupTable m_ComponentLUT
T * GetComponent(const EntityId entityId)
void RemoveAllComponents(const EntityId entityId)
ComponentManager(const ComponentManager &)=delete
void RemoveComponent(const EntityId entityId)
eastl::unordered_map< ComponentTypeId, IComponentContainer * > ComponentContainerRegistry
TComponentIterator< T > end()
T * AddComponent(ComponentManager *mgr, const EntityId id, P &&... param)
T * GetComponent(ComponentManager *mgr, const EntityId id)
void RemoveComponent(ComponentManager *mgr, const EntityId id)
static std::atomic_bool s_ComponentsChanged