Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ComponentManager.cpp
Go to the documentation of this file.
1
2/*
3 Author : Tobias Stein
4 Date : 7th September, 2017
5 File : ComponentManager.cpp
6
7 Manages all component container.
8
9 All Rights Reserved. (c) Copyright 2016.
10*/
11
12
13#include "ComponentManager.h"
14
15namespace ECS {
16
18 {
19 DEFINE_LOGGER("ComponentManager");
20 LOG_INFO("Initialize ComponentManager!","");
21
22 const size_t NUM_COMPONENTS { util::Internal::FamilyTypeID<IComponent>::Get() };
23
25 for (auto i = 0; i < ENITY_LUT_GROW; ++i)
26 this->m_EntityComponentMap[i].resize(NUM_COMPONENTS, INVALID_COMPONENT_ID);
27 }
28
30 {
31 for (auto cc : this->m_ComponentContainerRegistry)
32 {
33 LOG_DEBUG("Releasing remaining entities of type '{}' ...", cc.second->GetComponentContainerTypeName());
34 delete cc.second;
35 cc.second = nullptr;
36 }
37
38 LOG_INFO("Release ComponentManager!","");
39 }
40
42 {
43 size_t i = 0;
44 for (; i < this->m_ComponentLUT.size(); ++i)
45 {
46 if (this->m_ComponentLUT[i] == nullptr)
47 {
48 this->m_ComponentLUT[i] = component;
49 return i;
50 }
51 }
52
53 // increase component LUT size
54 this->m_ComponentLUT.resize(this->m_ComponentLUT.size() + COMPONENT_LUT_GROW, nullptr);
55
56 this->m_ComponentLUT[i] = component;
57 return i;
58 }
59
61 {
62 assert((id != INVALID_COMPONENT_ID && id < this->m_ComponentLUT.size()) && "Invalid component id");
63 this->m_ComponentLUT[id] = nullptr;
64 }
65
66 void ComponentManager::MapEntityComponent(EntityId entityId, ComponentId componentId, ComponentTypeId componentTypeId)
67 {
68 static const size_t NUM_COMPONENTS { util::Internal::FamilyTypeID<IComponent>::Get() };
69
70 if ((this->m_EntityComponentMap.size() - 1) < entityId.index)
71 {
72 size_t oldSize = this->m_EntityComponentMap.size();
73
74 // we scale this map size along the entity lookup table size
75 size_t newSize = oldSize + ENITY_LUT_GROW;
76
77 this->m_EntityComponentMap.resize(newSize);
78
79 for (auto i = oldSize; i < newSize; ++i)
80 this->m_EntityComponentMap[i].resize(NUM_COMPONENTS, INVALID_COMPONENT_ID);
81 }
82
83 // create mapping
84 this->m_EntityComponentMap[entityId.index][componentTypeId] = componentId;
85 }
86
88 {
89 assert(this->m_EntityComponentMap[entityId.index][componentTypeId] == componentId && "FATAL: Entity Component ID mapping corruption!");
90
91 // free mapping
92 this->m_EntityComponentMap[entityId.index][componentTypeId] = INVALID_COMPONENT_ID;
93
94 // free component id
95 this->ReleaseComponentId(componentId);
96 }
97
98} // namespace ECS
#define COMPONENT_LUT_GROW
Definition: API.h:24
#define ENITY_LUT_GROW
Definition: API.h:20
#define DEFINE_LOGGER(name)
Definition: LoggerMacro.h:18
#define LOG_INFO(format,...)
Definition: LoggerMacro.h:25
#define LOG_DEBUG(format,...)
Definition: LoggerMacro.h:24
void UnmapEntityComponent(EntityId entityId, ComponentId componentId, ComponentTypeId componentTypeId)
ComponentContainerRegistry m_ComponentContainerRegistry
void ReleaseComponentId(ComponentId id)
EntityComponentMap m_EntityComponentMap
ComponentLookupTable m_ComponentLUT
void MapEntityComponent(EntityId entityId, ComponentId componentId, ComponentTypeId componentTypeId)
ComponentId AqcuireComponentId(IComponent *component)
static const ComponentId INVALID_COMPONENT_ID
Definition: IComponent.h:24
ObjectID ComponentId
Definition: IComponent.h:21
TypeID ComponentTypeId
Definition: IComponent.h:22