6 #include <unordered_map>
9 #include "ComponentArray.hpp"
10 #include "SystemBase.hpp"
22 typedef std::unordered_map<ComponentTypeID, IComponentDataArray*> CompIdArrayMap;
23 typedef std::unordered_map<EntityID, Archetype> EntityArchMap;
24 typedef std::unordered_map<SystemID, Ref<SystemBase>> SystemMap;
27 EntityID m_entityIdCounter;
28 SystemID m_systemIdCounter;
30 CompIdArrayMap m_componentMap;
32 EntityArchMap m_entityArch;
38 ECS() : m_systemIdCounter(0), m_entityIdCounter(1) { }
42 m_componentMap.clear();
49 EntityID
id = GetNewID();
50 m_entityArch[id] = Archetype().reset();
59 ComponentTypeID compId = GetTypeID<C>();
60 if (!IsComponentRegistered<C>()){
61 Log::printStackTrace();
62 throw std::runtime_error(
"Component Not Registered!");
67 m_entityArch[id].set(compId, 0);
74 return m_entityArch[id];
81 ComponentTypeID compId = GetTypeID<C>();
82 if (!IsComponentRegistered<C>()){
83 Log::printStackTrace();
84 throw std::runtime_error(
"Component Not Registered!");
96 m_entityArch.erase(
id);
97 for (
auto itr : m_componentMap) {
98 itr.second->EntityDestroyed(
id);
105 template<
class C,
typename... Args>
107 ComponentTypeID compId = GetTypeID<C>();
108 if (!IsComponentRegistered<C>()){
109 Log::printStackTrace();
110 throw std::runtime_error(
"Component Not Registered!");
114 m_entityArch[id].set(compId,
true);
116 return arr->
Insert(
id, args);
122 template<
class C,
typename... Args>
124 ComponentTypeID compId = GetTypeID<C>();
125 if (!IsComponentRegistered<C>()){
126 Log::printStackTrace();
127 throw std::runtime_error(
"Component Not Registered!");
131 m_entityArch[id].set(compId,
true);
133 return arr->
Insert(
id, C(std::forward<Args>(args)...));
141 s->m_id = m_systemIdCounter++;
142 m_systems[s->m_id] = s;
159 for (
auto& itr : m_systems) {
160 if (itr.second->m_arch == a) {
166 template<
class... Cs>
172 for(
auto b : std::vector<bool>{{IsComponentRegistered<Cs>()...}}) {
174 Log::Err(
"ECS",
"Oh no compnent not registered");
177 std::vector<ComponentTypeID> ids = {{GetTypeID<Cs>()...}};
178 for (
auto& i : ids) {
187 template<
class... Cs>
190 std::vector<EntityID> ret;
191 for (
auto& itr : m_entityArch) {
192 auto bits = itr.second;
194 if ((itr.second & a) == a) {
195 ret.push_back(itr.first);
204 std::vector<EntityID> ret;
205 for (
auto& itr : m_entityArch) {
206 auto bits = itr.second;
208 if ((itr.second & a) == a) {
209 ret.push_back(itr.first);
219 ComponentTypeID typeId = GetTypeID<C>();
222 if (m_componentMap.find(typeId) != m_componentMap.end())
232 ComponentTypeID typeId = GetTypeID<C>();
233 return m_componentMap.find(typeId) != m_componentMap.end();
242 const ComponentTypeID compId = GetTypeID<C>();
243 if (!IsComponentRegistered<C>()){
244 throw std::runtime_error(
"Component Not Registered!");
248 return (ComponentDataArray<C>*) m_componentMap[compId];
253 EntityID GetNewID() {
254 return m_entityIdCounter++;
258 ComponentTypeID GetTypeID() {
259 return TypeIdGenerator<ComponentBase>::GetTypeID<C>();