VOX
A little voxel engine
Loading...
Searching...
No Matches
ECS_old.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "define.hpp"
4
5#include <cstdint>
6#include <queue>
7#include <unordered_map>
8#include <typeindex>
9#include <vector>
10#include <functional>
11#include <memory>
12#include <set>
13
14// This is a tentative implementation of an Entity Component System
15// The goal is to have a system that is easy to use, understand and extend
16
17class ECS
18{
19
20public:
21
22 typedef uint64_t Entity;
23
24
26 {
27 }
28
30 {
31 }
32
33 ECS(ECS & other) = delete;
34 ECS(ECS && other) = delete;
35 ECS & operator=(ECS & other) = delete;
36 ECS & operator=(ECS && other) = delete;
37
38
40 {
41 static Entity id = 0;
42 m_entities[id];
43 return id++;
44 }
45
46 void removeEntity(Entity entity)
47 {
48 m_entities.erase(entity);
49 for (auto & component : m_components)
50 {
51 component.second.erase(entity);
52 }
53 }
54
55
56 template <typename T, typename... Args>
57 void addComponent(Entity entity, Args &&... args)
58 {
59 const std::type_index type = std::type_index(typeid(T));
60 m_entities[entity].insert(type);
61 m_components[type][entity] = std::make_shared<T>(T{std::forward<Args>(args)...});
62 }
63
64 template <typename T>
65 T * getComponent(Entity entity)
66 {
67 const std::type_index type = std::type_index(typeid(T));
68 auto it = m_components[type].find(entity);
69 if (it == m_components[type].end())
70 {
71 throw std::runtime_error("Entity does not have component");
72 }
73 return static_cast<T *>(it->second.get());
74 }
75
76 template <typename T>
78 {
79 const std::type_index type = std::type_index(typeid(T));
80 m_entities[entity].erase(type);
81 m_components[type].erase(entity);
82 }
83
90 template <typename... Components>
91 void forEach(void (*func)(Components &...))
92 {
93 std::vector<std::type_index> types = {std::type_index(typeid(Components))...};
94 for (auto & [entity, entity_component_types] : m_entities)
95 {
96 if (
97 std::all_of(types.begin(), types.end(),
98 [&entity_component_types](std::type_index type)
99 {
100 return entity_component_types.contains(type);
101 }
102 )
103 )
104 {
105 func(*getComponent<Components>(entity)...);
106 }
107 }
108 }
109
110
111
112private:
113
114 std::unordered_map<Entity, std::set<std::type_index>> m_entities;
115
116 std::unordered_map<std::type_index, std::unordered_map<Entity, std::shared_ptr<void>>> m_components;
117
118};
Definition: ECS_old.hpp:18
uint64_t Entity
Definition: ECS_old.hpp:22
ECS(ECS &&other)=delete
T * getComponent(Entity entity)
Definition: ECS_old.hpp:65
void removeEntity(Entity entity)
Definition: ECS_old.hpp:46
Entity createEntity()
Definition: ECS_old.hpp:39
ECS & operator=(ECS &&other)=delete
ECS & operator=(ECS &other)=delete
void addComponent(Entity entity, Args &&... args)
Definition: ECS_old.hpp:57
ECS(ECS &other)=delete
void forEach(void(*func)(Components &...))
This function will call the function func for each entity that has all the components specified.
Definition: ECS_old.hpp:91
~ECS()
Definition: ECS_old.hpp:29
ECS()
Definition: ECS_old.hpp:25
void removeComponent(Entity entity)
Definition: ECS_old.hpp:77