12#include <unordered_map>
27 static std::size_t componentTypeCounter = 0;
28 return componentTypeCounter++;
36 template<
typename ComponentType >
40 return componentTypeID;
103 template<
typename T,
typename... Args >
106 static_assert( std::is_base_of< Component, T >::value,
"T must be derived from Component" );
109 if (mComponents.contains( typeID ))
111 std::cerr <<
"Warning: Component " <<
typeid( T ).name( )
112 <<
" already exists in entity " << mEntityID << std::endl;
113 throw std::runtime_error(
"Component already exists in entity" );
116 auto component = std::make_shared< T >( std::forward< Args >( args )... );
117 component->setParent(
this );
118 mComponents[ typeID ] = component;
130 template<
typename T >
133 static_assert( std::is_base_of< Component, T >::value,
"T must be derived from Component" );
136 mComponents.erase( typeID );
145 template<
typename T >
148 static_assert( std::is_base_of< Component, T >::value,
"T must be derived from Component" );
159 template<
typename T >
162 static_assert( std::is_base_of< Component, T >::value,
"T must be derived from Component" );
165 auto it = mComponents.find( typeID );
166 if (it != mComponents.end( ))
168 return std::dynamic_pointer_cast< T >( it->second );
186 template<
typename T >
187 [[nodiscard]] std::shared_ptr< T >
requireComponent(
const std::string& context =
"" )
const
189 static_assert( std::is_base_of< Component, T >::value,
"T must be derived from Component" );
193 std::string msg =
"Missing required component: ";
194 msg +=
typeid( T ).name( );
195 if (!context.empty( ))
197 msg +=
" in context: " + context;
199 throw std::runtime_error( msg );
214 template<
typename Interface >
217 for (
const auto& [ typeID, component ] : mComponents)
219 auto interfaceComponent = std::dynamic_pointer_cast< Interface >( component );
220 if (interfaceComponent)
222 return interfaceComponent;
229 unsigned int mEntityID;
230 std::unordered_map< std::size_t, std::shared_ptr< Component > > mComponents;
232 static unsigned int sEntityIDCounter;
233 static unsigned int generateNextEntityID( );
unsigned int getEntityID() const
Returns the unique ID associated with this entity.
Definition wEntity.cpp:37
std::shared_ptr< T > addComponent(Args &&... args)
Adds a new component of type T to the entity.
Definition wEntity.hpp:104
std::shared_ptr< T > requireComponent(const std::string &context="") const
Retrieves the component of type T and throws if it's missing.
Definition wEntity.hpp:187
void clearComponents()
Removes all components currently attached to the entity.
Definition wEntity.cpp:42
void removeComponent()
Removes the component of type T from the entity.
Definition wEntity.hpp:131
virtual ~Entity()
Definition wEntity.cpp:23
std::shared_ptr< T > getComponent() const
Retrieves the component of type T attached to the entity.
Definition wEntity.hpp:160
std::shared_ptr< Interface > getInterfaceComponent() const
Returns the first component that implements the specified interface.
Definition wEntity.hpp:215
bool hasComponent() const noexcept
Checks whether the entity has a component of type T.
Definition wEntity.hpp:146
Entity()
Definition wEntity.cpp:17
static void resetEntityIDCounter()
Resets the global entity ID counter to zero.
Definition wEntity.cpp:55
Definition wColorComponent.cpp:9
std::size_t getComponentTypeID() noexcept
Definition wEntity.hpp:37
std::size_t getNextComponentTypeID()
Definition wEntity.hpp:25