From 2e03d950a15504b784aca8324d2d41955d7f558f Mon Sep 17 00:00:00 2001 From: Ricardo Antunes Date: Thu, 1 Feb 2024 11:32:36 +0000 Subject: [PATCH] refactor(ecs): remove unused Entity/Component Managers --- core/CMakeLists.txt | 4 - .../cubos/core/ecs/component/manager.hpp | 78 -------- .../cubos/core/ecs/component/module.dox | 9 - .../cubos/core/ecs/component/storage.hpp | 53 ------ .../include/cubos/core/ecs/entity/manager.hpp | 113 ------------ core/include/cubos/core/ecs/world.hpp | 2 - core/src/cubos/core/ecs/component/manager.cpp | 68 ------- core/src/cubos/core/ecs/component/storage.cpp | 48 ----- core/src/cubos/core/ecs/entity/manager.cpp | 167 ------------------ .../broad_phase/sweep_and_prune.hpp | 2 +- 10 files changed, 1 insertion(+), 543 deletions(-) delete mode 100644 core/include/cubos/core/ecs/component/manager.hpp delete mode 100644 core/include/cubos/core/ecs/component/module.dox delete mode 100644 core/include/cubos/core/ecs/component/storage.hpp delete mode 100644 core/include/cubos/core/ecs/entity/manager.hpp delete mode 100644 core/src/cubos/core/ecs/component/manager.cpp delete mode 100644 core/src/cubos/core/ecs/component/storage.cpp delete mode 100644 core/src/cubos/core/ecs/entity/manager.cpp diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index a1a42988c..fbc09584c 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -84,7 +84,6 @@ set(CUBOS_CORE_SOURCE "src/cubos/core/ecs/entity/entity.cpp" "src/cubos/core/ecs/entity/hash.cpp" - "src/cubos/core/ecs/entity/manager.cpp" "src/cubos/core/ecs/entity/archetype_graph.cpp" "src/cubos/core/ecs/entity/archetype_id.cpp" "src/cubos/core/ecs/entity/pool.cpp" @@ -97,9 +96,6 @@ set(CUBOS_CORE_SOURCE "src/cubos/core/ecs/table/sparse_relation/registry.cpp" "src/cubos/core/ecs/table/tables.cpp" - "src/cubos/core/ecs/component/storage.cpp" - "src/cubos/core/ecs/component/manager.cpp" - "src/cubos/core/ecs/system/access.cpp" "src/cubos/core/ecs/system/system.cpp" "src/cubos/core/ecs/system/options.cpp" diff --git a/core/include/cubos/core/ecs/component/manager.hpp b/core/include/cubos/core/ecs/component/manager.hpp deleted file mode 100644 index ee931570b..000000000 --- a/core/include/cubos/core/ecs/component/manager.hpp +++ /dev/null @@ -1,78 +0,0 @@ -/// @file -/// @brief Class @ref cubos::core::ecs::ComponentManager and related types. -/// @ingroup core-ecs-component - -#pragma once - -#include -#include - -#include -#include -#include - -namespace cubos::core::ecs -{ - /// @brief Holds and manages components. - /// - /// Used internally by @ref World. - /// - /// @ingroup core-ecs-component - class ComponentManager final - { - public: - /// @brief Registers a new component type with the component manager. - /// @param type Type of the component. - void registerType(const reflection::Type& type); - - /// @brief Gets the components registered with the component manager. - /// @return Component types. - reflection::TypeRegistry registry() const; - - /// @brief Gets the identifier of a registered component type. - /// @param type Component type. - /// @return Component identifier. - uint32_t id(const reflection::Type& type) const; - - /// @brief Gets the identifier of a registered component type. - /// @tparam T Component type. - /// @return Component identifier. - template - uint32_t id() const - { - return this->id(reflection::reflect()); - } - - /// @brief Gets the type of a component from its identifier. - /// @param id Component identifier. - /// @return Component type index. - const reflection::Type& type(uint32_t id) const; - - /// @brief Adds a component to an entity. - /// @param index Entity index. - /// @param id Component identifier. - /// @param value Component value to move. - void insert(uint32_t index, uint32_t id, void* value); - - /// @brief Removes a component from an entity. - /// @param index Entity index. - /// @param id Component identifier. - void erase(uint32_t index, uint32_t id); - - /// @brief Removes all components from an entity. - /// @param index Entity index. - void erase(uint32_t index); - - /// @brief Gets the storage of the given component type. - /// @param id Component identifier. - /// @return Component storage. - Storage& storage(uint32_t id); - - /// @copydoc storage(uint32_t) - const Storage& storage(uint32_t id) const; - - private: - memory::TypeMap mTypeToIds; ///< Maps component types to component IDs. - std::vector mStorages; ///< Registered component storages. - }; -} // namespace cubos::core::ecs diff --git a/core/include/cubos/core/ecs/component/module.dox b/core/include/cubos/core/ecs/component/module.dox deleted file mode 100644 index 57fc6436b..000000000 --- a/core/include/cubos/core/ecs/component/module.dox +++ /dev/null @@ -1,9 +0,0 @@ -/// @dir -/// @brief @ref core-ecs-component directory. - -namespace cubos::core::ecs -{ - /// @defgroup core-ecs-component Component - /// @ingroup core-ecs - /// @brief Component part of the ECS. -} diff --git a/core/include/cubos/core/ecs/component/storage.hpp b/core/include/cubos/core/ecs/component/storage.hpp deleted file mode 100644 index e7418388f..000000000 --- a/core/include/cubos/core/ecs/component/storage.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/// @file -/// @brief Class @ref cubos::core::ecs::Storage and related types. -/// @ingroup core-ecs-component - -#pragma once - -#include - -#include - -namespace cubos::core::ecs -{ - /// @brief Stores the components of a given type. - /// @ingroup core-ecs-component - class Storage final - { - public: - /// @brief Constructs. - /// @param type Component type. - Storage(const reflection::Type& type); - - /// @brief Move constructs. - /// @param other Moved storage. - Storage(Storage&& other) noexcept = default; - - /// @brief Inserts a value into the storage. - /// @param index Index where to insert the value. - /// @param value Value to be moved. - void insert(uint32_t index, void* value); - - /// @brief Remove a value from the storage. - /// @param index Index of the value to be removed. - void erase(uint32_t index); - - /// @brief Gets a value from the storage. - /// @param index Index of the value to be retrieved. - /// @return Pointer to the value. - void* get(uint32_t index); - - /// @brief Gets a value from the storage. - /// @param index Index of the value to be retrieved. - /// @return Pointer to the value. - const void* get(uint32_t index) const; - - /// @brief Gets the component type stored in the storage. - /// @return Component type. - const reflection::Type& type() const; - - private: - memory::AnyVector mData; - const reflection::ConstructibleTrait* mConstructibleTrait{nullptr}; - }; -} // namespace cubos::core::ecs diff --git a/core/include/cubos/core/ecs/entity/manager.hpp b/core/include/cubos/core/ecs/entity/manager.hpp deleted file mode 100644 index d6adbae10..000000000 --- a/core/include/cubos/core/ecs/entity/manager.hpp +++ /dev/null @@ -1,113 +0,0 @@ -/// @file -/// @brief Class @ref cubos::core::ecs::EntityManager. -/// @ingroup core-ecs-entity - -#pragma once - -#include -#include -#include -#include - -#include -#include - -namespace cubos::core::ecs -{ - /// @brief Internal type which holds and manages entities and their component masks. - /// - /// Used internally by @ref World. - /// - /// @ingroup core-ecs-entity - class EntityManager final - { - public: - /// @brief Used to iterate over all entities in a manager with a certain component mask. - class Iterator - { - public: - Entity operator*() const; - bool operator==(const Iterator& /*other*/) const; - bool operator!=(const Iterator& /*other*/) const; - Iterator& operator++(); - - private: - friend EntityManager; - - const EntityManager& mManager; ///< Entity manager being iterated. - const Entity::Mask mMask; ///< Mask of the components to be iterated. - - /// @param e Entity manager being iterated. - /// @param m Mask of the components to be iterated. - Iterator(const EntityManager& e, Entity::Mask m); - Iterator(const EntityManager& e); - - std::unordered_map>::const_iterator - mArchetypeIt; ///< Current archetype iterator. - std::set::const_iterator mEntityIt; ///< Current entity iterator. - }; - - /// @brief Constructs with a certain initial entity capacity. - /// @param initialCapacity Initial capacity of the entity manager. - EntityManager(std::size_t initialCapacity); - ~EntityManager() = default; - - /// @brief Creates a new entity with a certain component mask. - /// @param mask Component mask of the entity. - /// @return Entity handle. - Entity create(Entity::Mask mask); - - /// @brief Removes an entity from the world. - /// @param entity Entity to remove. - void destroy(Entity entity); - - /// @brief Sets the component mask of an entity. - /// @param entity Entity to set the mask of. - /// @param mask Mask to set. - void setMask(Entity entity, Entity::Mask mask); - - /// @brief Gets the component mask of an entity. - /// @param entity Entity to get the mask of. - /// @return Component mask of the entity. - const Entity::Mask& getMask(Entity entity) const; - - /// @brief Checks if an entity is still valid. - /// - /// Different from isAlive, as it will return true for entities which still have not been - /// commited. - /// - /// @param entity Entity to check. - /// @return Whether the entity is valid. - bool isValid(Entity entity) const; - - /// @brief Checks if an entity is alive. - /// @param entity Entity to check. - /// @return Whether the entity is alive. - bool isAlive(Entity entity) const; - - /// @brief Returns an iterator over all entities. - /// @return Iterator over all entities. - Iterator begin() const; - - /// @brief Returns an iterator over all entities with a certain mask of components. - /// @param mask Mask of the components to be iterated. - /// @return Iterator over all entities with the given component mask. - Iterator withMask(Entity::Mask mask) const; - - /// @brief Returns an iterator which points to the end of the entity manager. - /// @return Iterator which points to the end of the entity manager. - Iterator end() const; - - private: - /// @brief Internal data struct containing the state of an entity. - struct EntityData - { - uint32_t generation; ///< Used to detect if the entity has been removed. - Entity::Mask mask; ///< Component mask of the entity. - }; - - std::vector mEntities; ///< Pool of entities. - std::queue mAvailableEntities; ///< Queue with available entity indices. - std::unordered_map> mArchetypes; ///< Cache archetype entity indices. - }; -} // namespace cubos::core::ecs diff --git a/core/include/cubos/core/ecs/world.hpp b/core/include/cubos/core/ecs/world.hpp index 16708a59d..e2a2a8b6d 100644 --- a/core/include/cubos/core/ecs/world.hpp +++ b/core/include/cubos/core/ecs/world.hpp @@ -7,9 +7,7 @@ #include #include -#include #include -#include #include #include #include diff --git a/core/src/cubos/core/ecs/component/manager.cpp b/core/src/cubos/core/ecs/component/manager.cpp deleted file mode 100644 index ff0180cc4..000000000 --- a/core/src/cubos/core/ecs/component/manager.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include -#include -#include - -using cubos::core::reflection::Type; -using cubos::core::reflection::TypeRegistry; - -using namespace cubos::core::ecs; - -void ComponentManager::registerType(const Type& type) -{ - if (!mTypeToIds.contains(type)) - { - mTypeToIds.insert(type, static_cast(mStorages.size()) + 1); // Component ids start at 1. - mStorages.emplace_back(type); - } -} - -TypeRegistry ComponentManager::registry() const -{ - TypeRegistry registry{}; - for (const auto& [type, _] : mTypeToIds) - { - registry.insert(*type); - } - return registry; -} - -uint32_t ComponentManager::id(const Type& type) const -{ - CUBOS_ASSERT(mTypeToIds.contains(type), "Component type {} was not registered", type.name()); - return mTypeToIds.at(type); -} - -const Type& ComponentManager::type(uint32_t id) const -{ - return mStorages[static_cast(id) - 1].type(); -} - -void ComponentManager::insert(uint32_t index, uint32_t id, void* value) -{ - mStorages[static_cast(id) - 1].insert(index, value); -} - -void ComponentManager::erase(uint32_t index, uint32_t id) -{ - mStorages[static_cast(id) - 1].erase(index); -} - -void ComponentManager::erase(uint32_t index) -{ - for (auto& storage : mStorages) - { - storage.erase(index); - } -} - -Storage& ComponentManager::storage(uint32_t id) -{ - return mStorages[static_cast(id) - 1]; -} - -const Storage& ComponentManager::storage(uint32_t id) const -{ - return mStorages[static_cast(id) - 1]; -} diff --git a/core/src/cubos/core/ecs/component/storage.cpp b/core/src/cubos/core/ecs/component/storage.cpp deleted file mode 100644 index 80c7fba83..000000000 --- a/core/src/cubos/core/ecs/component/storage.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include - -using cubos::core::ecs::Storage; -using cubos::core::reflection::Type; - -Storage::Storage(const Type& type) - : mData{type} -{ - mConstructibleTrait = &type.get(); -} - -void Storage::insert(uint32_t index, void* value) -{ - while (mData.size() <= static_cast(index)) - { - mData.pushDefault(); - } - - mConstructibleTrait->destruct(mData.at(index)); - mConstructibleTrait->moveConstruct(mData.at(index), value); -} - -void Storage::erase(uint32_t index) -{ - if (static_cast(index) < mData.size()) - { - void* value = mData.at(static_cast(index)); - mConstructibleTrait->destruct(value); - mConstructibleTrait->defaultConstruct(value); - } -} - -void* Storage::get(uint32_t index) -{ - return mData.at(static_cast(index)); -} - -const void* Storage::get(uint32_t index) const -{ - return mData.at(static_cast(index)); -} - -const Type& Storage::type() const -{ - return mData.elementType(); -} diff --git a/core/src/cubos/core/ecs/entity/manager.cpp b/core/src/cubos/core/ecs/entity/manager.cpp deleted file mode 100644 index b6196d6cb..000000000 --- a/core/src/cubos/core/ecs/entity/manager.cpp +++ /dev/null @@ -1,167 +0,0 @@ -#include - -using namespace cubos::core::ecs; - -EntityManager::Iterator::Iterator(const EntityManager& e, const Entity::Mask m) - : mManager(e) - , mMask(m) -{ - if (!m.test(0)) - { - abort(); // You can't iterate over invalid entities. - } - - mArchetypeIt = mManager.mArchetypes.begin(); - while (mArchetypeIt != mManager.mArchetypes.end() && - ((mArchetypeIt->first & mMask) != mMask || mArchetypeIt->second.empty())) - { - ++mArchetypeIt; - } - - if (mArchetypeIt != mManager.mArchetypes.end()) - { - mEntityIt = mArchetypeIt->second.begin(); - } -} - -EntityManager::Iterator::Iterator(const EntityManager& e) - : mManager(e) -{ - mArchetypeIt = mManager.mArchetypes.end(); -} - -Entity EntityManager::Iterator::operator*() const -{ - return {*mEntityIt, mManager.mEntities[*mEntityIt].generation}; -} - -bool EntityManager::Iterator::operator==(const Iterator& other) const -{ - if (other.mArchetypeIt == mManager.mArchetypes.end()) - { - return mArchetypeIt == mManager.mArchetypes.end(); - } - - return mArchetypeIt == other.mArchetypeIt && mEntityIt == other.mEntityIt; -} - -bool EntityManager::Iterator::operator!=(const Iterator& other) const -{ - return !(*this == other); -} - -EntityManager::Iterator& EntityManager::Iterator::operator++() -{ - if (mArchetypeIt != mManager.mArchetypes.end()) - { - if (mEntityIt != mArchetypeIt->second.end()) - { - ++mEntityIt; - } - - if (mEntityIt == mArchetypeIt->second.end()) - { - // Move to the next archetype. - do - { - ++mArchetypeIt; - } while (mArchetypeIt != mManager.mArchetypes.end() && - ((mArchetypeIt->first & mMask) != mMask || mArchetypeIt->second.empty())); - - if (mArchetypeIt != mManager.mArchetypes.end()) - { - mEntityIt = mArchetypeIt->second.begin(); - } - } - } - - return *this; -} - -EntityManager::EntityManager(std::size_t initialCapacity) -{ - mEntities.reserve(initialCapacity); - for (std::size_t i = 0; i < initialCapacity; ++i) - { - mEntities.push_back(EntityData{0, 1}); - mAvailableEntities.push(static_cast(i)); - } -} - -Entity EntityManager::create(Entity::Mask mask) -{ - if (mAvailableEntities.empty()) - { - // Expand the entity pool. - std::size_t oldSize = mEntities.size(); - mEntities.reserve(oldSize * 2); - for (std::size_t i = oldSize; i < oldSize * 2; ++i) - { - mEntities.push_back(EntityData{0, 0}); - mAvailableEntities.push(static_cast(i)); - } - } - - uint32_t index = mAvailableEntities.front(); - mAvailableEntities.pop(); - mEntities[index].mask = mask; - if (mask.any() && mask.test(0)) - { - mArchetypes[mask].insert(index); - } - - return {index, mEntities[index].generation}; -} - -void EntityManager::destroy(Entity entity) -{ - this->setMask(entity, 0); - mEntities[entity.index].generation += 1; - mAvailableEntities.push(entity.index); -} - -void EntityManager::setMask(Entity entity, Entity::Mask mask) -{ - if (mEntities[entity.index].mask != mask) - { - if (mEntities[entity.index].mask.any() && mEntities[entity.index].mask.test(0)) - { - mArchetypes[mEntities[entity.index].mask].erase(entity.index); - } - mEntities[entity.index].mask = mask; - if (mask.any() && mask.test(0)) - { - mArchetypes[mask].insert(entity.index); - } - } -} - -const Entity::Mask& EntityManager::getMask(Entity entity) const -{ - return mEntities[entity.index].mask; -} - -bool EntityManager::isValid(Entity entity) const -{ - return entity.index < mEntities.size() && mEntities[entity.index].generation == entity.generation; -} - -bool EntityManager::isAlive(Entity entity) const -{ - return this->isValid(entity) && mEntities[entity.index].mask.test(0); -} - -EntityManager::Iterator EntityManager::begin() const -{ - return {*this, Entity::Mask(1)}; -} - -EntityManager::Iterator EntityManager::withMask(Entity::Mask mask) const -{ - return {*this, mask}; -} - -EntityManager::Iterator EntityManager::end() const -{ - return {*this}; -} diff --git a/engine/src/cubos/engine/collisions/broad_phase/sweep_and_prune.hpp b/engine/src/cubos/engine/collisions/broad_phase/sweep_and_prune.hpp index 5d3c4f9b6..e055c47e4 100644 --- a/engine/src/cubos/engine/collisions/broad_phase/sweep_and_prune.hpp +++ b/engine/src/cubos/engine/collisions/broad_phase/sweep_and_prune.hpp @@ -8,8 +8,8 @@ #include #include +#include #include -#include #include