-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponent.h
More file actions
301 lines (267 loc) · 8.28 KB
/
Component.h
File metadata and controls
301 lines (267 loc) · 8.28 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#pragma once
#include "Serialize.h"
namespace ECS
{
template<typename Registry, typename Base>
class Dependency
{
private:
Registry& registry;
public:
Dependency(Registry& registry)
: registry(registry)
{
}
private:
template<typename T>
class Creation
{
public:
void on(Registry& registry, typename Registry::entity_type entity)
{
registry.get_or_assign<T>(entity);
}
};
template<typename T>
class Deletion
{
public:
void on(Registry& registry, typename Registry::entity_type entity)
{
registry.reset<T>(entity);
}
};
template<typename Dependency>
void DependsOns()
{
Creation<Dependency> cre;
registry.construction<Base>().connect<Creation<Dependency>, & Creation<Dependency>::on>(&cre);
Deletion<Base> del;
registry.destruction<Dependency>().connect<Deletion<Base>, & Deletion<Base>::on>(&del);
}
public:
template<typename... Dependency>
void DependsOn()
{
using accumulator_type = int[];
accumulator_type accumulator = { 0, (DependsOns<Dependency>(), 0)... };
(void)accumulator;
}
};
template<typename Registry>
class ReferenceResolver
{
public:
using map_type = std::unordered_map<typename Registry::entity_type, typename Registry::entity_type>;
private:
Registry& registry;
const map_type& map;
public:
ReferenceResolver(Registry& registry, const map_type& map)
: registry(registry)
, map(map)
{
}
public:
typename Registry::entity_type& operator()(typename Registry::entity_type& ref)
{
if (map.find(ref) != map.end())
ref = map.at(ref);
return ref;
}
};
template<typename Registry>
class ComponentDependency
{
private:
template<typename Component, typename = decltype(&Component::Dependency<Component>)>
static void DependsOn0(int, Registry & reg)
{
Dependency<Registry, Component> dep(reg);
Component::Dependency(dep);
}
template<typename Component>
static void DependsOn0(bool, Registry& reg)
{
}
public:
template<typename Component>
static void DependsOn(Registry& reg)
{
DependsOn0<Component>(0, reg);
}
};
template<typename Registry>
class ComponentReference
{
private:
template<typename Component, typename = decltype(&Component::Reference<Component>)>
static void Resolve0(int, Registry & reg, const std::vector<typename Registry::entity_type> & srcs, const std::vector<typename Registry::entity_type> & dsts)
{
ReferenceResolver<Registry>::map_type map;
for (auto srcitr = srcs.begin(), dstitr = dsts.begin(); srcitr != srcs.end() && dstitr != dsts.end(); ++srcitr, ++dstitr)
map.insert(std::make_pair(*srcitr, *dstitr));
ReferenceResolver<Registry> ref(reg, map);
for (auto& dst : dsts)
if (reg.has<Component>(dst))
reg.get<Component>(dst).Reference(ref);
}
template<typename Component>
static void Resolve0(bool, Registry& reg, const std::vector<typename Registry::entity_type>& srcs, const std::vector<typename Registry::entity_type>& dsts)
{
}
public:
template<typename Component>
static void Resolve(Registry& reg, const std::vector<typename Registry::entity_type>& srcs, const std::vector<typename Registry::entity_type>& dsts)
{
Resolve0<Component>(0, reg, srcs, dsts);
}
};
template<typename Registry>
class ComponentGui
{
private:
template<typename Component, typename = decltype(&Component::EditorGui)>
static void EditorWidget0(int, Registry & reg, MM::ImGuiEntityEditor<Registry> & editor)
{
editor.registerComponentWidgetFn(
reg.type<Component>(),
[](auto& registry, auto entity) {
registry.get<Component>(entity).EditorGui();
});
}
template<typename Component>
static void EditorWidget0(bool, Registry& reg, MM::ImGuiEntityEditor<Registry>& editor)
{
}
public:
template<typename Component>
static void EditorWidget(Registry& reg, MM::ImGuiEntityEditor<Registry>& editor)
{
EditorWidget0<Component>(0, reg, editor);
}
};
// Declaration of a template
template<typename Registry, typename Components, typename Tags, typename Events>
class ComponentManager;
template<typename Registry, typename... Components, typename... Tags, typename... Events>
class ComponentManager<Registry, std::tuple<Components...>, std::tuple<Tags...>, std::tuple<Events...>>
{
private:
template<typename Event>
static void InitializeEvent()
{
using accumulator_type = int[];
accumulator_type accumulator = { 0, (Event::Register<Components>(), 0)... };
(void)accumulator;
}
template<typename Registry, typename Component>
static void InitializeEditorComponent(Registry& reg, MM::ImGuiEntityEditor<Registry>& editor)
{
editor.registerTrivial<Component>(reg, ECS::IdentifierResolver::name<Component>());
ComponentGui<Registry>::EditorWidget<Component>(reg, editor);
}
template<typename Registry, typename Component>
static void InitializeDependency(Registry& reg)
{
ComponentDependency<Registry>::DependsOn<Component>(reg);
}
template<typename Registry, typename Component>
static void UpdateReference(Registry& reg, const std::vector<typename Registry::entity_type>& srcs, const std::vector<typename Registry::entity_type>& dsts)
{
ComponentReference<Registry>::Resolve<Component>(reg, srcs, dsts);
}
public:
static void InitializeEvents()
{
using accumulator_type = int[];
accumulator_type accumulator = { 0, (InitializeEvent<Events>(), 0)... };
(void)accumulator;
}
template<typename Registry>
static void InitializeEditorComponents(Registry& reg, MM::ImGuiEntityEditor<Registry>& editor)
{
using accumulator_type = int[];
accumulator_type accumulator = { 0, (InitializeEditorComponent<Registry, Components>(reg, editor), 0)... };
(void)accumulator;
}
template<typename Registry>
static void InitializeDependency(Registry& reg)
{
using accumulator_type = int[];
accumulator_type accumulator = { 0, (InitializeDependency<Registry, Components>(reg), 0)... };
(void)accumulator;
}
template<typename Registry>
static void InitializeLifecycleEvents(Registry& reg)
{
ECS::LifecycleEvents<Registry>::Lifecycle<Components...>(reg);
}
template<typename Registry>
static void UpdateReferences(Registry& reg, const std::vector<typename Registry::entity_type>& srcs, const std::vector<typename Registry::entity_type>& dsts)
{
using accumulator_type = int[];
accumulator_type accumulator = { 0, (UpdateReference<Registry, Components>(reg, srcs, dsts), 0)... };
(void)accumulator;
}
template<typename Registry>
static void CloneComponents(Registry& reg, const std::vector<typename Registry::entity_type>& srcs, std::vector<typename Registry::entity_type>& dsts)
{
ComponentClone<Registry>::Clone<Components...>(reg, srcs, dsts);
}
template<typename Registry, typename RegistryInitializer>
static bool LoadScene(const std::string& location, Registry& scene, RegistryInitializer initFunc)
{
std::ifstream storage(location);
auto snap = scene.restore();
initFunc(scene);
return ObjectSerializer<Registry, std::tuple<Components...>, std::tuple<Tags...>>::Import(storage, snap);
}
template<typename Registry>
static bool SaveScene(const std::string& location, const Registry& scene)
{
std::ofstream storage(location);
return ObjectSerializer<Registry, std::tuple<Components...>, std::tuple<Tags...>>::Export(storage, scene.snapshot());
}
template<typename Registry>
static bool LoadEntity(const std::string& location, Registry& scene, std::vector<typename Registry::entity_type>& srcs, std::vector<typename Registry::entity_type>& dsts)
{
std::ifstream storage(location);
if (storage)
{
try
{
cereal::JSONInputArchive archive(storage);
EntityImporter<Registry> serializer(archive);
serializer.components<Components...>(scene, srcs, dsts);
return true;
}
catch (cereal::Exception e)
{
// —áŠO
}
}
return false;
}
template<typename Registry>
static bool SaveEntity(const std::string& location, const Registry& scene, const std::vector<typename Registry::entity_type>& srcs)
{
std::ofstream storage(location);
if (storage)
{
try
{
cereal::JSONOutputArchive archive(storage);
EntityExporter<Registry> serializer(archive);
serializer.components<Components...>(scene, srcs);
return true;
}
catch (cereal::Exception)
{
// —áŠO
}
}
return false;
}
};
}