// 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 #include namespace Ndk { inline BaseSystem& World::AddSystem(std::unique_ptr&& 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 SystemType& World::AddSystem(Args&&... args) { static_assert(std::is_base_of(), "SystemType is not a component"); // Allocation et affectation du component std::unique_ptr ptr(new SystemType(std::forward(args)...)); return static_cast(AddSystem(std::move(ptr))); } inline World::EntityList World::CreateEntities(unsigned int count) { EntityList list; list.reserve(count); for (unsigned int i = 0; i < count; ++i) list.emplace_back(CreateEntity()); 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 SystemType& World::GetSystem() { ///DOC: Le système doit être présent static_assert(std::is_base_of(), "SystemType is not a system"); SystemId systemId = GetSystemId(); return static_cast(GetSystem(systemId)); } inline bool World::HasSystem(SystemId systemId) const { return m_systems.count(systemId) > 0; } template bool World::HasSystem() const { static_assert(std::is_base_of(), "SystemType is not a component"); SystemId systemId = GetSystemId(); return HasSystem(systemId); } inline void World::KillEntities(const EntityList& list) { for (const EntityHandle& entity : list) KillEntity(entity); } inline bool World::IsEntityValid(const Entity* entity) const { return entity && entity->GetWorld() == this && IsEntityIdValid(entity->GetId()); } inline bool World::IsEntityIdValid(EntityId id) const { 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 void World::RemoveSystem() { static_assert(std::is_base_of(), "SystemType is not a system"); SystemId systemId = GetSystemId(); 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); } }