6 #include <unordered_map>
9 #include <lazarus/common.h>
14 std::type_index getTypeIndex()
16 return std::type_index(
typeid(T));
25 template <
typename Component>
30 : component(std::move(comp))
34 Component *
get() {
return component.get(); }
37 std::shared_ptr<Component> component;
43 using Identifier = size_t;
63 Identifier
getId()
const {
return entityId; }
68 template <
typename Component>
74 template <
typename T,
typename V,
typename... Types>
87 template <
typename Component,
typename... Args>
96 template <
typename Component>
104 template <
typename Component>
137 const Identifier entityId;
138 static Identifier entityCount;
139 std::unordered_map<std::type_index, std::shared_ptr<__lz::BaseComponentHandle>> components;
140 bool deleted =
false;
143 template <
typename Component>
146 return components.find(__lz::getTypeIndex<Component>()) != components.end();
149 template <
typename T,
typename V,
typename... Types>
152 return has<T>() && has<V, Types...>();
155 template <
typename Component,
typename... Args>
159 if (has<Component>())
161 std::stringstream msg;
162 msg <<
"Entity " <<
getId() <<
" already holds a component of type "
163 <<
typeid(Component).name();
168 std::shared_ptr<__lz::BaseComponentHandle> handle(
171 components[__lz::getTypeIndex<Component>()] = std::move(handle);
174 template <
typename Component>
177 if (!has<Component>())
179 std::stringstream msg;
180 msg <<
"Entity " <<
getId() <<
" does not have a component of type "
181 <<
typeid(Component).name();
185 components.erase(__lz::getTypeIndex<Component>());
188 template <
typename Component>
191 auto found = components.find(__lz::getTypeIndex<Component>());
192 if (found == components.end())
195 return compHandle->get();
Identifier getId() const
Returns the ID of the entity.
Definition: Entity.h:63
bool operator!=(const Entity &other)
Returns true if the IDs of the entities are different.
Definition: Entity.cpp:17
bool isDeleted() const
Returns whether this entity is marked for deletion upon the next pass of the garbage collector...
Definition: Entity.h:111
void addComponent(Args &&...args)
Attaches a component to the entity.
Definition: Entity.h:156
void removeComponent()
Removes the component of type T from the entity.
Definition: Entity.h:175
Component * get()
Returns a pointer to the entity's component of the specified type.
Definition: Entity.h:189
bool has() const
Returns whether the entity has a component of type T.
Definition: Entity.h:144
bool operator==(const Entity &other)
Returns true if the IDs of the entities are the same.
Definition: Entity.cpp:12
An Entity is a collection of components with a unique ID.
Definition: Entity.h:50
Entity()
Default constructor.
Definition: Entity.cpp:7
bool operator<(const Entity &other)
Returns true if the ID of this entity is smaller than the other.
Definition: Entity.cpp:22
void markForDeletion()
Marks the entity for deletion.
Definition: Entity.h:119