ADD: ECS-System
This commit is contained in:
parent
474874d21b
commit
eedee9dd9f
@ -118,6 +118,12 @@ add_executable(ColorRace
|
||||
engine/src/renderer/shader/StaticShader.h
|
||||
engine/src/renderer/entities/Camera.cpp
|
||||
engine/src/renderer/entities/Camera.h
|
||||
engine/src/ecs/EntityRegistry.cpp
|
||||
engine/src/ecs/EntityRegistry.h
|
||||
engine/src/ecs/Entity.cpp
|
||||
engine/src/ecs/Entity.h
|
||||
engine/src/ecs/ComponentPool.cpp
|
||||
engine/src/ecs/ComponentPool.h
|
||||
|
||||
)
|
||||
|
||||
|
||||
5
engine/src/ecs/ComponentPool.cpp
Normal file
5
engine/src/ecs/ComponentPool.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by sebastian on 29.07.26.
|
||||
//
|
||||
|
||||
#include "ComponentPool.h"
|
||||
45
engine/src/ecs/ComponentPool.h
Normal file
45
engine/src/ecs/ComponentPool.h
Normal file
@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by sebastian on 29.07.26.
|
||||
//
|
||||
|
||||
#ifndef COLORRACE_COMPONENTPOOL_H
|
||||
#define COLORRACE_COMPONENTPOOL_H
|
||||
#include "Entity.h"
|
||||
|
||||
|
||||
namespace engine {
|
||||
class IComponentPool {
|
||||
public:
|
||||
virtual ~IComponentPool() = default;
|
||||
virtual void remove(Entity entity) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class ComponentPool : public IComponentPool {
|
||||
public:
|
||||
void remove(Entity entity) override {
|
||||
m_components.erase(entity);
|
||||
}
|
||||
|
||||
void insert(Entity entity, T component) {
|
||||
m_components[entity] = std::move(component);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool has(Entity entity) const {
|
||||
return m_components.contains(entity);
|
||||
}
|
||||
|
||||
T& get(Entity entity) {
|
||||
return m_components.at(entity);
|
||||
}
|
||||
|
||||
// Für Iteration in Systemen
|
||||
auto begin() { return m_components.begin(); }
|
||||
auto end() { return m_components.end(); }
|
||||
|
||||
private:
|
||||
std::unordered_map<Entity, T> m_components;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //COLORRACE_COMPONENTPOOL_H
|
||||
5
engine/src/ecs/Entity.cpp
Normal file
5
engine/src/ecs/Entity.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by sebastian on 29.07.26.
|
||||
//
|
||||
|
||||
#include "Entity.h"
|
||||
39
engine/src/ecs/Entity.h
Normal file
39
engine/src/ecs/Entity.h
Normal file
@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by sebastian on 29.07.26.
|
||||
//
|
||||
|
||||
#ifndef COLORRACE_ENTITY_H
|
||||
#define COLORRACE_ENTITY_H
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <functional> // <- das hier fehlt vermutlich
|
||||
|
||||
namespace engine {
|
||||
class Entity {
|
||||
public:
|
||||
constexpr Entity() noexcept = default;
|
||||
constexpr explicit Entity(std::uint32_t id) noexcept : m_id(id) {}
|
||||
|
||||
[[nodiscard]] constexpr std::uint32_t value() const noexcept { return m_id; }
|
||||
|
||||
constexpr bool operator==(const Entity&) const = default;
|
||||
|
||||
static constexpr Entity null() noexcept {
|
||||
return Entity{std::numeric_limits<std::uint32_t>::max()};
|
||||
}
|
||||
|
||||
private:
|
||||
std::uint32_t m_id = std::numeric_limits<std::uint32_t>::max();
|
||||
};
|
||||
}
|
||||
|
||||
// Damit Entity in unordered_map als Key funktioniert
|
||||
template<>
|
||||
struct std::hash<engine::Entity> {
|
||||
std::size_t operator()(const engine::Entity& e) const noexcept {
|
||||
return std::hash<std::uint32_t>{}(e.value());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //COLORRACE_ENTITY_H
|
||||
5
engine/src/ecs/EntityRegistry.cpp
Normal file
5
engine/src/ecs/EntityRegistry.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by sebastian on 29.07.26.
|
||||
//
|
||||
|
||||
#include "EntityRegistry.h"
|
||||
70
engine/src/ecs/EntityRegistry.h
Normal file
70
engine/src/ecs/EntityRegistry.h
Normal file
@ -0,0 +1,70 @@
|
||||
//
|
||||
// Created by sebastian on 29.07.26.
|
||||
//
|
||||
|
||||
#ifndef COLORRACE_ENTITYREGISTRY_H
|
||||
#define COLORRACE_ENTITYREGISTRY_H
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <typeindex>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "ComponentPool.h"
|
||||
|
||||
|
||||
namespace engine {
|
||||
class EntityRegistry {
|
||||
public:
|
||||
Entity createEntity() {
|
||||
return Entity{m_nextId++};
|
||||
}
|
||||
|
||||
void destroyEntity(Entity entity) {
|
||||
for (auto& [type, pool] : m_pools) {
|
||||
pool->remove(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void addComponent(Entity entity, T component) {
|
||||
getPool<T>().insert(entity, std::move(component));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void removeComponent(Entity entity) {
|
||||
getPool<T>().remove(entity);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] bool hasComponent(Entity entity) {
|
||||
auto key = std::type_index(typeid(T));
|
||||
auto it = m_pools.find(key);
|
||||
if (it == m_pools.end()) return false;
|
||||
return static_cast<ComponentPool<T>&>(*it->second).has(entity);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& getComponent(Entity entity) {
|
||||
return getPool<T>().get(entity);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ComponentPool<T>& getPool() {
|
||||
auto key = std::type_index(typeid(T));
|
||||
auto it = m_pools.find(key);
|
||||
if (it == m_pools.end()) {
|
||||
auto pool = std::make_unique<ComponentPool<T>>();
|
||||
auto* ptr = pool.get();
|
||||
m_pools.emplace(key, std::move(pool));
|
||||
return *ptr;
|
||||
}
|
||||
return static_cast<ComponentPool<T>&>(*it->second);
|
||||
}
|
||||
private:
|
||||
std::uint32_t m_nextId = 0;
|
||||
std::pmr::unordered_map<std::type_index, std::unique_ptr<IComponentPool>> m_pools;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //COLORRACE_ENTITYREGISTRY_H
|
||||
Loading…
Reference in New Issue
Block a user