mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-10-23 07:48:08 +00:00
Merge main into property-entrance-rewrite
This commit is contained in:
@@ -66,7 +66,7 @@ public:
|
||||
|
||||
eGameMasterLevel GetGMLevel() const { return m_GMLevel; }
|
||||
|
||||
uint8_t GetCollectibleID() const { return uint8_t(m_CollectibleID); }
|
||||
uint8_t GetCollectibleID() const;
|
||||
|
||||
Entity* GetParentEntity() const { return m_ParentEntity; }
|
||||
|
||||
@@ -274,6 +274,9 @@ public:
|
||||
template<typename T>
|
||||
T GetVarAs(const std::u16string& name) const;
|
||||
|
||||
template<typename ComponentType, typename... VaArgs>
|
||||
ComponentType* AddComponent(VaArgs... args);
|
||||
|
||||
/**
|
||||
* Get the LDF data.
|
||||
*/
|
||||
@@ -501,3 +504,36 @@ T Entity::GetNetworkVar(const std::u16string& name) {
|
||||
|
||||
return LDFData<T>::Default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Adds a component of type ComponentType to this entity and forwards the arguments to the constructor.
|
||||
*
|
||||
* @tparam ComponentType The component class type to add. Must derive from Component.
|
||||
* @tparam VaArgs The argument types to forward to the constructor.
|
||||
* @param args The arguments to forward to the constructor. The first argument passed to the ComponentType constructor will be this entity.
|
||||
* @return ComponentType* The added component. Will never return null.
|
||||
*/
|
||||
template<typename ComponentType, typename... VaArgs>
|
||||
inline ComponentType* Entity::AddComponent(VaArgs... args) {
|
||||
static_assert(std::is_base_of_v<Component, ComponentType>, "ComponentType must be a Component");
|
||||
|
||||
// Get the component if it already exists, or default construct a nullptr
|
||||
auto*& componentToReturn = m_Components[ComponentType::ComponentType];
|
||||
|
||||
// If it doesn't exist, create it and forward the arguments to the constructor
|
||||
if (!componentToReturn) {
|
||||
componentToReturn = new ComponentType(this, std::forward<VaArgs>(args)...);
|
||||
} else {
|
||||
// In this case the block is already allocated and ready for use
|
||||
// so we use a placement new to construct the component again as was requested by the caller.
|
||||
// Placement new means we already have memory allocated for the object, so this just calls its constructor again.
|
||||
// This is useful for when we want to create a new object in the same memory location as an old one.
|
||||
componentToReturn->~Component();
|
||||
new(componentToReturn) ComponentType(this, std::forward<VaArgs>(args)...);
|
||||
}
|
||||
|
||||
// Finally return the created or already existing component.
|
||||
// Because of the assert above, this should always be a ComponentType* but I need a way to guarantee the map cannot be modifed outside this function
|
||||
// To allow a static cast here instead of a dynamic one.
|
||||
return dynamic_cast<ComponentType*>(componentToReturn);
|
||||
}
|
||||
|
Reference in New Issue
Block a user