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
|
||||
#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();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -9,9 +9,5 @@
|
||||
|
||||
#include <Systems/ECS.hpp>
|
||||
#include <Systems/ECS/BuiltinComponents.hpp>
|
||||
#include <utility>
|
||||
|
||||
bool Tourmaline::Systems::Components::Enabled::isEnabled() { return enabled; }
|
||||
void Tourmaline::Systems::Components::Enabled::setEnabled(bool enable) {
|
||||
ownerWorld->entitiesToDisable.emplace(std::pair{this, enable});
|
||||
}
|
||||
// Empty until future use
|
||||
|
||||
@@ -26,12 +26,7 @@ void World::preSystems() {
|
||||
}
|
||||
|
||||
void World::postSystems() {
|
||||
// Can't do a foreach with a std::stack
|
||||
while (!entitiesToDisable.empty()) {
|
||||
std::pair<Components::Enabled *, bool> &request = entitiesToDisable.top();
|
||||
request.first->enabled = request.second;
|
||||
entitiesToDisable.pop();
|
||||
}
|
||||
// Defined for future use
|
||||
}
|
||||
|
||||
// Entities
|
||||
@@ -39,18 +34,12 @@ Entity World::CreateEntity() {
|
||||
auto newEntity = Random::GenerateUUID();
|
||||
|
||||
// Default components
|
||||
entityComponentMap.insert(newEntity, typeid(Components::Position),
|
||||
Components::Position());
|
||||
entityComponentMap.insert(newEntity, typeid(Components::Enabled),
|
||||
Components::Enabled(this));
|
||||
entityComponentMap.insert(newEntity, typeid(Components::Base),
|
||||
Components::Base());
|
||||
|
||||
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 exists = false;
|
||||
entityComponentMap.scan(
|
||||
|
||||
Reference in New Issue
Block a user