From dadbfbc0857ec2d885f12507ed8da342818a7297 Mon Sep 17 00:00:00 2001 From: cat Date: Fri, 30 Jan 2026 10:14:06 +0200 Subject: [PATCH] Continuing to add Enabled --- headers/Systems/ECS.hpp | 34 +++++++++++++++------------------- source/Systems/ECS/World.cpp | 21 ++++++++++++++------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/headers/Systems/ECS.hpp b/headers/Systems/ECS.hpp index 654a3b0..a450689 100644 --- a/headers/Systems/ECS.hpp +++ b/headers/Systems/ECS.hpp @@ -10,42 +10,32 @@ #ifndef GUARD_TOURMALINE_ECS_H #define GUARD_TOURMALINE_ECS_H #include -#include #include #include #include "../Containers/DualkeyMap.hpp" #include "../Types.hpp" +#include "ECS/BuiltinComponents.hpp" #include "Logging.hpp" namespace Tourmaline::Systems::ECS { using Entity = Tourmaline::Type::UUID; class World; -struct BaseComponent { -public: - virtual ~BaseComponent() = default; - -private: - friend World; -}; - -// Concepts -template -concept Component = std::derived_from; - class World { public: - // Entity + // ======== Entities ======== [[nodiscard]] Entity CreateEntity(); + const Components::Enabled &EntityEnable(const Entity &entity) noexcept; bool EntityExists(const Entity &entity) noexcept; [[nodiscard("It is not guaranteed that an entity can always be destroyed, " "please make sure by checking the returned bool")]] bool DestroyEntity(Entity entity); - // Components - template + // ======== Components ======== + template component &AddComponent(const Entity &entity, Args &&...constructionArguments) { auto newComponent = entityComponentMap.insert( @@ -54,7 +44,7 @@ public: return std::any_cast(std::get<2>(newComponent)); } - template + template [[nodiscard("Discarding an expensive operation's result!")]] component &GetComponent(const Entity &entity) { auto result = entityComponentMap.query(entity, typeid(component)); @@ -66,13 +56,13 @@ public: return std::any_cast(result.begin()->second); } - template + template [[nodiscard("Discarding an expensive operation's result!")]] bool HasComponent(const Entity &entity) { return entityComponentMap.query(entity, typeid(component)).size(); } - template + template [[nodiscard("It is not guaranteed that a component can always be removed, " "please make sure by checking the returned bool")]] bool RemoveComponent(const Entity &entity) { @@ -82,6 +72,12 @@ public: private: Tourmaline::Containers::DualkeyMap entityComponentMap{}; + + // All of this is done so entities are not disabled during the + // run of the systems + std::vector> entitiesToDisable; + // Oh here comes the jank + friend void Components::Enabled::setEnabled(bool); }; } // namespace Tourmaline::Systems::ECS #endif diff --git a/source/Systems/ECS/World.cpp b/source/Systems/ECS/World.cpp index 70b1341..70dd1a1 100644 --- a/source/Systems/ECS/World.cpp +++ b/source/Systems/ECS/World.cpp @@ -12,16 +12,23 @@ #include #include -using namespace Tourmaline::Systems::ECS; +using namespace Tourmaline::Systems; +using namespace ECS; -// It is preferable to send a copy of the UUID instead of reference since -// the entity itself may be destroyed in the memory Entity World::CreateEntity() { - auto newEntity = entityComponentMap.insert( - Random::GenerateUUID(), typeid(Tourmaline::Systems::Components::Position), - Tourmaline::Systems::Components::Position()); + auto newEntity = Random::GenerateUUID(); - return Entity(std::get<0>(newEntity)); + // Default components + entityComponentMap.insert(newEntity, typeid(Components::Position), + Components::Position()); + entityComponentMap.insert(newEntity, typeid(Components::Enabled), + Components::Enabled(this)); + + return newEntity; +} + +const Components::Enabled &World::EntityEnable(const Entity &entity) noexcept { + return this->GetComponent(entity); } bool World::EntityExists(const Entity &entity) noexcept {