From cc2cca2442e49293644c0c2672638b6849e725e6 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Fri, 22 May 2026 06:34:57 +0200 Subject: [PATCH] add EntityAdmin wrapper --- src/lib/core/CMakeLists.txt | 2 + src/lib/core/EntityAdmin.cpp | 33 ++++++++ src/lib/core/EntityAdmin.h | 153 +++++++++++++++++++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 src/lib/core/EntityAdmin.cpp create mode 100644 src/lib/core/EntityAdmin.h diff --git a/src/lib/core/CMakeLists.txt b/src/lib/core/CMakeLists.txt index cf8aaff..b74c37d 100644 --- a/src/lib/core/CMakeLists.txt +++ b/src/lib/core/CMakeLists.txt @@ -4,6 +4,7 @@ SET(HDRS ${CMAKE_CURRENT_SOURCE_DIR}/EntityId.h ${CMAKE_CURRENT_SOURCE_DIR}/Rotation.h ${CMAKE_CURRENT_SOURCE_DIR}/BuildingType.h + ${CMAKE_CURRENT_SOURCE_DIR}/EntityAdmin.h ${CMAKE_CURRENT_SOURCE_DIR}/Blueprint.h ${CMAKE_CURRENT_SOURCE_DIR}/ItemType.h ${CMAKE_CURRENT_SOURCE_DIR}/Item.h @@ -17,6 +18,7 @@ SET(HDRS SET(SRCS ${SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/BuildingType.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/EntityAdmin.cpp PARENT_SCOPE ) diff --git a/src/lib/core/EntityAdmin.cpp b/src/lib/core/EntityAdmin.cpp new file mode 100644 index 0000000..e7fecac --- /dev/null +++ b/src/lib/core/EntityAdmin.cpp @@ -0,0 +1,33 @@ +#include "EntityAdmin.h" + +std::shared_ptr EntityAdmin::getInstance() +{ + if (!s_instance) + { + s_instance = std::shared_ptr(new EntityAdmin()); + } + return s_instance; +} + +std::shared_ptr EntityAdmin::s_instance; + +bool EntityAdmin::isValid(const entt::entity &entity) +{ + return m_registry->valid(entity); +} + +void EntityAdmin::clear() +{ + m_registry->clear(); +} + +void EntityAdmin::destroy(entt::entity &entity) +{ + m_registry->destroy(entity); +} + +EntityAdmin::EntityAdmin() + : m_registry(std::make_shared()) +{ +} + diff --git a/src/lib/core/EntityAdmin.h b/src/lib/core/EntityAdmin.h new file mode 100644 index 0000000..572e26c --- /dev/null +++ b/src/lib/core/EntityAdmin.h @@ -0,0 +1,153 @@ +#ifndef ENTITY_ADMIN_H +#define ENTITY_ADMIN_H + +#include "entt/entity/registry.hpp" + +#include + +class EntityAdmin +{ +public: + static std::shared_ptr getInstance(); +private: + static std::shared_ptr s_instance; + +public: + template + T1 &add(const entt::entity &entity); + void clear(); + + template + void forEach(std::function f); + template + void forEach(std::function f); + template + void forEach(std::function f); + template + void forEach(std::function f); + template + void forEach(std::function f); + + template + bool hasAll(const entt::entity &entity); + template + bool hasAll(const entt::entity &entity); + template + bool hasAll(const entt::entity &entity); + template + bool hasAll(const entt::entity &entity); + + template + T &get(const entt::entity &entity); + + bool isValid(const entt::entity &entity); + + /* + factory methods (like spawnShip, spawnScrap, etc shall go here) + */ + + void destroy(entt::entity &entity); + +private: + EntityAdmin(); + EntityAdmin(const EntityAdmin&) = delete; + EntityAdmin& operator=(const EntityAdmin&) = delete; + + std::shared_ptr m_registry; +}; + +template +T1 &EntityAdmin::add(const entt::entity &entity) +{ + return m_registry->emplace(entity); +} + +template +void EntityAdmin::forEach(std::function f) +{ + auto view = m_registry->view(); + for (auto ae : view) + { + f(ae, view.get(ae)); + } +} + +template +void EntityAdmin::forEach(std::function f) +{ + auto view = m_registry->view(); + for (auto[ae, a1, a2] : view.each()) + { + f(ae, a1, a2); + } +} + +template +void EntityAdmin::forEach(std::function f) +{ + auto view = m_registry->view(); + for (auto[ae, a1, a2, a3] : view.each()) + { + f(ae, a1, a2, a3); + } +} + +template +void EntityAdmin::forEach(std::function f) +{ + auto view = m_registry->view(); + for (auto[ae, a1, a2, a3, a4] : view.each()) + { + f(ae, a1, a2, a3, a4); + } +} + +template +void EntityAdmin::forEach(std::function f) +{ + auto view = m_registry->view(); + for (auto[ae, a1, a2, a3, a4, a5] : view.each()) + { + f(ae, a1, a2, a3, a4, a5); + } +} + +template +bool EntityAdmin::hasAll(const entt::entity &entity) +{ + if (m_registry->all_of(entity)) + return true; + return false; +} + +template +bool EntityAdmin::hasAll(const entt::entity &entity) +{ + if (m_registry->all_of(entity)) + return true; + return false; +} + +template +bool EntityAdmin::hasAll(const entt::entity &entity) +{ + if (m_registry->all_of(entity)) + return true; + return false; +} + +template +bool EntityAdmin::hasAll(const entt::entity &entity) +{ + if (m_registry->all_of(entity)) + return true; + return false; +} + +template +T &EntityAdmin::get(const entt::entity &entity) +{ + return m_registry->get(entity); +} + +#endif // ENTITY_ADMIN_H