Ndk/Components: Added OnComponent[Attached|Detacted] events

And m_entity member variable


Former-commit-id: d6bd9e382e55bafd357e7ec62862ae731c21f897
This commit is contained in:
Lynix 2015-04-20 01:54:09 +02:00
parent a7be4c6346
commit 5b44619592
4 changed files with 55 additions and 7 deletions

View File

@ -14,8 +14,11 @@
namespace Ndk namespace Ndk
{ {
class Entity;
class NDK_API BaseComponent class NDK_API BaseComponent
{ {
friend Entity;
friend class Sdk; friend class Sdk;
public: public:
@ -35,10 +38,15 @@ namespace Ndk
protected: protected:
ComponentIndex m_componentIndex; ComponentIndex m_componentIndex;
Entity* m_entity;
static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc); static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc);
private: private:
virtual void OnComponentAttached(BaseComponent& component);
virtual void OnComponentDetached(BaseComponent& component);
void SetEntity(Entity* entity);
static bool Initialize(); static bool Initialize();
static void Uninitialize(); static void Uninitialize();

View File

@ -7,7 +7,8 @@
namespace Ndk namespace Ndk
{ {
inline BaseComponent::BaseComponent(ComponentIndex index) : inline BaseComponent::BaseComponent(ComponentIndex index) :
m_componentIndex(index) m_componentIndex(index),
m_entity(nullptr)
{ {
} }
@ -35,6 +36,11 @@ namespace Ndk
return index; return index;
} }
inline void BaseComponent::SetEntity(Entity* entity)
{
m_entity = entity;
}
inline bool BaseComponent::Initialize() inline bool BaseComponent::Initialize()
{ {
// Rien à faire // Rien à faire

View File

@ -8,6 +8,16 @@ namespace Ndk
{ {
BaseComponent::~BaseComponent() = default; BaseComponent::~BaseComponent() = default;
void BaseComponent::OnComponentAttached(BaseComponent& component)
{
NazaraUnused(component);
}
void BaseComponent::OnComponentDetached(BaseComponent& component)
{
NazaraUnused(component);
}
std::vector<BaseComponent::ComponentEntry> BaseComponent::s_entries; std::vector<BaseComponent::ComponentEntry> BaseComponent::s_entries;
std::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex; std::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex;
} }

View File

@ -23,24 +23,34 @@ namespace Ndk
Destroy(); Destroy();
} }
BaseComponent& Entity::AddComponent(std::unique_ptr<BaseComponent>&& component) BaseComponent& Entity::AddComponent(std::unique_ptr<BaseComponent>&& componentPtr)
{ {
NazaraAssert(component, "Component must be valid"); NazaraAssert(componentPtr, "Component must be valid");
ComponentIndex index = component->GetIndex(); ComponentIndex index = componentPtr->GetIndex();
// Nous nous assurons que le vecteur de component est suffisamment grand pour contenir le nouveau component // Nous nous assurons que le vecteur de component est suffisamment grand pour contenir le nouveau component
if (index >= m_components.size()) if (index >= m_components.size())
m_components.resize(index + 1); m_components.resize(index + 1);
// Affectation et retour du component // Affectation et retour du component
m_components[index] = std::move(component); m_components[index] = std::move(componentPtr);
m_componentBits.UnboundedSet(index); m_componentBits.UnboundedSet(index);
// On informe le monde que nous avons besoin d'une mise à jour // On informe le monde que nous avons besoin d'une mise à jour
m_world->Invalidate(m_id); m_world->Invalidate(m_id);
return *m_components[index].get(); // On récupère le component et on informe les composants existants du nouvel arrivant
BaseComponent& component = *m_components[index].get();
component.SetEntity(this);
for (unsigned int i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
{
if (i != index)
m_components[index]->OnComponentAttached(component);
}
return component;
} }
EntityHandle Entity::CreateHandle() EntityHandle Entity::CreateHandle()
@ -60,8 +70,12 @@ namespace Ndk
void Entity::RemoveAllComponents() void Entity::RemoveAllComponents()
{ {
for (unsigned int i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
RemoveComponent(i);
NazaraAssert(m_componentBits.TestNone(), "All components should be gone");
m_components.clear(); m_components.clear();
m_componentBits.Clear();
// On informe le monde que nous avons besoin d'une mise à jour // On informe le monde que nous avons besoin d'une mise à jour
m_world->Invalidate(m_id); m_world->Invalidate(m_id);
@ -72,6 +86,16 @@ namespace Ndk
///DOC: N'a aucun effet si le component n'est pas présent ///DOC: N'a aucun effet si le component n'est pas présent
if (HasComponent(index)) if (HasComponent(index))
{ {
// On récupère le component et on informe les composants du détachement
BaseComponent& component = *m_components[index].get();
for (unsigned int i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
{
if (i != index)
m_components[index]->OnComponentDetached(component);
}
component.SetEntity(nullptr);
m_components[index].reset(); m_components[index].reset();
m_componentBits.Reset(index); m_componentBits.Reset(index);