SDK/Entity: Delay component removal until world update
Allows system to freely remove components while updating Former-commit-id: 24dabc3423951621e8be7889fc27eaad4e8566b7 [formerly ed68ae6028a369be7a639dc8269848cd3b7af25f] [formerly 7404bc26b369203316834a15eda120e2a7ce94ee [formerly b3b81c6068bf35a7b838edefa9275de94f9231f7]] Former-commit-id: bc3f57813ed8b3146c0db385ff5df490b39ec3fa [formerly bc31fad8f0ce68ac2b94f4eca1c96664b4b68c93] Former-commit-id: 22c300e946a3f921ea49de7451567bc6202bb9a0
This commit is contained in:
parent
3f899d24aa
commit
f22633adb5
|
|
@ -52,8 +52,8 @@ namespace Ndk
|
|||
inline bool IsEnabled() const;
|
||||
inline bool IsValid() const;
|
||||
|
||||
void RemoveAllComponents();
|
||||
void RemoveComponent(ComponentIndex index);
|
||||
inline void RemoveAllComponents();
|
||||
inline void RemoveComponent(ComponentIndex index);
|
||||
template<typename ComponentType> void RemoveComponent();
|
||||
|
||||
inline Nz::String ToString() const;
|
||||
|
|
@ -67,6 +67,10 @@ namespace Ndk
|
|||
void Create();
|
||||
void Destroy();
|
||||
|
||||
void DestroyComponent(ComponentIndex index);
|
||||
|
||||
inline Nz::Bitset<>& GetRemovedComponentBits();
|
||||
|
||||
inline void RegisterSystem(SystemIndex index);
|
||||
|
||||
inline void SetWorld(World* world) noexcept;
|
||||
|
|
@ -75,6 +79,7 @@ namespace Ndk
|
|||
|
||||
std::vector<std::unique_ptr<BaseComponent>> m_components;
|
||||
Nz::Bitset<> m_componentBits;
|
||||
Nz::Bitset<> m_removedComponentBits;
|
||||
Nz::Bitset<> m_systemBits;
|
||||
EntityId m_id;
|
||||
World* m_world;
|
||||
|
|
|
|||
|
|
@ -103,12 +103,31 @@ namespace Ndk
|
|||
RemoveComponent(index);
|
||||
}
|
||||
|
||||
inline void Entity::RemoveAllComponents()
|
||||
{
|
||||
m_removedComponentBits = m_componentBits;
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
inline void Entity::RemoveComponent(ComponentIndex index)
|
||||
{
|
||||
m_removedComponentBits.UnboundedSet(index);
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
inline Nz::String Entity::ToString() const
|
||||
{
|
||||
Nz::StringStream ss;
|
||||
return ss << "Entity(" << GetId() << ')';
|
||||
}
|
||||
|
||||
inline Nz::Bitset<>& Entity::GetRemovedComponentBits()
|
||||
{
|
||||
return m_removedComponentBits;
|
||||
}
|
||||
|
||||
inline void Entity::RegisterSystem(SystemIndex index)
|
||||
{
|
||||
m_systemBits.UnboundedSet(index);
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ namespace Ndk
|
|||
// Affectation et retour du component
|
||||
m_components[index] = std::move(componentPtr);
|
||||
m_componentBits.UnboundedSet(index);
|
||||
m_removedComponentBits.UnboundedReset(index);
|
||||
|
||||
Invalidate();
|
||||
|
||||
|
|
@ -71,40 +72,6 @@ namespace Ndk
|
|||
m_world->Invalidate(m_id);
|
||||
}
|
||||
|
||||
void Entity::RemoveAllComponents()
|
||||
{
|
||||
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
RemoveComponent(static_cast<ComponentIndex>(i));
|
||||
|
||||
NazaraAssert(m_componentBits.TestNone(), "All components should be gone");
|
||||
|
||||
m_components.clear();
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
|
||||
void Entity::RemoveComponent(ComponentIndex index)
|
||||
{
|
||||
///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 (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
{
|
||||
if (i != index)
|
||||
m_components[i]->OnComponentDetached(component);
|
||||
}
|
||||
|
||||
component.SetEntity(nullptr);
|
||||
|
||||
m_components[index].reset();
|
||||
m_componentBits.Reset(index);
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::Create()
|
||||
{
|
||||
m_enabled = true;
|
||||
|
|
@ -128,4 +95,25 @@ namespace Ndk
|
|||
|
||||
m_valid = false;
|
||||
}
|
||||
|
||||
void Entity::DestroyComponent(ComponentIndex index)
|
||||
{
|
||||
///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 (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
|
||||
{
|
||||
if (i != index)
|
||||
m_components[i]->OnComponentDetached(component);
|
||||
}
|
||||
|
||||
component.SetEntity(nullptr);
|
||||
|
||||
m_components[index].reset();
|
||||
m_componentBits.Reset(index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -139,6 +139,11 @@ namespace Ndk
|
|||
if (!entity->IsValid())
|
||||
continue;
|
||||
|
||||
Nz::Bitset<>& removedComponents = entity->GetRemovedComponentBits();
|
||||
for (std::size_t j = removedComponents.FindFirst(); j != m_dirtyEntities.npos; j = removedComponents.FindNext(j))
|
||||
entity->DestroyComponent(j);
|
||||
removedComponents.Reset();
|
||||
|
||||
for (auto& system : m_systems)
|
||||
{
|
||||
// Ignore non-existent systems
|
||||
|
|
|
|||
Loading…
Reference in New Issue