engine/include/ecs.h

71 lines
1.5 KiB
C
Raw Permalink Normal View History

2024-03-31 10:11:22 +00:00
#pragma once
2023-01-02 17:24:20 +00:00
2023-04-29 14:22:25 +00:00
#include <cassert>
#include <cstddef>
2023-01-02 17:24:20 +00:00
#include <cstdint>
2024-06-04 18:01:49 +00:00
#include <bitset>
2023-04-29 14:22:25 +00:00
#include <map>
#include <set>
2024-06-04 18:01:49 +00:00
#include <vector>
2023-04-24 10:53:43 +00:00
2024-06-04 18:01:49 +00:00
#include "entity.h"
2023-01-02 17:24:20 +00:00
2024-06-04 18:01:49 +00:00
namespace engine {
2023-04-29 14:22:25 +00:00
2024-06-04 18:01:49 +00:00
class Scene; // forward-dec
2023-09-19 07:40:45 +00:00
2024-06-04 18:01:49 +00:00
constexpr size_t MAX_COMPONENTS = 10;
2023-04-29 14:22:25 +00:00
class IComponentArray {
2024-06-04 18:01:49 +00:00
public:
2024-03-31 10:11:22 +00:00
virtual ~IComponentArray() = default;
2023-04-29 14:22:25 +00:00
};
2023-01-02 17:24:20 +00:00
2023-04-29 14:56:49 +00:00
template <typename T>
2023-04-29 14:22:25 +00:00
class ComponentArray : public IComponentArray {
2024-06-04 18:01:49 +00:00
private:
std::vector<T> m_component_array{};
public:
2024-07-29 17:58:33 +00:00
void insertData(Entity entity, T&& component)
2024-03-31 10:11:22 +00:00
{
2024-06-04 18:01:49 +00:00
if (m_component_array.size() < entity + 1) {
m_component_array.resize(entity + 1);
2024-03-31 10:11:22 +00:00
}
// bounds checking here as not performance critical
2024-07-29 17:58:33 +00:00
m_component_array.at(entity) = std::move(component);
}
2023-01-02 17:24:20 +00:00
2024-06-04 18:01:49 +00:00
void removeData(Entity entity)
2024-03-31 10:11:22 +00:00
{
(void)entity; // TODO
}
2023-04-29 14:56:49 +00:00
2024-06-04 18:01:49 +00:00
T* getData(Entity entity)
2024-03-31 10:11:22 +00:00
{
2024-06-04 18:01:49 +00:00
assert(entity < m_component_array.size());
return &m_component_array[entity];
2024-03-31 10:11:22 +00:00
}
2023-04-29 14:22:25 +00:00
};
2023-01-02 17:24:20 +00:00
2023-04-29 14:22:25 +00:00
class System {
2024-06-04 18:01:49 +00:00
public:
Scene* const m_scene;
std::bitset<MAX_COMPONENTS> m_signature;
std::set<Entity> m_entities{}; // entities that contain the needed components
public:
2024-03-31 10:11:22 +00:00
System(Scene* scene, std::set<size_t> required_component_hashes);
System(const System&) = delete;
2023-01-02 17:24:20 +00:00
2024-07-29 17:58:33 +00:00
virtual ~System() {}
2023-01-02 17:24:20 +00:00
2024-06-04 18:01:49 +00:00
System& operator=(const System&) = delete;
2023-04-29 14:56:49 +00:00
2024-06-04 18:01:49 +00:00
virtual void onUpdate(float ts) = 0;
virtual void onComponentInsert(Entity) {}
virtual void onComponentRemove(Entity) {}
2023-04-29 14:22:25 +00:00
};
2023-01-02 17:24:20 +00:00
2024-03-31 10:11:22 +00:00
} // namespace engine