(NDK) Minor changes
Added some asserts and comments, Former-commit-id: 805246f0276aac157c66cbf346c392ab57934e84
This commit is contained in:
parent
6d1ac4fe18
commit
1903cbda8d
|
|
@ -2,6 +2,7 @@
|
||||||
// This file is part of the "Nazara Development Kit"
|
// This file is part of the "Nazara Development Kit"
|
||||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Ndk/Algorithm.hpp>
|
#include <Ndk/Algorithm.hpp>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
|
|
@ -20,9 +21,13 @@ namespace Ndk
|
||||||
template<typename ComponentType, unsigned int N>
|
template<typename ComponentType, unsigned int N>
|
||||||
ComponentIndex BaseComponent::Register(const char (&name)[N])
|
ComponentIndex BaseComponent::Register(const char (&name)[N])
|
||||||
{
|
{
|
||||||
|
// Il faut que notre composant possède un constructeur par défaut (pour la factory)
|
||||||
static_assert(std::is_default_constructible<ComponentType>::value, "ComponentType should be default-constructible");
|
static_assert(std::is_default_constructible<ComponentType>::value, "ComponentType should be default-constructible");
|
||||||
|
|
||||||
|
// On récupère la chaîne de caractère sous la forme d'un nombre qui servira d'identifiant unique
|
||||||
ComponentId id = BuildComponentId(name);
|
ComponentId id = BuildComponentId(name);
|
||||||
|
|
||||||
|
// On utilise les lambda pour créer une fonction factory
|
||||||
auto factory = []() -> BaseComponent*
|
auto factory = []() -> BaseComponent*
|
||||||
{
|
{
|
||||||
return new ComponentType;
|
return new ComponentType;
|
||||||
|
|
@ -33,13 +38,18 @@ namespace Ndk
|
||||||
|
|
||||||
inline ComponentIndex BaseComponent::Register(ComponentId id, Factory factoryFunc)
|
inline ComponentIndex BaseComponent::Register(ComponentId id, Factory factoryFunc)
|
||||||
{
|
{
|
||||||
|
// Nous allons rajouter notre composant à la fin
|
||||||
ComponentIndex index = s_entries.size();
|
ComponentIndex index = s_entries.size();
|
||||||
|
|
||||||
s_entries.resize(index + 1);
|
s_entries.resize(index + 1);
|
||||||
|
|
||||||
|
// On récupère et on affecte
|
||||||
ComponentEntry& entry = s_entries.back();
|
ComponentEntry& entry = s_entries.back();
|
||||||
entry.factory = factoryFunc;
|
entry.factory = factoryFunc;
|
||||||
entry.id = id;
|
entry.id = id;
|
||||||
|
|
||||||
|
// Une petite assertion pour s'assurer que l'identifiant n'est pas déjà utilisé
|
||||||
|
NazaraAssert(s_idToInsert.find(id) == s_idToInsert.end(), "This id is already in use");
|
||||||
|
|
||||||
s_idToIndex[id] = index;
|
s_idToIndex[id] = index;
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
|
|
|
||||||
|
|
@ -72,8 +72,8 @@ namespace Ndk
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<std::unique_ptr<BaseSystem>> m_systems;
|
std::vector<std::unique_ptr<BaseSystem>> m_systems;
|
||||||
std::vector<EntityId> m_freeIdList;
|
|
||||||
std::vector<EntityBlock> m_entities;
|
std::vector<EntityBlock> m_entities;
|
||||||
|
std::vector<EntityId> m_freeIdList;
|
||||||
EntityList m_aliveEntities;
|
EntityList m_aliveEntities;
|
||||||
NzBitset<nzUInt64> m_dirtyEntities;
|
NzBitset<nzUInt64> m_dirtyEntities;
|
||||||
NzBitset<nzUInt64> m_killedEntities;
|
NzBitset<nzUInt64> m_killedEntities;
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,7 @@ namespace Ndk
|
||||||
|
|
||||||
void World::Update()
|
void World::Update()
|
||||||
{
|
{
|
||||||
|
// Gestion des entités tuées depuis le dernier appel
|
||||||
for (unsigned int i = m_killedEntities.FindFirst(); i != m_killedEntities.npos; i = m_killedEntities.FindNext(i))
|
for (unsigned int i = m_killedEntities.FindFirst(); i != m_killedEntities.npos; i = m_killedEntities.FindNext(i))
|
||||||
{
|
{
|
||||||
EntityBlock& block = m_entities[i];
|
EntityBlock& block = m_entities[i];
|
||||||
|
|
@ -107,6 +108,7 @@ namespace Ndk
|
||||||
}
|
}
|
||||||
m_killedEntities.Reset();
|
m_killedEntities.Reset();
|
||||||
|
|
||||||
|
// Gestion des entités nécessitant une mise à jour de leurs systèmes
|
||||||
for (unsigned int i = m_dirtyEntities.FindFirst(); i != m_dirtyEntities.npos; i = m_dirtyEntities.FindNext(i))
|
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");
|
NazaraAssert(i < m_entities.size(), "Entity index out of range");
|
||||||
|
|
@ -120,19 +122,16 @@ namespace Ndk
|
||||||
{
|
{
|
||||||
// L'entité est-elle enregistrée comme faisant partie du système ?
|
// L'entité est-elle enregistrée comme faisant partie du système ?
|
||||||
bool partOfSystem = system->HasEntity(&entity);
|
bool partOfSystem = system->HasEntity(&entity);
|
||||||
if (system->Filters(&entity))
|
|
||||||
|
// Doit-elle en faire partie ?
|
||||||
|
if (system->Filters(&entity) != partOfSystem)
|
||||||
{
|
{
|
||||||
// L'entité doit faire partie du système, est-ce que c'est déjà le cas ?
|
// L'entité n'est pas dans l'état dans lequel elle devrait être vis-à-vis de ce système
|
||||||
if (!partOfSystem)
|
// si elle en fait partie, nous devons l'en enlever, et inversément
|
||||||
// Non, rajoutons-là
|
|
||||||
system->AddEntity(&entity);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// L'entité ne doit pas faire partie du système, était-ce le cas ?
|
|
||||||
if (partOfSystem)
|
if (partOfSystem)
|
||||||
// Oui, enlevons-là
|
|
||||||
system->RemoveEntity(&entity);
|
system->RemoveEntity(&entity);
|
||||||
|
else
|
||||||
|
system->AddEntity(&entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue