Sdk/Entity: Add DropComponent method

This commit is contained in:
Lynix 2019-10-21 19:04:55 +02:00
parent 9161886cc8
commit dac4f7806a
5 changed files with 45 additions and 32 deletions

View File

@ -292,6 +292,7 @@ Nazara Development Kit:
- ⚠️ Console now supports text color in history
- Added World::CloneEntity overload taking an EntityHandle const reference, allowing to copy entities from other worlds
- Fixed PhysicsComponent2D copy not copying physics attributes
- Added Entity::DropComponent which detaches a component without necessarily destroying it
# 0.4:

View File

@ -43,6 +43,10 @@ namespace Ndk
const EntityHandle& Clone() const;
inline void Disable();
std::unique_ptr<BaseComponent> DropComponent(ComponentIndex index);
template<typename ComponentType> std::unique_ptr<BaseComponent> DropComponent();
void Enable(bool enable = true);
inline BaseComponent& GetComponent(ComponentIndex index);
@ -83,8 +87,6 @@ namespace Ndk
void Create();
void Destroy();
void DestroyComponent(ComponentIndex index);
inline Nz::Bitset<>& GetRemovedComponentBits();
inline void RegisterEntityList(EntityList* list);

View File

@ -64,6 +64,15 @@ namespace Ndk
* \remark Produces a NazaraAssert if component is not available in this entity
*/
template<typename ComponentType>
std::unique_ptr<BaseComponent> Entity::DropComponent()
{
static_assert(std::is_base_of<BaseComponent, ComponentType>::value, "ComponentType is not a component");
ComponentIndex index = GetComponentIndex<ComponentType>();
return DropComponent(index);
}
template<typename ComponentType>
ComponentType& Entity::GetComponent()
{

View File

@ -97,6 +97,36 @@ namespace Ndk
return m_world->CloneEntity(m_id);
}
/*!
* \brief Detaches a component from the entity
* \return An owning pointer to the component
*
* Instantly detaches a component from the entity and returns it, allowing to attach it to another entity
*
* \remark Unlike RemoveComponent, this function instantly removes the component
*/
std::unique_ptr<BaseComponent> Entity::DropComponent(ComponentIndex index)
{
if (!HasComponent(index))
return nullptr;
// We get the component and we alert existing components of the deleted one
std::unique_ptr<BaseComponent> component = std::move(m_components[index]);
m_components[index].reset();
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
{
if (i != index)
m_components[i]->OnComponentDetached(*component);
}
m_componentBits.Reset(index);
m_removedComponentBits.UnboundedReset(index);
component->SetEntity(nullptr);
return component;
}
/*!
* \brief Enables the entity
*
@ -208,32 +238,4 @@ namespace Ndk
m_valid = false;
}
/*!
* \brief Destroys a component by index
*
* \param index Index of the component
*
* \remark If component is not available, no action is performed
*/
void Entity::DestroyComponent(ComponentIndex index)
{
if (HasComponent(index))
{
// We get the component and we alert existing components of the deleted one
BaseComponent& component = *m_components[index].get();
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
{
if (i != index)
m_components[i]->OnComponentDetached(component);
}
component.SetEntity(nullptr);
m_components[index].reset();
m_componentBits.Reset(index);
}
}
}

View File

@ -249,8 +249,7 @@ namespace Ndk
Nz::Bitset<>& removedComponents = entity->GetRemovedComponentBits();
for (std::size_t j = removedComponents.FindFirst(); j != m_dirtyEntities.back.npos; j = removedComponents.FindNext(j))
entity->DestroyComponent(static_cast<Ndk::ComponentIndex>(j));
removedComponents.Reset();
entity->DropComponent(static_cast<Ndk::ComponentIndex>(j));
for (auto& system : m_orderedSystems)
{