-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECS.cpp
More file actions
213 lines (150 loc) · 6.4 KB
/
ECS.cpp
File metadata and controls
213 lines (150 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) October 2025 Félix-Olivier Dumas. All rights reserved.
// Licensed under the terms described in the LICENSE file
// Special thanks to the internet and forums for all the wisdom and inspiration.
#include <iostream>
#include <unordered_map>
#include <string>
#include <sstream>
#include <map>
#include <type_traits>
#include <cassert>
#include <iomanip>
#define VAR_NAME(x) #x
#define CLASS_NAME(x) typeid(x).name()
template<typename T> struct ID {
T value;
explicit ID(T v = 0) : value(v) {}
};
template <typename T> void printValue(const T& t) {
//std::cout << t << std::endl;
std::cout << " Address: " << static_cast<const void*>(&t) << std::endl;
}
using EntityID = uint32_t;
class Entity {
private:
static EntityID nuid;
EntityID uid;
public:
Entity() : uid(nuid++) { }
EntityID getId() const { return uid; }
bool operator==(const Entity& other) const { return uid == other.uid; }
bool operator!=(const Entity& other) const { return uid != other.uid; }
bool operator<(const Entity& other) const { return uid < other.uid; }
}; EntityID Entity::nuid = 0;
class Component {
private:
public:
virtual ~Component() = default;
void doStuff() { std::cout << "Mock Component check\n"; }
};
class Velocity : public Component { public: float vx, vy = 0; };
class Position : public Component { public: float x, y = 0; };
class Weight : public Component { public: float weight = 0; };
class Name : public Component { public: std::string name = "default"; };
template<typename E = Entity, typename C = Component> class Registry {
private:
std::map<E, std::vector<std::unique_ptr<C>>> registry;
public:
Registry() {}
template <typename CC, typename E> void addComponent(const E& e) {
static_assert(std::is_base_of_v<C, CC>, "Must derive from Component");
static_assert(std::is_default_constructible_v<CC>, "Must be default constructible");
auto& vec = registry[e];
for (auto& comp : registry[e])
if (typeid(*comp) == typeid(CC))
throw std::runtime_error("Component already exists!");
vec.push_back(std::make_unique<CC>());
}
template <typename CC, typename E> CC* getComponent(const E& e) {
for (auto& comp : registry[e])
if (auto derived = dynamic_cast<CC*>(comp.get()))
return derived;
return nullptr;
}
template<typename CC = Component> size_t countComponents(const E& e) {
size_t count = 0;
for (auto& comp : registry[e])
if (auto derived = dynamic_cast<CC*>(comp.get())) ++count;
return count;
}
template <typename E> bool hasComponents(const E& e) const { return !registry[e].empty(); }
template <typename CC = Component> std::vector<std::string> getComponentNames(const E& e) {
std::vector<std::string> cnvec;
for (auto& comp : registry[e])
if (auto derived = dynamic_cast<CC*>(comp.get()))
cnvec.push_back(typeid(*derived).name());
for (auto& name : cnvec) {
const std::string prefix = "class ";
if (name.rfind(prefix, 0) == 0)
name = name.substr(prefix.size());
} return cnvec;
}
};
class System {
private:
public:
virtual void update(Registry<>& registry, float dt) = 0;
};
class ConsolePrintingSystem : public System {
private:
public:
void update(Registry<>& registry, float dt) override {
}
};
template <typename E> void printEntity(Registry<>& reg, const E& e) {
std::cout << std::left;
std::cout << "--------- Entity ---------\n";
std::cout << std::setw(15) << "Name" << ": " << VAR_NAME(e) << "\n";
std::cout << std::setw(15) << "Type" << ": " << CLASS_NAME(e) << "\n";
std::cout << std::setw(15) << "ID" << ": " << e.getId() << "\n\n";
std::cout << "---- Core Attributes ----\n";
if (auto name = reg.getComponent<Name>(e))
std::cout << std::setw(15) << "Name" << ": " << name->name << "\n";
if (auto weight = reg.getComponent<Weight>(e))
std::cout << std::setw(15) << "Weight" << ": " << weight->weight << " Kg\n";
if (auto pos = reg.getComponent<Position>(e))
std::cout << std::setw(15) << "Position" << ": (" << pos->x << ", " << pos->y << ")\n";
if (auto vel = reg.getComponent<Velocity>(e))
std::cout << std::setw(15) << "Velocity" << ": (" << vel->vx << ", " << vel->vy << ")\n";
auto componentNames = reg.getComponentNames(e);
std::cout << "\n------- Components -------\n";
std::cout << std::setw(15) << "Count" << ": " << componentNames.size() << "\n";
std::cout << std::setw(15) << "Names" << ": ";
for (size_t i = 0; i < componentNames.size(); ++i) {
if (i > 0) std::cout << ", ";
std::cout << componentNames[i];
}
std::cout << "\n\n\n\n";
}
int main() {
auto registry = Registry<Entity, Component>();
auto cat = Entity();
auto dog = Entity();
auto hamster = Entity();
registry.addComponent<Name>(cat);
registry.addComponent<Weight>(cat);
registry.addComponent<Velocity>(cat);
registry.addComponent<Position>(cat);
registry.addComponent<Name>(dog);
registry.addComponent<Weight>(dog);
registry.addComponent<Name>(hamster);
auto name = registry.getComponent <Name> (cat);
auto weight = registry.getComponent <Weight> (cat);
auto velocity = registry.getComponent<Velocity>(cat);
auto position = registry.getComponent<Position>(cat);
auto componentCount = registry.countComponents(cat);
name->name = "Garfield";
weight->weight = 10.0f;
velocity->vx = 5.0f; velocity->vy = 15.0f;
position->x = 25; position->y = 75;
printEntity(registry, hamster);
if (registry.getComponent<Name>(dog)->name == registry.getComponent<Name>(cat)->name) {
std::cout << "Identiques" << std::endl;
} else std::cout << "Non-identiques" << std::endl;
//newRegistry.addComponent<Component>(cat);
//newRegistry.addComponent<Component>(cat);
//newRegistry.addComponent<Component>(cat);
//registry.addComponent<ComponentType pas instanciation>(cat); // possiblement faire ca, faut prendre le type de instancier dans la fonction addComponent();
// Aussi, faire en sorte qu'on ne puisse qu'ajouter un seul composant de type par entité, par exemple uniquement un seul MockComponent...
//auto comp = reg.getComponent<MockComponent>(cat); lui
}