From 58416694d4733b10a143aace7ae855cdf5633ae5 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 4 May 2015 00:25:54 +0200 Subject: [PATCH] Ndk: Added EntityList Former-commit-id: f68c3830e59f460b06e23dbacc150c17937491df --- SDK/include/NDK/EntityList.hpp | 63 ++++++++++++ SDK/include/NDK/EntityList.inl | 117 ++++++++++++++++++++++ SDK/include/NDK/Systems/PhysicsSystem.hpp | 5 +- SDK/src/NDK/Systems/PhysicsSystem.cpp | 18 +--- 4 files changed, 186 insertions(+), 17 deletions(-) create mode 100644 SDK/include/NDK/EntityList.hpp create mode 100644 SDK/include/NDK/EntityList.inl diff --git a/SDK/include/NDK/EntityList.hpp b/SDK/include/NDK/EntityList.hpp new file mode 100644 index 000000000..753189580 --- /dev/null +++ b/SDK/include/NDK/EntityList.hpp @@ -0,0 +1,63 @@ +// 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 + +#pragma once + +#ifndef NDK_ENTITYLIST_HPP +#define NDK_ENTITYLIST_HPP + +#include +#include +#include + +namespace Ndk +{ + class NDK_API EntityList + { + public: + using Container = std::vector; + + EntityList() = default; + ~EntityList() = default; + + void Clear(); + + bool Has(const Entity* entity); + bool Has(EntityId entity); + + void Insert(Entity* entity); + + void Remove(Entity* entity); + + // Interface STD + Container::iterator begin(); + Container::const_iterator begin() const; + + Container::const_iterator cbegin() const; + Container::const_iterator cend() const; + Container::const_reverse_iterator crbegin() const; + Container::const_reverse_iterator crend() const; + + bool empty() const; + + Container::iterator end(); + Container::const_iterator end() const; + + Container::reverse_iterator rbegin(); + Container::const_reverse_iterator rbegin() const; + + Container::reverse_iterator rend(); + Container::const_reverse_iterator rend() const; + + Container::size_type size() const; + + private: + std::vector m_entities; + NzBitset m_entityBits; + }; +} + +#include + +#endif // NDK_ENTITYLIST_HPP diff --git a/SDK/include/NDK/EntityList.inl b/SDK/include/NDK/EntityList.inl new file mode 100644 index 000000000..e7cd2bb3b --- /dev/null +++ b/SDK/include/NDK/EntityList.inl @@ -0,0 +1,117 @@ +// 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 void EntityList::Clear() + { + m_entities.clear(); + m_entityBits.Clear(); + } + + inline bool EntityList::Has(const Entity* entity) + { + return entity && entity->IsValid() && Has(entity->GetId()); + } + + inline bool EntityList::Has(EntityId entity) + { + return m_entityBits.UnboundedTest(entity); + } + + inline void EntityList::Insert(Entity* entity) + { + if (!Has(entity)) + { + m_entities.emplace_back(entity); + m_entityBits.UnboundedSet(entity->GetId(), true); + } + } + + inline void EntityList::Remove(Entity* entity) + { + if (Has(entity)) + { + auto it = std::find(m_entities.begin(), m_entities.end(), *entity); + NazaraAssert(it != m_entities.end(), "Entity should be part of the vector"); + + std::swap(*it, m_entities.back()); + m_entities.pop_back(); // On le sort du vector + } + } + + // Interface STD + inline EntityList::Container::iterator EntityList::begin() + { + return m_entities.begin(); + } + + inline EntityList::Container::const_iterator EntityList::begin() const + { + return m_entities.begin(); + } + + inline EntityList::Container::const_iterator EntityList::cbegin() const + { + return m_entities.cbegin(); + } + + inline EntityList::Container::const_iterator EntityList::cend() const + { + return m_entities.cend(); + } + + inline EntityList::Container::const_reverse_iterator EntityList::crbegin() const + { + return m_entities.crbegin(); + } + + inline EntityList::Container::const_reverse_iterator EntityList::crend() const + { + return m_entities.crend(); + } + + inline bool EntityList::empty() const + { + return m_entities.empty(); + } + + inline EntityList::Container::iterator EntityList::end() + { + return m_entities.end(); + } + + inline EntityList::Container::const_iterator EntityList::end() const + { + return m_entities.end(); + } + + inline EntityList::Container::reverse_iterator EntityList::rbegin() + { + return m_entities.rbegin(); + } + + inline EntityList::Container::const_reverse_iterator EntityList::rbegin() const + { + return m_entities.rbegin(); + } + + inline EntityList::Container::reverse_iterator EntityList::rend() + { + return m_entities.rend(); + } + + inline EntityList::Container::const_reverse_iterator EntityList::rend() const + { + return m_entities.rend(); + } + + inline EntityList::Container::size_type EntityList::size() const + { + return m_entities.size(); + } +} diff --git a/SDK/include/NDK/Systems/PhysicsSystem.hpp b/SDK/include/NDK/Systems/PhysicsSystem.hpp index dbe3bf42b..227ee1f9d 100644 --- a/SDK/include/NDK/Systems/PhysicsSystem.hpp +++ b/SDK/include/NDK/Systems/PhysicsSystem.hpp @@ -8,6 +8,7 @@ #define NDK_SYSTEMS_PHYSICSSYSTEM_HPP #include +#include #include namespace Ndk @@ -29,8 +30,8 @@ namespace Ndk private: void OnEntityValidation(Entity* entity, bool justAdded) override; - std::vector m_dynamicObjects; - std::vector m_staticObjects; + EntityList m_dynamicObjects; + EntityList m_staticObjects; NzPhysWorld m_world; }; } diff --git a/SDK/src/NDK/Systems/PhysicsSystem.cpp b/SDK/src/NDK/Systems/PhysicsSystem.cpp index ec7d10d5f..a54c776a2 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem.cpp @@ -82,23 +82,11 @@ namespace Ndk { // On prend le tableau inverse de celui dont l'entité devrait faire partie auto& entities = (entity->HasComponent()) ? m_staticObjects : m_dynamicObjects; - - auto it = std::find(entities.begin(), entities.end(), *entity); - if (it != entities.end()) - { - // Pour éviter de déplacer beaucoup de handles, on swap le dernier avec celui à supprimer - std::swap(*it, entities.back()); - entities.pop_back(); // On le sort du vector - } + entities.Remove(entity); } - if (entity->HasComponent()) - m_dynamicObjects.emplace_back(entity); - else - { - NazaraAssert(entity->HasComponent(), "Validated entity should have component CollisionComponent"); - m_staticObjects.emplace_back(entity); - } + auto& entities = (entity->HasComponent()) ? m_dynamicObjects : m_staticObjects; + entities.Insert(entity); } SystemIndex PhysicsSystem::systemIndex;