Continuing to add Enabled

This commit is contained in:
2026-01-30 10:14:06 +02:00
parent 610a22a852
commit dadbfbc085
2 changed files with 29 additions and 26 deletions

View File

@@ -10,42 +10,32 @@
#ifndef GUARD_TOURMALINE_ECS_H #ifndef GUARD_TOURMALINE_ECS_H
#define GUARD_TOURMALINE_ECS_H #define GUARD_TOURMALINE_ECS_H
#include <any> #include <any>
#include <concepts>
#include <format> #include <format>
#include <typeindex> #include <typeindex>
#include "../Containers/DualkeyMap.hpp" #include "../Containers/DualkeyMap.hpp"
#include "../Types.hpp" #include "../Types.hpp"
#include "ECS/BuiltinComponents.hpp"
#include "Logging.hpp" #include "Logging.hpp"
namespace Tourmaline::Systems::ECS { namespace Tourmaline::Systems::ECS {
using Entity = Tourmaline::Type::UUID; using Entity = Tourmaline::Type::UUID;
class World; class World;
struct BaseComponent {
public:
virtual ~BaseComponent() = default;
private:
friend World;
};
// Concepts
template <typename T>
concept Component = std::derived_from<T, BaseComponent>;
class World { class World {
public: public:
// Entity // ======== Entities ========
[[nodiscard]] [[nodiscard]]
Entity CreateEntity(); Entity CreateEntity();
const Components::Enabled &EntityEnable(const Entity &entity) noexcept;
bool EntityExists(const Entity &entity) noexcept; bool EntityExists(const Entity &entity) noexcept;
[[nodiscard("It is not guaranteed that an entity can always be destroyed, " [[nodiscard("It is not guaranteed that an entity can always be destroyed, "
"please make sure by checking the returned bool")]] "please make sure by checking the returned bool")]]
bool DestroyEntity(Entity entity); bool DestroyEntity(Entity entity);
// Components // ======== Components ========
template <Component component, typename... Args> template <Tourmaline::Systems::Components::Component component,
typename... Args>
component &AddComponent(const Entity &entity, component &AddComponent(const Entity &entity,
Args &&...constructionArguments) { Args &&...constructionArguments) {
auto newComponent = entityComponentMap.insert( auto newComponent = entityComponentMap.insert(
@@ -54,7 +44,7 @@ public:
return std::any_cast<component &>(std::get<2>(newComponent)); return std::any_cast<component &>(std::get<2>(newComponent));
} }
template <Component component> template <Tourmaline::Systems::Components::Component component>
[[nodiscard("Discarding an expensive operation's result!")]] [[nodiscard("Discarding an expensive operation's result!")]]
component &GetComponent(const Entity &entity) { component &GetComponent(const Entity &entity) {
auto result = entityComponentMap.query(entity, typeid(component)); auto result = entityComponentMap.query(entity, typeid(component));
@@ -66,13 +56,13 @@ public:
return std::any_cast<component &>(result.begin()->second); return std::any_cast<component &>(result.begin()->second);
} }
template <Component component> template <Tourmaline::Systems::Components::Component component>
[[nodiscard("Discarding an expensive operation's result!")]] [[nodiscard("Discarding an expensive operation's result!")]]
bool HasComponent(const Entity &entity) { bool HasComponent(const Entity &entity) {
return entityComponentMap.query(entity, typeid(component)).size(); return entityComponentMap.query(entity, typeid(component)).size();
} }
template <Component component> template <Tourmaline::Systems::Components::Component component>
[[nodiscard("It is not guaranteed that a component can always be removed, " [[nodiscard("It is not guaranteed that a component can always be removed, "
"please make sure by checking the returned bool")]] "please make sure by checking the returned bool")]]
bool RemoveComponent(const Entity &entity) { bool RemoveComponent(const Entity &entity) {
@@ -82,6 +72,12 @@ public:
private: private:
Tourmaline::Containers::DualkeyMap<Entity, std::type_index, std::any> Tourmaline::Containers::DualkeyMap<Entity, std::type_index, std::any>
entityComponentMap{}; entityComponentMap{};
// All of this is done so entities are not disabled during the
// run of the systems
std::vector<std::pair<Components::Enabled *, bool>> entitiesToDisable;
// Oh here comes the jank
friend void Components::Enabled::setEnabled(bool);
}; };
} // namespace Tourmaline::Systems::ECS } // namespace Tourmaline::Systems::ECS
#endif #endif

View File

@@ -12,16 +12,23 @@
#include <Systems/Random.hpp> #include <Systems/Random.hpp>
#include <optional> #include <optional>
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() { Entity World::CreateEntity() {
auto newEntity = entityComponentMap.insert( auto newEntity = Random::GenerateUUID();
Random::GenerateUUID(), typeid(Tourmaline::Systems::Components::Position),
Tourmaline::Systems::Components::Position());
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<Components::Enabled>(entity);
} }
bool World::EntityExists(const Entity &entity) noexcept { bool World::EntityExists(const Entity &entity) noexcept {