Merge remote-tracking branch 'refs/remotes/origin/master' into reflection-mapping

This commit is contained in:
Lynix
2017-03-18 17:23:12 +01:00
23 changed files with 427 additions and 39 deletions

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,11 @@ 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

@@ -30,6 +30,7 @@ namespace Ndk
m_enabled(entity.m_enabled),
m_valid(entity.m_valid)
{
entity.m_world = nullptr;
}
/*!
@@ -53,7 +54,8 @@ namespace Ndk
Entity::~Entity()
{
Destroy();
if (m_world)
Destroy();
}
/*!
@@ -145,6 +147,17 @@ namespace Ndk
void Entity::Destroy()
{
OnEntityDestruction(this);
OnEntityDestruction.Clear();
// 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();
// Detach components while they're still attached to systems
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
m_components[i]->SetEntity(nullptr);
// We alert each system
for (std::size_t index = m_systemBits.FindFirst(); index != m_systemBits.npos; index = m_systemBits.FindNext(index))
{
@@ -158,10 +171,7 @@ namespace Ndk
}
m_systemBits.Clear();
// We properly destroy each component
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
m_components[i]->SetEntity(nullptr);
// Destroy components
m_components.clear();
m_componentBits.Reset();

View File

@@ -185,12 +185,12 @@ namespace Ndk
NazaraAssert(entity.IsValid(), "Entity must be valid");
// Send back the identifier of the entity to the free queue
m_freeIdList.push_back(entity.GetId());
// Destruction of the entity (invalidation of handle by the same way)
entity.Destroy();
// Send back the identifier of the entity to the free queue
m_freeIdList.push_back(entity.GetId());
// We take out the handle from the list of alive entities
// With the idiom swap and pop