Added systems
It's now officially an ECS, yay! Former-commit-id: e2aacaa5c9fd362921cf3d064e346d11f942bd59
This commit is contained in:
41
SDK/src/NDK/BaseSystem.cpp
Normal file
41
SDK/src/NDK/BaseSystem.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/BaseSystem.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
BaseSystem::~BaseSystem()
|
||||
{
|
||||
for (const EntityHandle& entity : m_entities)
|
||||
entity->UnregisterSystem(m_systemId);
|
||||
}
|
||||
|
||||
bool BaseSystem::Filters(const EntityHandle& entity) const
|
||||
{
|
||||
for (ComponentId component : m_requiredComponents)
|
||||
{
|
||||
if (!entity->HasComponent(component))
|
||||
return false; // Au moins un component requis n'est pas présent
|
||||
}
|
||||
|
||||
for (ComponentId component : m_excludedComponents)
|
||||
{
|
||||
if (entity->HasComponent(component))
|
||||
return false; // Au moins un component exclu est présent
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BaseSystem::OnEntityAdded(const EntityHandle& entity)
|
||||
{
|
||||
NazaraUnused(entity);
|
||||
}
|
||||
|
||||
void BaseSystem::OnEntityRemoved(const EntityHandle& entity)
|
||||
{
|
||||
NazaraUnused(entity);
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,9 @@ namespace Ndk
|
||||
// Affectation et retour du component
|
||||
m_components[componentId] = std::move(component);
|
||||
|
||||
// On informe le monde que nous avons besoin d'une mise à jour
|
||||
m_world->MarkAsDirty(m_id);
|
||||
|
||||
return *m_components[componentId].get();
|
||||
}
|
||||
|
||||
@@ -53,13 +56,21 @@ namespace Ndk
|
||||
void Entity::RemoveAllComponents()
|
||||
{
|
||||
m_components.clear();
|
||||
|
||||
// On informe le monde que nous avons besoin d'une mise à jour
|
||||
m_world->MarkAsDirty(m_id);
|
||||
}
|
||||
|
||||
void Entity::RemoveComponent(ComponentId componentId)
|
||||
{
|
||||
///DOC: N'a aucun effet si le component n'est pas présent
|
||||
if (HasComponent(componentId))
|
||||
{
|
||||
m_components[componentId].reset();
|
||||
|
||||
// On informe le monde que nous avons besoin d'une mise à jour
|
||||
m_world->MarkAsDirty(m_id);
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::Create()
|
||||
@@ -71,6 +82,17 @@ namespace Ndk
|
||||
{
|
||||
m_valid = false;
|
||||
|
||||
// On informe chaque système
|
||||
for (SystemId systemId : m_systems)
|
||||
{
|
||||
if (m_world->HasSystem(systemId))
|
||||
{
|
||||
BaseSystem& system = m_world->GetSystem(systemId);
|
||||
system.RemoveEntity(CreateHandle());
|
||||
}
|
||||
}
|
||||
m_systems.clear();
|
||||
|
||||
// On informe chaque handle de notre destruction pour éviter qu'il ne continue de pointer sur nous
|
||||
for (EntityHandle* handle : m_handles)
|
||||
handle->OnEntityDestroyed();
|
||||
|
||||
@@ -106,5 +106,41 @@ namespace Ndk
|
||||
m_aliveEntities.pop_back();
|
||||
}
|
||||
m_killedEntities.Reset();
|
||||
|
||||
for (unsigned int i = m_dirtyEntities.FindFirst(); i != m_dirtyEntities.npos; i = m_dirtyEntities.FindNext(i))
|
||||
{
|
||||
NazaraAssert(i < m_entities.size(), "Entity index out of range");
|
||||
|
||||
EntityBlock& block = m_entities[i];
|
||||
Entity& entity = block.entity;
|
||||
EntityHandle& handle = m_aliveEntities[block.aliveIndex];
|
||||
|
||||
// Aucun intérêt de traiter une entité n'existant plus
|
||||
if (entity.IsValid())
|
||||
{
|
||||
for (auto& systemPair : m_systems)
|
||||
{
|
||||
BaseSystem* system = systemPair.second.get();
|
||||
|
||||
// L'entité est-elle enregistrée comme faisant partie du système ?
|
||||
bool partOfSystem = system->HasEntity(handle);
|
||||
if (system->Filters(handle))
|
||||
{
|
||||
// L'entité doit faire partie du système, est-ce que c'est déjà le cas ?
|
||||
if (!partOfSystem)
|
||||
// Non, rajoutons-là
|
||||
system->AddEntity(handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
// L'entité ne doit pas faire partie du système, était-ce le cas ?
|
||||
if (partOfSystem)
|
||||
// Oui, enlevons-là
|
||||
system->RemoveEntity(handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_dirtyEntities.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user