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

@@ -8,6 +8,16 @@ namespace Ndk
{
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::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex;
}

View File

@@ -23,24 +23,34 @@ namespace Ndk
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
if (index >= m_components.size())
m_components.resize(index + 1);
// Affectation et retour du component
m_components[index] = std::move(component);
m_components[index] = std::move(componentPtr);
m_componentBits.UnboundedSet(index);
// On informe le monde que nous avons besoin d'une mise à jour
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()
@@ -60,8 +70,12 @@ namespace Ndk
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_componentBits.Clear();
// On informe le monde que nous avons besoin d'une mise à jour
m_world->Invalidate(m_id);
@@ -72,6 +86,16 @@ namespace Ndk
///DOC: N'a aucun effet si le component n'est pas présent
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_componentBits.Reset(index);