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

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

@@ -33,6 +33,7 @@ namespace Ndk
inline void BaseWidget::AddChild(std::unique_ptr<BaseWidget>&& widget)
{
widget->Show(m_visible);
widget->SetParent(this);
m_children.emplace_back(std::move(widget));
}

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

@@ -9,6 +9,7 @@
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Core/HandledObject.hpp>
#include <Nazara/Core/Signal.hpp>
#include <NDK/Algorithm.hpp>
#include <memory>
#include <vector>
@@ -65,6 +66,8 @@ namespace Ndk
Entity& operator=(const Entity&) = delete;
Entity& operator=(Entity&&) = delete;
NazaraSignal(OnEntityDestruction, Entity* /*entity*/);
private:
Entity(World* world, EntityId id);

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