Sdk/BaseComponent: Add OnEntityDestruction callback

Also fixes physics component callbacks (OnContactEnd by example) being
fired while entity is destroyed
This commit is contained in:
Lynix 2017-03-03 17:52:07 +01:00
parent 04f7b40150
commit 605e5c2fd0
7 changed files with 29 additions and 0 deletions

View File

@ -47,6 +47,7 @@ namespace Ndk
virtual void OnComponentAttached(BaseComponent& component);
virtual void OnComponentDetached(BaseComponent& component);
virtual void OnDetached();
virtual void OnEntityDestruction();
void SetEntity(Entity* entity);

View File

@ -55,6 +55,7 @@ namespace Ndk
void OnComponentAttached(BaseComponent& component) override;
void OnComponentDetached(BaseComponent& component) override;
void OnDetached() override;
void OnEntityDestruction() override;
std::unique_ptr<Nz::RigidBody2D> m_object;
};

View File

@ -62,6 +62,7 @@ namespace Ndk
void OnComponentAttached(BaseComponent& component) override;
void OnComponentDetached(BaseComponent& component) override;
void OnDetached() override;
void OnEntityDestruction() override;
std::unique_ptr<Nz::RigidBody3D> m_object;
};

View File

@ -54,6 +54,15 @@ namespace Ndk
{
}
/*!
* \brief Operation to perform when the entity is destroyed and we're still attached to it
*
* \remark This is always called before the entity proper destruction, and thus its components.
*/
void BaseComponent::OnEntityDestruction()
{
}
std::vector<BaseComponent::ComponentEntry> BaseComponent::s_entries;
std::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex;
}

View File

@ -88,5 +88,11 @@ namespace Ndk
m_object.reset();
}
void PhysicsComponent2D::OnEntityDestruction()
{
// Kill rigidbody before entity destruction to force contact callbacks to be called while the entity is still valid
m_object.reset();
}
ComponentIndex PhysicsComponent2D::componentIndex;
}

View File

@ -88,5 +88,12 @@ namespace Ndk
m_object.reset();
}
void PhysicsComponent3D::OnEntityDestruction()
{
// Kill rigid body before entity destruction to force contact callbacks to be called while the entity is still valid
m_object.reset();
}
ComponentIndex PhysicsComponent3D::componentIndex;
}

View File

@ -145,6 +145,10 @@ namespace Ndk
void Entity::Destroy()
{
// We prepare components for entity destruction (some components needs this to handle some final callbacks while the entity is still valid)
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
m_components[i]->OnEntityDestruction();
// We alert each system
for (std::size_t index = m_systemBits.FindFirst(); index != m_systemBits.npos; index = m_systemBits.FindNext(index))
{