Go to the documentation of this file.
8 #include <unordered_map>
34 std::unordered_map<std::type_index, std::unique_ptr<Component>>
_components = {};
57 void setName(std::string &name);
76 T *ret = this->tryGetComponent<T>();
78 throw NotFoundError(
"No component could be found with the type \"" + std::string(
typeid(T).name()) +
"\".");
88 const std::type_index &type =
typeid(T);
89 auto existing = this->_components.find(type);
90 if (existing == this->_components.end())
92 return static_cast<T *
>(existing->second.get());
102 const T *ret = this->tryGetComponent<T>();
104 throw NotFoundError(
"No component could be found with the type \"" + std::string(
typeid(T).name()) +
"\".");
114 const std::type_index &type =
typeid(T);
115 auto existing = this->_components.find(type);
116 if (existing == this->_components.end())
118 return static_cast<T *
>(existing->second.get());
126 const std::type_info &type =
typeid(T);
141 template<
typename T,
typename ...TNested,
typename ...Types>
144 const std::type_index &type =
typeid(T);
146 throw DuplicateError(
"A component of the type \"" + std::string(type.name()) +
"\" already exists on " + this->_name +
".");
147 this->_components[type] = std::make_unique<T>(*
this,
TypeHolder<TNested>()..., std::forward<Types>(params)...);
148 if (this->_notifyScene)
163 const std::type_info &type =
typeid(T);
164 auto existing = this->_components.find(type);
165 if (existing == this->_components.end())
166 throw NotFoundError(
"No component could be found with the type \"" + std::string(type.name()) +
"\".");
167 this->_components.erase(existing);
168 if (this->_notifyScene)
174 explicit Entity(
Scene &wal, std::string name,
bool notifyScene =
true);
T * tryGetComponent()
Get a component of a specific type or null if not found.
Definition: Entity.hpp:86
void _componentAdded(const std::type_index &type)
Callback called when a component is added.
Definition: Entity.cpp:78
Scene & _scene
A reference to the ECS.
Definition: Entity.hpp:51
Definition: Component.cpp:7
void _componentRemoved(const std::type_index &type)
Callback called when a component is removed.
Definition: Entity.cpp:83
std::string getName() const
Get the name of the entity.
Definition: Entity.cpp:37
Represent a single component of WAL.
Definition: Component.hpp:17
unsigned _uid
The unique ID of the entity.
Definition: Entity.hpp:24
The main WAL class, it is used to setup and run the ECS.
Definition: Wal.hpp:27
Entity & addComponent(Types &&...params)
Add a component to this entity. The component is constructed in place.
Definition: Entity.hpp:142
An entity of the WAL's ECS.
Definition: Entity.hpp:20
void setDisable(bool disabled)
Disable this entity.
Definition: Entity.cpp:52
Entity(Scene &wal, std::string name, bool notifyScene=true)
A default constructor.
Definition: Entity.cpp:14
bool hasComponent() const
Check if this entity has a component.
Definition: Entity.hpp:124
static unsigned nextID
This ID will be the one of the next entity created.
Definition: Entity.hpp:37
A class only used to specify template arguments.
Definition: TypeHolder.hpp:12
bool isDisable() const
Used if the entity is disabled.
Definition: Entity.cpp:47
~Entity()=default
A default destructor.
unsigned getUid() const
Get the ID of the entity.
Definition: Entity.cpp:32
void setName(std::string &name)
Set the name of the entity.
Definition: Entity.cpp:42
Entity & removeComponent()
Remove a specific component (by type).
Definition: Entity.hpp:161
std::unordered_map< std::type_index, std::unique_ptr< Component > > _components
The list of the components of this entity.
Definition: Entity.hpp:34
const T * tryGetComponent() const
Get a component of a specific type or null if not found.
Definition: Entity.hpp:112
std::string _name
An entity name (this is useful for debugging)
Definition: Entity.hpp:26
friend Scene
Definition: Entity.hpp:46
Represent a single scene that contains entities.
Definition: Scene.hpp:17
bool operator==(const Entity &) const
Definition: Entity.cpp:88
T & getComponent()
Get a component of a specific type.
Definition: Entity.hpp:74
void scheduleDeletion()
Schedule this entity for deletion.
Definition: Entity.cpp:98
const T & getComponent() const
Get a component of a specific type.
Definition: Entity.hpp:100
Entity & operator=(const Entity &)=delete
An entity is not assignable.
bool _disabled
Is this entity enabled?
Definition: Entity.hpp:28
bool _shouldDelete
Has this entity been scheduled for deletion?
Definition: Entity.hpp:30
An exception informing the user that something could not be found.
Definition: WalError.hpp:43
bool shouldDelete() const
Has this entity been scheduled for deletion?
Definition: Entity.cpp:93
An exception informing the user that something already exists.
Definition: WalError.hpp:29
bool _notifyScene
Should this entity notify the scene of component changes?
Definition: Entity.hpp:32