Renamed BaseComponent to ECS::Component, removed enabled, renamed Position to Base
This commit is contained in:
@@ -11,7 +11,6 @@
|
|||||||
#define GUARD_TOURMALINE_ECS_H
|
#define GUARD_TOURMALINE_ECS_H
|
||||||
#include <any>
|
#include <any>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <stack>
|
|
||||||
#include <typeindex>
|
#include <typeindex>
|
||||||
|
|
||||||
#include "../Containers/DualkeyMap.hpp"
|
#include "../Containers/DualkeyMap.hpp"
|
||||||
@@ -31,15 +30,13 @@ public:
|
|||||||
// ======== Entities ========
|
// ======== 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 <Tourmaline::Systems::Components::Component component,
|
template <isAComponent component, typename... Args>
|
||||||
typename... Args>
|
|
||||||
component &AddComponent(const Entity &entity,
|
component &AddComponent(const Entity &entity,
|
||||||
Args &&...constructionArguments) {
|
Args &&...constructionArguments) {
|
||||||
auto newComponent = entityComponentMap.insert(
|
auto newComponent = entityComponentMap.insert(
|
||||||
@@ -48,7 +45,7 @@ public:
|
|||||||
return std::any_cast<component &>(std::get<2>(newComponent));
|
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!")]]
|
[[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));
|
||||||
@@ -60,13 +57,13 @@ public:
|
|||||||
return std::any_cast<component &>(result.begin()->second);
|
return std::any_cast<component &>(result.begin()->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <Tourmaline::Systems::Components::Component component>
|
template <isAComponent 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 <Tourmaline::Systems::Components::Component component>
|
template <isAComponent 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) {
|
||||||
@@ -77,12 +74,6 @@ 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::stack<std::pair<Components::Enabled *, bool>> entitiesToDisable;
|
|
||||||
// Oh here comes the jank
|
|
||||||
friend void Components::Enabled::setEnabled(bool);
|
|
||||||
|
|
||||||
// ======== Life-cycle ========
|
// ======== Life-cycle ========
|
||||||
void preSystems();
|
void preSystems();
|
||||||
void postSystems();
|
void postSystems();
|
||||||
|
|||||||
@@ -13,36 +13,18 @@
|
|||||||
#include <concepts>
|
#include <concepts>
|
||||||
|
|
||||||
namespace Tourmaline::Systems::ECS {
|
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 {
|
namespace Tourmaline::Systems::Components {
|
||||||
// Base
|
|
||||||
struct BaseComponent {
|
|
||||||
public:
|
|
||||||
virtual ~BaseComponent() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
concept Component = std::derived_from<T, BaseComponent>;
|
|
||||||
|
|
||||||
// Builtin
|
// Builtin
|
||||||
struct Position : public BaseComponent {
|
struct Base : public ECS::Component {
|
||||||
Position(double x = 0, double y = 0, double z = 0) : x(x), y(y), z(z) {}
|
Base() {}
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
} // namespace Tourmaline::Systems::Components
|
} // namespace Tourmaline::Systems::Components
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,9 +9,5 @@
|
|||||||
|
|
||||||
#include <Systems/ECS.hpp>
|
#include <Systems/ECS.hpp>
|
||||||
#include <Systems/ECS/BuiltinComponents.hpp>
|
#include <Systems/ECS/BuiltinComponents.hpp>
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
bool Tourmaline::Systems::Components::Enabled::isEnabled() { return enabled; }
|
// Empty until future use
|
||||||
void Tourmaline::Systems::Components::Enabled::setEnabled(bool enable) {
|
|
||||||
ownerWorld->entitiesToDisable.emplace(std::pair{this, enable});
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -26,12 +26,7 @@ void World::preSystems() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::postSystems() {
|
void World::postSystems() {
|
||||||
// Can't do a foreach with a std::stack
|
// Defined for future use
|
||||||
while (!entitiesToDisable.empty()) {
|
|
||||||
std::pair<Components::Enabled *, bool> &request = entitiesToDisable.top();
|
|
||||||
request.first->enabled = request.second;
|
|
||||||
entitiesToDisable.pop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entities
|
// Entities
|
||||||
@@ -39,18 +34,12 @@ Entity World::CreateEntity() {
|
|||||||
auto newEntity = Random::GenerateUUID();
|
auto newEntity = Random::GenerateUUID();
|
||||||
|
|
||||||
// Default components
|
// Default components
|
||||||
entityComponentMap.insert(newEntity, typeid(Components::Position),
|
entityComponentMap.insert(newEntity, typeid(Components::Base),
|
||||||
Components::Position());
|
Components::Base());
|
||||||
entityComponentMap.insert(newEntity, typeid(Components::Enabled),
|
|
||||||
Components::Enabled(this));
|
|
||||||
|
|
||||||
return newEntity;
|
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 {
|
||||||
bool exists = false;
|
bool exists = false;
|
||||||
entityComponentMap.scan(
|
entityComponentMap.scan(
|
||||||
|
|||||||
Reference in New Issue
Block a user