Added systems
It's now officially an ECS, yay! Former-commit-id: e2aacaa5c9fd362921cf3d064e346d11f942bd59
This commit is contained in:
@@ -3,9 +3,35 @@
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline BaseSystem& World::AddSystem(std::unique_ptr<BaseSystem>&& system)
|
||||
{
|
||||
NazaraAssert(system, "System must be valid");
|
||||
|
||||
SystemId systemId = system->GetId();
|
||||
|
||||
// Affectation et retour du système
|
||||
m_systems[systemId] = std::move(system);
|
||||
m_systems[systemId]->SetWorld(*this);
|
||||
|
||||
MarkAllAsDirty(); // On force une mise à jour de toutes les entités
|
||||
|
||||
return *m_systems[systemId].get();
|
||||
}
|
||||
|
||||
template<typename SystemType, typename... Args>
|
||||
SystemType& World::AddSystem(Args&&... args)
|
||||
{
|
||||
static_assert(std::is_base_of<BaseSystem, SystemType>(), "SystemType is not a component");
|
||||
|
||||
// Allocation et affectation du component
|
||||
std::unique_ptr<SystemType> ptr(new SystemType(std::forward(args)...));
|
||||
return static_cast<SystemType&>(AddSystem(std::move(ptr)));
|
||||
}
|
||||
|
||||
inline World::EntityList World::CreateEntities(unsigned int count)
|
||||
{
|
||||
EntityList list;
|
||||
@@ -17,6 +43,41 @@ namespace Ndk
|
||||
return list;
|
||||
}
|
||||
|
||||
inline BaseSystem& World::GetSystem(SystemId systemId)
|
||||
{
|
||||
///DOC: Le système doit être présent
|
||||
NazaraAssert(HasSystem(systemId), "This system is not part of the world");
|
||||
|
||||
BaseSystem* system = m_systems[systemId].get();
|
||||
NazaraAssert(system, "Invalid system pointer");
|
||||
|
||||
return *system;
|
||||
}
|
||||
|
||||
template<typename SystemType>
|
||||
SystemType& World::GetSystem()
|
||||
{
|
||||
///DOC: Le système doit être présent
|
||||
static_assert(std::is_base_of<BaseSystem, SystemType>(), "SystemType is not a system");
|
||||
|
||||
SystemId systemId = GetSystemId<SystemType>();
|
||||
return static_cast<SystemType&>(GetSystem(systemId));
|
||||
}
|
||||
|
||||
inline bool World::HasSystem(SystemId systemId) const
|
||||
{
|
||||
return m_systems.count(systemId) > 0;
|
||||
}
|
||||
|
||||
template<typename SystemType>
|
||||
bool World::HasSystem() const
|
||||
{
|
||||
static_assert(std::is_base_of<BaseSystem, SystemType>(), "SystemType is not a component");
|
||||
|
||||
SystemId systemId = GetSystemId<SystemType>();
|
||||
return HasSystem(systemId);
|
||||
}
|
||||
|
||||
inline void World::KillEntities(const EntityList& list)
|
||||
{
|
||||
for (const EntityHandle& entity : list)
|
||||
@@ -32,4 +93,36 @@ namespace Ndk
|
||||
{
|
||||
return id < m_entities.size() && m_entities[id].entity.IsValid();
|
||||
}
|
||||
|
||||
inline void World::RemoveAllSystems()
|
||||
{
|
||||
m_systems.clear();
|
||||
}
|
||||
|
||||
inline void World::RemoveSystem(SystemId systemId)
|
||||
{
|
||||
///DOC: N'a aucun effet si le système n'est pas présent
|
||||
if (HasSystem(systemId))
|
||||
m_systems[systemId].reset();
|
||||
}
|
||||
|
||||
template<typename SystemType>
|
||||
void World::RemoveSystem()
|
||||
{
|
||||
static_assert(std::is_base_of<BaseSystem, SystemType>(), "SystemType is not a system");
|
||||
|
||||
SystemId systemId = GetSystemId<SystemType>();
|
||||
RemoveSystem(systemId);
|
||||
}
|
||||
|
||||
inline void World::MarkAllAsDirty()
|
||||
{
|
||||
m_dirtyEntities.Resize(m_entities.size(), false);
|
||||
m_dirtyEntities.Set(true); // Activation de tous les bits
|
||||
}
|
||||
|
||||
inline void World::MarkAsDirty(EntityId id)
|
||||
{
|
||||
m_dirtyEntities.UnboundedSet(id, true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user