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
#define GUARD_TOURMALINE_ECS_H
#include <any>
#include <concepts>
#include <format>
#include <typeindex>
#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 <typename T>
concept Component = std::derived_from<T, BaseComponent>;
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 <Component component, typename... Args>
// ======== Components ========
template <Tourmaline::Systems::Components::Component component,
typename... Args>
component &AddComponent(const Entity &entity,
Args &&...constructionArguments) {
auto newComponent = entityComponentMap.insert(
@@ -54,7 +44,7 @@ public:
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!")]]
component &GetComponent(const Entity &entity) {
auto result = entityComponentMap.query(entity, typeid(component));
@@ -66,13 +56,13 @@ public:
return std::any_cast<component &>(result.begin()->second);
}
template <Component component>
template <Tourmaline::Systems::Components::Component component>
[[nodiscard("Discarding an expensive operation's result!")]]
bool HasComponent(const Entity &entity) {
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, "
"please make sure by checking the returned bool")]]
bool RemoveComponent(const Entity &entity) {
@@ -82,6 +72,12 @@ public:
private:
Tourmaline::Containers::DualkeyMap<Entity, std::type_index, std::any>
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
#endif

View File

@@ -12,16 +12,23 @@
#include <Systems/Random.hpp>
#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() {
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<Components::Enabled>(entity);
}
bool World::EntityExists(const Entity &entity) noexcept {