5#include <unordered_map>
8#include "compact_list.hpp"
9#include "sparse_list.hpp"
11template <
typename T,
typename... Types>
21 explicit Entity(
const unsigned i) : id_(i) {}
22 explicit operator unsigned() {
return id_; }
57 template <
typename... T>
60 Entity e = addEntityEmpty(parent, editor_name);
83 if (t == ComponentListType::kSparse) {
84 component_lists_.emplace(
typeid(T).hash_code(),
85 std::make_unique<ComponentListSparse<T>>());
87 component_lists_.emplace(
typeid(T).hash_code(),
88 std::make_unique<ComponentListCompact<T>>());
98 auto comp_base = component_lists_.find(
typeid(T).hash_code());
99 if (comp_base->second.get()->type_ == ComponentListType::kSparse) {
100 ComponentListSparse<T>* component_vector =
101 static_cast<ComponentListSparse<T>*
>(comp_base->second.get());
102 component_vector->components_[entity.id_ - 1].emplace(component);
105 ComponentListCompact<T>* component_vector =
106 static_cast<ComponentListCompact<T>*
>(comp_base->second.get());
107 component_vector->setComponent(entity.id_, component);
116 template <
typename T>
118 auto comp_base = component_lists_.find(
typeid(T).hash_code());
119 if (comp_base->second.get()->type_ == ComponentListType::kSparse) {
120 ComponentListSparse<T>* component_vector =
121 static_cast<ComponentListSparse<T>*
>(comp_base->second.get());
122 return &component_vector->components_[entity.id_ - 1].value();
125 ComponentListCompact<T>* component_vector =
126 static_cast<ComponentListCompact<T>*
>(comp_base->second.get());
127 return component_vector->getComp(
static_cast<unsigned>(entity));
136 template <
typename T>
138 auto comp_base = component_lists_.find(
typeid(T).hash_code());
139 if (comp_base->second.get()->type_ == ComponentListType::kSparse) {
140 ComponentListSparse<T>* component_vector =
141 static_cast<ComponentListSparse<T>*
>(comp_base->second.get());
142 if(component_vector->components_[entity - 1].has_value()) {
143 return &component_vector->components_[entity - 1].value();
148 ComponentListCompact<T>* component_vector =
149 static_cast<ComponentListCompact<T>*
>(comp_base->second.get());
150 return component_vector->getComp(entity);
160 template <
typename T>
162 auto comp_base = component_lists_.find(
typeid(T).hash_code());
163 ComponentListCompact<T>* component_vector =
164 static_cast<ComponentListCompact<T>*
>(comp_base->second.get());
165 return *component_vector;
177 template <
typename T>
179 auto comp_base = component_lists_.find(
typeid(T).hash_code());
180 ComponentListSparse<T>* component_vector =
181 static_cast<ComponentListSparse<T>*
>(comp_base->second.get());
182 return *component_vector;
189 Entity addEntityEmpty(
Entity* parent,
const std::string& editor_name);
191 void innerDelete(
unsigned i);
193 RenderTreeCmp* getRender(
unsigned ent)
const;
196 std::unordered_map<std::size_t, std::unique_ptr<ComponentListBase>>
201 std::vector<unsigned> freed_entities_;
204 unsigned current_entity_;
Helper to iterate several component lists simultaneously.
Definition: lists_iterator.hpp:9
Handles anything related to entities and components in the engine.
Definition: component_manager.hpp:29
void setComponent(const Entity &entity, const T &component)
Adds a component to the specified entity.
Definition: component_manager.hpp:97
ComponentListCompact< T > & getComponentList() const
Retrieves the list of all the components of the same type.
Definition: component_manager.hpp:161
void addComponentClass(ComponentListType t=ComponentListType::kCompact)
Defines a list for a custom component and makes it ready to use with the engine.
Definition: component_manager.hpp:82
Entity addEntity(Entity *parent, const std::string &editor_name, const T &... args)
Creates an entity and assigns the specified components to it.
Definition: component_manager.hpp:58
ComponentManager & operator=(const ComponentManager &)=delete
Copy assignment operator (deleted).
ComponentListSparse< T > & getSparseList() const
Retrieves the list of all the components of the same type.
Definition: component_manager.hpp:178
void deleteEntity(Entity &entity)
Deletes an entity from the system and all its children.
T * getComponent(Entity entity) const
Retrieves a component for a specific entity.
Definition: component_manager.hpp:117
ComponentManager(const ComponentManager &)=delete
Copy constructor (deleted).
ComponentManager()
Default constructor.
Display and allows to modify entity information in real time.
Definition: editor.hpp:14
Identifies an entity. This class can only be modified by the engine.
Definition: component_manager.hpp:16
ComponentListType
type of container to be used for storing componets
Definition: component_lists.hpp:8