Compare commits

..

2 Commits

Author SHA1 Message Date
cat
4034cd4732 Continuing to add Enabled 2026-01-30 10:14:06 +02:00
cat
610a22a852 Moved base component from ECS.hpp and added Enabled 2026-01-30 10:12:14 +02:00
4 changed files with 80 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

@@ -7,11 +7,44 @@
* obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "../ECS.hpp"
#ifndef GUARD_TOURMALINE_BUILTIN_COMPONENTS_H
#define GUARD_TOURMALINE_BUILTIN_COMPONENTS_H
#include <concepts>
namespace Tourmaline::Systems::ECS {
class World;
}
namespace Tourmaline::Systems::Components {
struct Position : public Tourmaline::Systems::ECS::BaseComponent {
// Base
struct BaseComponent {
public:
virtual ~BaseComponent() = default;
private:
friend class World;
};
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(Tourmaline::Systems::ECS::World *world) : ownerWorld(world) {}
[[nodiscard]]
bool isEnabled();
void setEnabled(bool enable = true);
private:
bool enabled = true;
Tourmaline::Systems::ECS::World *ownerWorld;
};
} // namespace Tourmaline::Systems::Components
#endif

View File

@@ -0,0 +1,16 @@
/*
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <Systems/ECS.hpp>
#include <Systems/ECS/BuiltinComponents.hpp>
bool Tourmaline::Systems::Components::Enabled::isEnabled() { return enabled; }
void Tourmaline::Systems::Components::Enabled::setEnabled(bool enable) {
ownerWorld->entitiesToDisable.push_back({this, enable});
}

View File

@@ -12,16 +12,25 @@
#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 {