Compare commits
2 Commits
f760cfd658
...
4034cd4732
| Author | SHA1 | Date | |
|---|---|---|---|
| 4034cd4732 | |||
| 610a22a852 |
@@ -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
|
||||||
|
|||||||
@@ -7,11 +7,44 @@
|
|||||||
* obtain one at http://mozilla.org/MPL/2.0/.
|
* 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 {
|
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) {}
|
Position(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
|
||||||
double x, y, 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
|
} // namespace Tourmaline::Systems::Components
|
||||||
|
#endif
|
||||||
|
|||||||
16
source/Systems/ECS/Components.cpp
Normal file
16
source/Systems/ECS/Components.cpp
Normal 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});
|
||||||
|
}
|
||||||
@@ -12,16 +12,25 @@
|
|||||||
#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
|
// It is preferable to send a copy of the UUID instead of reference since
|
||||||
// the entity itself may be destroyed in the memory
|
// 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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user