3#include "../../../Opengl-Core/include/Core.hpp"
4#include "Component.hpp"
23 using Index =
unsigned int;
31 auto id = this->m_currentId;
34 this->m_entities.emplace(
id,
"Entity " + std::to_string(
id));
35 this->m_ettComponent.emplace(
id, std::vector<Shared<Component>>{});
44 this->m_currentId = 0;
53 std::vector<Index> res{};
54 for (
auto [
id, _] : this->m_ettComponent) {
66 inline std::string
getEntityName(
const Index &
id)
const {
return this->m_entities.at(
id); }
73 inline void setEntityName(
const Index &
id,
const std::string &name) { this->m_entities.at(
id) = std::move(name); }
84 bool removed = this->m_entities.erase(
id);
85 for (
auto c : this->m_ettComponent.at(
id)) {
88 this->m_ettComponent.erase(
id);
89 for (
auto it = this->m_compEntity.begin(); it != this->m_compEntity.end();) {
90 auto &ids = it->second;
91 ids.erase(std::remove(ALL(ids),
id), ids.end());
94 it = this->m_compEntity.erase(it);
108 inline bool isEntityValid(
const Index &
id) {
return this->m_ettComponent.count(
id); }
128 template <
typename T,
typename... Args>
133 auto comp = CreateShared<T>(std::forward<Args>(args)...);
134 this->m_ettComponent.at(
id).emplace_back(comp);
136 auto name = CLASSNAME(T);
137 if (this->m_compEntity.count(name)) {
138 this->m_compEntity.at(name).push_back(
id);
140 this->m_compEntity.emplace(name, std::vector<Index>{});
141 this->m_compEntity.at(name).push_back(
id);
154 template <
typename T>
159 this->m_compEntity.at(CLASSNAME(T)).erase(std::find(ALL(this->m_compEntity.at(CLASSNAME(T))),
id));
162 this->m_ettComponent.at(
id).erase(std::find(ALL(this->m_ettComponent.at(
id)), c));
173 template <
typename T>
175 return this->m_compEntity.count(CLASSNAME(T)) > 0 &&
176 std::find(ALL(this->m_compEntity.at(CLASSNAME(T))),
id) != this->m_compEntity.at(CLASSNAME(T)).end();
186 template <
typename T>
188 if (!this->m_compEntity.count(CLASSNAME(T)))
191 return this->m_compEntity.at(CLASSNAME(T));
202 template <
typename T>
204 if (!this->m_compEntity.count(CLASSNAME(T)))
208 if (std::find(ALL(this->m_compEntity.at(CLASSNAME(T))),
id) == this->m_compEntity.at(CLASSNAME(T)).end()) {
214 return std::dynamic_pointer_cast<T>(*std::find_if(ALL(this->m_ettComponent.at(
id)), [](
auto e) {
215 return std::dynamic_pointer_cast<T>(e) != nullptr;
230 if (s_pointer ==
nullptr) {
231 Shared<EntityManager> copy(
new EntityManager());
232 copy.swap(s_pointer);
239 Index m_currentId = 0;
242 std::map<Index, std::string> m_entities{};
244 std::map<Index, std::vector<Shared<Component>>> m_ettComponent{};
246 std::map<std::string, std::vector<Index>> m_compEntity{};
249 inline static Shared<EntityManager> s_pointer =
nullptr;
252 EntityManager() =
default;
255#include "../../../Opengl-Core/include/Core.hpp"
This class manages all the entities and the components instanced.
Definition EntityManager.hpp:21
Shared< T > addComponent(const Index &id, Args &&...args)
Adds a component to the given entity. It instances a shared pointer of that component and calls the c...
Definition EntityManager.hpp:129
bool removeEntity(const Index &id)
Removes the entity given from the ECS.
Definition EntityManager.hpp:83
void resetIndex()
Reset the ECS id, it is usually used when entities are cleaned.
Definition EntityManager.hpp:43
void setEntityName(const Index &id, const std::string &name)
Sets the name of the entity given.
Definition EntityManager.hpp:73
Shared< T > getComponentFromId(const Index &id)
Retrieves a shared pointer to the component of an entity.
Definition EntityManager.hpp:203
std::vector< Index > getEntitiesFromComponent() const
Retrieves all the entities that have the given component.
Definition EntityManager.hpp:187
bool entityHasComponent(const Index &id)
Checks if the given entity has the component.
Definition EntityManager.hpp:174
std::vector< Index > getEntities() const
Retrieves a vector containing all entities in the ECS.
Definition EntityManager.hpp:52
std::string getEntityName(const Index &id) const
Retrieves the entity name.
Definition EntityManager.hpp:66
static Shared< EntityManager > instance()
Retrieves the instance of the EntityManager. If it's not instanced, it will be instanced automaticall...
Definition EntityManager.hpp:229
bool removeComponent(const Index &id)
Removes a component from the given entity.
Definition EntityManager.hpp:155
unsigned int getEntitiesCount() const
Retrieves the number of entities in the ECS.
Definition EntityManager.hpp:115
Index createEntity()
Create an entity and increment the current index.
Definition EntityManager.hpp:30
bool isEntityValid(const Index &id)
Checks if the entity given has components.
Definition EntityManager.hpp:108