Renamed BaseComponent to ECS::Component, removed enabled, renamed Position to Base

This commit is contained in:
2026-01-30 17:32:16 +02:00
parent 16c1a2c620
commit ff062567d8
4 changed files with 17 additions and 59 deletions

View File

@@ -11,7 +11,6 @@
#define GUARD_TOURMALINE_ECS_H
#include <any>
#include <format>
#include <stack>
#include <typeindex>
#include "../Containers/DualkeyMap.hpp"
@@ -31,15 +30,13 @@ public:
// ======== 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 <Tourmaline::Systems::Components::Component component,
typename... Args>
template <isAComponent component, typename... Args>
component &AddComponent(const Entity &entity,
Args &&...constructionArguments) {
auto newComponent = entityComponentMap.insert(
@@ -48,7 +45,7 @@ public:
return std::any_cast<component &>(std::get<2>(newComponent));
}
template <Tourmaline::Systems::Components::Component component>
template <isAComponent component>
[[nodiscard("Discarding an expensive operation's result!")]]
component &GetComponent(const Entity &entity) {
auto result = entityComponentMap.query(entity, typeid(component));
@@ -60,13 +57,13 @@ public:
return std::any_cast<component &>(result.begin()->second);
}
template <Tourmaline::Systems::Components::Component component>
template <isAComponent component>
[[nodiscard("Discarding an expensive operation's result!")]]
bool HasComponent(const Entity &entity) {
return entityComponentMap.query(entity, typeid(component)).size();
}
template <Tourmaline::Systems::Components::Component component>
template <isAComponent 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) {
@@ -77,12 +74,6 @@ 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::stack<std::pair<Components::Enabled *, bool>> entitiesToDisable;
// Oh here comes the jank
friend void Components::Enabled::setEnabled(bool);
// ======== Life-cycle ========
void preSystems();
void postSystems();

View File

@@ -13,36 +13,18 @@
#include <concepts>
namespace Tourmaline::Systems::ECS {
class World;
}
struct Component {
public:
virtual ~Component() = default;
};
template <typename T>
concept isAComponent = std::derived_from<T, ECS::Component>;
} // namespace Tourmaline::Systems::ECS
namespace Tourmaline::Systems::Components {
// Base
struct BaseComponent {
public:
virtual ~BaseComponent() = default;
};
template <typename T>
concept Component = std::derived_from<T, BaseComponent>;
// Builtin
struct Position : public BaseComponent {
Position(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
double x, y, z;
};
struct Enabled : public BaseComponent {
Enabled(ECS::World *world) : ownerWorld(world) {}
[[nodiscard]]
bool isEnabled();
void setEnabled(bool enable = true);
private:
bool enabled = true;
ECS::World *ownerWorld;
friend ECS::World;
struct Base : public ECS::Component {
Base() {}
};
} // namespace Tourmaline::Systems::Components
#endif