diff --git a/SDK/include/NDK/BaseSystem.hpp b/SDK/include/NDK/BaseSystem.hpp index 0a8e1a9d5..99478d558 100644 --- a/SDK/include/NDK/BaseSystem.hpp +++ b/SDK/include/NDK/BaseSystem.hpp @@ -27,13 +27,13 @@ namespace Ndk virtual BaseSystem* Clone() const = 0; - bool Filters(const EntityHandle& entity) const; + bool Filters(const Entity* entity) const; const std::vector& GetEntities() const; SystemId GetId() const; World& GetWorld() const; - bool HasEntity(const EntityHandle& entity) const; + bool HasEntity(const Entity* entity) const; protected: template void Excludes(); @@ -45,12 +45,12 @@ namespace Ndk void RequiresComponent(ComponentId componentId); private: - void AddEntity(const EntityHandle& entity); + void AddEntity(Entity* entity); - virtual void OnEntityAdded(const EntityHandle& entity); - virtual void OnEntityRemoved(const EntityHandle& entity); + virtual void OnEntityAdded(Entity* entity); + virtual void OnEntityRemoved(Entity* entity); - void RemoveEntity(const EntityHandle& entity); + void RemoveEntity(Entity* entity); void SetWorld(World& world); diff --git a/SDK/include/NDK/BaseSystem.inl b/SDK/include/NDK/BaseSystem.inl index f4edda7ae..91efa89fe 100644 --- a/SDK/include/NDK/BaseSystem.inl +++ b/SDK/include/NDK/BaseSystem.inl @@ -27,8 +27,11 @@ namespace Ndk return *m_world; } - inline bool BaseSystem::HasEntity(const EntityHandle& entity) const + inline bool BaseSystem::HasEntity(const Entity* entity) const { + if (!entity) + return false; + return m_entityBits.UnboundedTest(entity->GetId()); } @@ -72,9 +75,11 @@ namespace Ndk m_requiredComponents.insert(componentId); } - inline void BaseSystem::AddEntity(const EntityHandle& entity) + inline void BaseSystem::AddEntity(Entity* entity) { - m_entities.push_back(entity); + NazaraAssert(entity, "Invalid entity"); + + m_entities.push_back(entity->CreateHandle()); m_entityBits.UnboundedSet(entity->GetId(), true); entity->RegisterSystem(m_systemId); @@ -82,9 +87,11 @@ namespace Ndk OnEntityAdded(entity); } - inline void BaseSystem::RemoveEntity(const EntityHandle& entity) + inline void BaseSystem::RemoveEntity(Entity* entity) { - auto it = std::find(m_entities.begin(), m_entities.end(), entity); + NazaraAssert(entity, "Invalid entity"); + + auto it = std::find(m_entities.begin(), m_entities.end(), *entity); NazaraAssert(it != m_entities.end(), "Entity is not part of this system"); // Pour éviter de déplacer beaucoup de handles, on swap le dernier avec celui à supprimer diff --git a/SDK/include/NDK/EntityHandle.hpp b/SDK/include/NDK/EntityHandle.hpp index d1ffa7411..309016a7c 100644 --- a/SDK/include/NDK/EntityHandle.hpp +++ b/SDK/include/NDK/EntityHandle.hpp @@ -44,11 +44,28 @@ namespace Ndk friend std::ostream& operator<<(std::ostream& out, const EntityHandle& handle); friend bool operator==(const EntityHandle& lhs, const EntityHandle& rhs); + friend bool operator==(const Entity& lhs, const EntityHandle& rhs); + friend bool operator==(const EntityHandle& lhs, const Entity& rhs); + friend bool operator!=(const EntityHandle& lhs, const EntityHandle& rhs); + friend bool operator!=(const Entity& lhs, const EntityHandle& rhs); + friend bool operator!=(const EntityHandle& lhs, const Entity& rhs); + friend bool operator<(const EntityHandle& lhs, const EntityHandle& rhs); + friend bool operator<(const Entity& lhs, const EntityHandle& rhs); + friend bool operator<(const EntityHandle& lhs, const Entity& rhs); + friend bool operator<=(const EntityHandle& lhs, const EntityHandle& rhs); + friend bool operator<=(const Entity& lhs, const EntityHandle& rhs); + friend bool operator<=(const EntityHandle& lhs, const Entity& rhs); + friend bool operator>(const EntityHandle& lhs, const EntityHandle& rhs); + friend bool operator>(const Entity& lhs, const EntityHandle& rhs); + friend bool operator>(const EntityHandle& lhs, const Entity& rhs); + friend bool operator>=(const EntityHandle& lhs, const EntityHandle& rhs); + friend bool operator>=(const Entity& lhs, const EntityHandle& rhs); + friend bool operator>=(const EntityHandle& lhs, const Entity& rhs); private: void OnEntityDestroyed(); diff --git a/SDK/include/NDK/EntityHandle.inl b/SDK/include/NDK/EntityHandle.inl index abd31dab1..4544a825f 100644 --- a/SDK/include/NDK/EntityHandle.inl +++ b/SDK/include/NDK/EntityHandle.inl @@ -149,30 +149,90 @@ namespace Ndk return lhs.m_entity == rhs.m_entity; } + inline bool operator==(const Entity& lhs, const EntityHandle& rhs) + { + return &lhs == rhs.m_entity; + } + + inline bool operator==(const EntityHandle& lhs, const Entity& rhs) + { + return lhs.m_entity == &rhs; + } + inline bool operator!=(const EntityHandle& lhs, const EntityHandle& rhs) { return !(lhs == rhs); } + inline bool operator!=(const Entity& lhs, const EntityHandle& rhs) + { + return !(lhs == rhs); + } + + inline bool operator!=(const EntityHandle& lhs, const Entity& rhs) + { + return !(lhs == rhs); + } + inline bool operator<(const EntityHandle& lhs, const EntityHandle& rhs) { return lhs.m_entity < rhs.m_entity; } + inline bool operator<(const Entity& lhs, const EntityHandle& rhs) + { + return &lhs < rhs.m_entity; + } + + inline bool operator<(const EntityHandle& lhs, const Entity& rhs) + { + return lhs.m_entity < &rhs; + } + inline bool operator<=(const EntityHandle& lhs, const EntityHandle& rhs) { return !(lhs > rhs); } + inline bool operator<=(const Entity& lhs, const EntityHandle& rhs) + { + return !(lhs > rhs); + } + + inline bool operator<=(const EntityHandle& lhs, const Entity& rhs) + { + return !(lhs > rhs); + } + inline bool operator>(const EntityHandle& lhs, const EntityHandle& rhs) { return rhs < lhs; } + inline bool operator>(const Entity& lhs, const EntityHandle& rhs) + { + return rhs < lhs; + } + + inline bool operator>(const EntityHandle& lhs, const Entity& rhs) + { + return rhs < lhs; + } + inline bool operator>=(const EntityHandle& lhs, const EntityHandle& rhs) { return !(lhs < rhs); } + + inline bool operator>=(const Entity& lhs, const EntityHandle& rhs) + { + return !(lhs < rhs); + } + + inline bool operator>=(const EntityHandle& lhs, const Entity& rhs) + { + return !(lhs < rhs); + } } namespace std diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 885053c54..51aef339d 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -32,7 +32,7 @@ namespace Ndk BaseSystem& AddSystem(std::unique_ptr&& system); template SystemType& AddSystem(Args&&... args); - EntityHandle CreateEntity(); + const EntityHandle& CreateEntity(); EntityList CreateEntities(unsigned int count); void Clear(); @@ -44,10 +44,10 @@ namespace Ndk bool HasSystem(SystemId systemId) const; template bool HasSystem() const; - void KillEntity(const EntityHandle& entity); + void KillEntity(Entity* entity); void KillEntities(const EntityList& list); - bool IsEntityValid(const EntityHandle& entity) const; + bool IsEntityValid(const Entity* entity) const; bool IsEntityIdValid(EntityId id) const; void RemoveAllSystems(); diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index 3b02e043e..eb3d819f1 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -84,9 +84,9 @@ namespace Ndk KillEntity(entity); } - inline bool World::IsEntityValid(const EntityHandle& entity) const + inline bool World::IsEntityValid(const Entity* entity) const { - return entity.IsValid() && entity->GetWorld() == this && IsEntityIdValid(entity->GetId()); + return entity && entity->GetWorld() == this && IsEntityIdValid(entity->GetId()); } inline bool World::IsEntityIdValid(EntityId id) const diff --git a/SDK/src/NDK/BaseSystem.cpp b/SDK/src/NDK/BaseSystem.cpp index d10b66e9d..58213a107 100644 --- a/SDK/src/NDK/BaseSystem.cpp +++ b/SDK/src/NDK/BaseSystem.cpp @@ -12,8 +12,11 @@ namespace Ndk entity->UnregisterSystem(m_systemId); } - bool BaseSystem::Filters(const EntityHandle& entity) const + bool BaseSystem::Filters(const Entity* entity) const { + if (!entity) + return false; + for (ComponentId component : m_requiredComponents) { if (!entity->HasComponent(component)) @@ -29,12 +32,12 @@ namespace Ndk return true; } - void BaseSystem::OnEntityAdded(const EntityHandle& entity) + void BaseSystem::OnEntityAdded(Entity* entity) { NazaraUnused(entity); } - void BaseSystem::OnEntityRemoved(const EntityHandle& entity) + void BaseSystem::OnEntityRemoved(Entity* entity) { NazaraUnused(entity); } diff --git a/SDK/src/NDK/Entity.cpp b/SDK/src/NDK/Entity.cpp index 7558438ae..51feed183 100644 --- a/SDK/src/NDK/Entity.cpp +++ b/SDK/src/NDK/Entity.cpp @@ -45,7 +45,7 @@ namespace Ndk void Entity::Kill() { - m_world->KillEntity(CreateHandle()); + m_world->KillEntity(this); } bool Entity::IsValid() const @@ -88,7 +88,7 @@ namespace Ndk if (m_world->HasSystem(systemId)) { BaseSystem& system = m_world->GetSystem(systemId); - system.RemoveEntity(CreateHandle()); + system.RemoveEntity(this); } } m_systems.clear(); diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index 65125d568..b5debc08c 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -13,7 +13,7 @@ namespace Ndk Clear(); } - EntityHandle World::CreateEntity() + const EntityHandle& World::CreateEntity() { EntityId id; if (!m_freeIdList.empty()) @@ -35,11 +35,10 @@ namespace Ndk Entity& entity = m_entities[id].entity; entity.Create(); - EntityHandle handle = entity.CreateHandle(); - m_aliveEntities.push_back(handle); + m_aliveEntities.emplace_back(&entity); m_entities[id].aliveIndex = m_aliveEntities.size()-1; - return handle; + return m_aliveEntities.back(); } void World::Clear() @@ -54,7 +53,7 @@ namespace Ndk m_killedEntities.Clear(); } - void World::KillEntity(const EntityHandle& entity) + void World::KillEntity(Entity* entity) { ///DOC: Ignoré si l'entité est invalide @@ -111,9 +110,7 @@ namespace Ndk { 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]; + Entity& entity = m_entities[i].entity; // Aucun intérêt de traiter une entité n'existant plus if (entity.IsValid()) @@ -123,20 +120,20 @@ namespace Ndk 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)) + bool partOfSystem = system->HasEntity(&entity); + if (system->Filters(&entity)) { // 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); + system->AddEntity(&entity); } else { // L'entité ne doit pas faire partie du système, était-ce le cas ? if (partOfSystem) // Oui, enlevons-là - system->RemoveEntity(handle); + system->RemoveEntity(&entity); } } }