Sdk: Make World moveable

Former-commit-id: 285cb9da4cbafd3da0c0859eb9d366bb2fb978a3
This commit is contained in:
Lynix 2016-03-01 14:01:31 +01:00
parent 36067e31c4
commit dbce7592a9
8 changed files with 47 additions and 14 deletions

View File

@ -71,7 +71,7 @@ namespace Ndk
inline void RemoveEntity(Entity* entity);
inline void SetWorld(World& world);
inline void SetWorld(World* world) noexcept;
inline void ValidateEntity(Entity* entity, bool justAdded);

View File

@ -172,9 +172,9 @@ namespace Ndk
OnEntityValidation(entity, justAdded);
}
inline void BaseSystem::SetWorld(World& world)
inline void BaseSystem::SetWorld(World* world) noexcept
{
m_world = &world;
m_world = world;
}
inline bool BaseSystem::Initialize()
@ -186,6 +186,6 @@ namespace Ndk
inline void BaseSystem::Uninitialize()
{
// Rien à faire
// Nothing to do
}
}

View File

@ -60,13 +60,16 @@ namespace Ndk
Entity& operator=(Entity&&) = delete;
private:
Entity(World& world, EntityId id);
Entity(World* world, EntityId id);
void Create();
void Destroy();
inline void RegisterHandle(EntityHandle* handle);
inline void RegisterSystem(SystemIndex index);
inline void SetWorld(World* world) noexcept;
inline void UnregisterHandle(EntityHandle* handle);
inline void UnregisterSystem(SystemIndex index);

View File

@ -113,6 +113,13 @@ namespace Ndk
m_systemBits.UnboundedSet(index);
}
inline void Entity::SetWorld(World* world) noexcept
{
NazaraAssert(world, "An entity must be attached to a world at any time");
m_world = world;
}
inline void Entity::UnregisterHandle(EntityHandle* handle)
{
///DOC: Un handle ne doit être libéré qu'une fois, et doit faire partie de la liste, sous peine de crash

View File

@ -27,8 +27,8 @@ namespace Ndk
inline World(bool addDefaultSystems = true);
World(const World&) = delete;
World(World&&) = delete; ///TODO
~World();
inline World(World&& world) noexcept;
~World() noexcept;
void AddDefaultSystems();
@ -38,7 +38,7 @@ namespace Ndk
const EntityHandle& CreateEntity();
inline EntityList CreateEntities(unsigned int count);
void Clear();
void Clear() noexcept;
const EntityHandle& GetEntity(EntityId id);
inline const EntityList& GetEntities();
@ -62,7 +62,7 @@ namespace Ndk
inline void Update(float elapsedTime);
World& operator=(const World&) = delete;
World& operator=(World&&) = delete; ///TODO
inline World& operator=(World&& world) noexcept;
private:
inline void Invalidate();

View File

@ -13,6 +13,11 @@ namespace Ndk
AddDefaultSystems();
}
inline World::World(World&& world) noexcept
{
operator=(std::move(world));
}
inline BaseSystem& World::AddSystem(std::unique_ptr<BaseSystem>&& system)
{
NazaraAssert(system, "System must be valid");
@ -25,7 +30,7 @@ namespace Ndk
// Affectation et retour du système
m_systems[index] = std::move(system);
m_systems[index]->SetWorld(*this);
m_systems[index]->SetWorld(this);
Invalidate(); // On force une mise à jour de toutes les entités
@ -152,4 +157,22 @@ namespace Ndk
{
m_dirtyEntities.UnboundedSet(id, true);
}
inline World& World::operator=(World&& world) noexcept
{
m_aliveEntities = std::move(world.m_aliveEntities);
m_dirtyEntities = std::move(world.m_dirtyEntities);
m_freeIdList = std::move(world.m_freeIdList);
m_killedEntities = std::move(world.m_killedEntities);
m_entities = std::move(world.m_entities);
for (EntityBlock& block : m_entities)
block.entity.SetWorld(this);
m_systems = std::move(world.m_systems);
for (const auto& systemPtr : m_systems)
systemPtr->SetWorld(this);
return *this;
}
}

View File

@ -23,9 +23,9 @@ namespace Ndk
handle->OnEntityMoved(this);
}
Entity::Entity(World& world, EntityId id) :
Entity::Entity(World* world, EntityId id) :
m_id(id),
m_world(&world)
m_world(world)
{
}

View File

@ -46,7 +46,7 @@ namespace Ndk
id = m_entities.size();
// Impossible d'utiliser emplace_back à cause de la portée
m_entities.push_back(Entity(*this, id));
m_entities.push_back(Entity(this, id));
}
// On initialise l'entité et on l'ajoute à la liste des entités vivantes
@ -59,7 +59,7 @@ namespace Ndk
return m_aliveEntities.back();
}
void World::Clear()
void World::Clear() noexcept
{
///DOC: Tous les handles sont correctement invalidés