From 901b3866be59da1a8a24e947bdd2e08bd6f8739f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?koukaku01=20=E8=A1=8C=E5=AE=A2?= Date: Thu, 17 Sep 2026 18:40:48 +0100 Subject: [PATCH] refactor(ecs): make systems own component signatures --- include/EcoSimEngine/system/AISystem.hpp | 8 +++++ .../EcoSimEngine/system/MetabolismSystem.hpp | 8 +++++ .../EcoSimEngine/system/MovementSystem.hpp | 6 ++++ include/EcoSimEngine/system/SystemManager.hpp | 13 ++------ src/simulation/SimulationWorld.cpp | 25 +------------- tests/foundation_tests.cpp | 33 +++++-------------- 6 files changed, 35 insertions(+), 58 deletions(-) diff --git a/include/EcoSimEngine/system/AISystem.hpp b/include/EcoSimEngine/system/AISystem.hpp index cb699b7..b4bf3fe 100644 --- a/include/EcoSimEngine/system/AISystem.hpp +++ b/include/EcoSimEngine/system/AISystem.hpp @@ -7,10 +7,18 @@ #include "EcoSimEngine/math/Vec2.hpp" #include "EcoSimEngine/system/System.hpp" #include "EcoSimEngine/utils/Random.hpp" +#include "EcoSimEngine/component/ComponentIndices.hpp" class AISystem : public System { public: + static Signature requiredSignature() { + Signature signature; + signature.set(COMP_INDEX_CBehavior); + signature.set(COMP_INDEX_CTransform); + return signature; + } + void update( EntityManager &em, ComponentManager &cm, diff --git a/include/EcoSimEngine/system/MetabolismSystem.hpp b/include/EcoSimEngine/system/MetabolismSystem.hpp index f0f8c77..2bb9d11 100644 --- a/include/EcoSimEngine/system/MetabolismSystem.hpp +++ b/include/EcoSimEngine/system/MetabolismSystem.hpp @@ -5,10 +5,18 @@ #include "EcoSimEngine/ecs/EntityManager.hpp" #include "EcoSimEngine/system/System.hpp" +#include "EcoSimEngine/component/ComponentIndices.hpp" class MetabolismSystem : public System { public: + static Signature requiredSignature() { + Signature signature; + signature.set(COMP_INDEX_CEnergy); + signature.set(COMP_INDEX_CHealth); + return signature; + } + static constexpr float kStarvationDamagePerSecond{10.0f}; // Damage applied to entities that are starving void update( diff --git a/include/EcoSimEngine/system/MovementSystem.hpp b/include/EcoSimEngine/system/MovementSystem.hpp index e57b62c..9cdfcb7 100644 --- a/include/EcoSimEngine/system/MovementSystem.hpp +++ b/include/EcoSimEngine/system/MovementSystem.hpp @@ -8,6 +8,12 @@ class MovementSystem : public System { public: + static Signature requiredSignature() { + Signature signature; + signature.set(COMP_INDEX_CTransform); + return signature; + } + // EntityManager must implement getEntityById(size_t) void update(EntityManager& em, ComponentManager& cm, float dt) { diff --git a/include/EcoSimEngine/system/SystemManager.hpp b/include/EcoSimEngine/system/SystemManager.hpp index 4cac98a..94882ae 100644 --- a/include/EcoSimEngine/system/SystemManager.hpp +++ b/include/EcoSimEngine/system/SystemManager.hpp @@ -27,18 +27,11 @@ class SystemManager { } auto system = std::make_shared(std::forward(args)...); - m_systems.emplace(type, system); - return system; - } - template - void SetSignature(const Signature& signature) { - const std::type_index type(typeid(T)); - if (!m_systems.contains(type)) { - throw std::logic_error("Cannot set a signature for an unregistered system"); - } + m_systems.emplace(type, system); + m_signatures.emplace(type, T::requiredSignature()); - m_signatures[type] = signature; + return system; } void EntityDestroyed(EntityId id) { diff --git a/src/simulation/SimulationWorld.cpp b/src/simulation/SimulationWorld.cpp index 31f274d..b706a76 100644 --- a/src/simulation/SimulationWorld.cpp +++ b/src/simulation/SimulationWorld.cpp @@ -1,7 +1,5 @@ #include "EcoSimEngine/simulation/SimulationWorld.hpp" -#include "EcoSimEngine/component/ComponentIndices.hpp" - #include "EcoSimEngine/system/AISystem.hpp" #include "EcoSimEngine/system/MovementSystem.hpp" #include "EcoSimEngine/system/MetabolismSystem.hpp" @@ -9,31 +7,10 @@ SimulationWorld::SimulationWorld() : m_entityManager(m_systemManager, m_componentManager) { - // Movement system + // Register systems m_systemManager.RegisterSystem(); - - Signature movementSignature; - movementSignature.set(COMP_INDEX_CTransform); - - m_systemManager.SetSignature(movementSignature); - - // AI system m_systemManager.RegisterSystem(); - - Signature aiSignature; - aiSignature.set(COMP_INDEX_CBehavior); - aiSignature.set(COMP_INDEX_CTransform); - - m_systemManager.SetSignature(aiSignature); - - // Metabolism system m_systemManager.RegisterSystem(); - - Signature metabolismSignature; - metabolismSignature.set(COMP_INDEX_CEnergy); - metabolismSignature.set(COMP_INDEX_CHealth); - - m_systemManager.SetSignature(metabolismSignature); } void SimulationWorld::update(float dt) diff --git a/tests/foundation_tests.cpp b/tests/foundation_tests.cpp index 0a66f9b..90dbb38 100644 --- a/tests/foundation_tests.cpp +++ b/tests/foundation_tests.cpp @@ -31,9 +31,13 @@ namespace class TransformSystem final : public System { - }; - class UnregisteredSystem final : public System - { + public: + static Signature requiredSignature() + { + Signature signature; + signature.set(COMP_INDEX_CTransform); + return signature; + } }; void require(bool condition, const char *message) @@ -140,21 +144,17 @@ namespace SystemManager systems; auto transformSystem = systems.RegisterSystem(); - Signature required; - required.set(0); - systems.SetSignature(required); - Signature entitySignature; systems.EntitySignatureChanged(11, entitySignature); require(!transformSystem->mEntities.contains(11), "entity without required components must not join system"); - entitySignature.set(0); + entitySignature.set(COMP_INDEX_CTransform); systems.EntitySignatureChanged(11, entitySignature); require(transformSystem->mEntities.contains(11), "matching entity must join system"); - entitySignature.reset(0); + entitySignature.reset(COMP_INDEX_CTransform); systems.EntitySignatureChanged(11, entitySignature); require(!transformSystem->mEntities.contains(11), "entity must leave system after signature stops matching"); @@ -170,17 +170,6 @@ namespace } require(duplicateRejected, "duplicate system registration must fail in release builds too"); - bool unregisteredSignatureRejected = false; - try - { - systems.SetSignature(required); - } - catch (const std::logic_error &) - { - unregisteredSignatureRejected = true; - } - require(unregisteredSignatureRejected, - "setting a signature for an unregistered system must fail explicitly"); } void testEntityDestruction() @@ -191,10 +180,6 @@ namespace auto transformSystem = systems.RegisterSystem(); - Signature required; - required.set(COMP_INDEX_CTransform); - systems.SetSignature(required); - auto destroyedEntity = entities.addEntity("destroyed"); auto survivingEntity = entities.addEntity("survivor");