-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECSManager.h
More file actions
344 lines (288 loc) · 8.34 KB
/
ECSManager.h
File metadata and controls
344 lines (288 loc) · 8.34 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#pragma once
#include <memory>
#include <unordered_map>
#include <typeindex>
#include <array>
#include <vector>
#include <bitset>
#include <functional>
using Archetype = std::bitset<128>;
class ECSManager
{
public:
// エンティティを新規作成
inline const size_t GenerateEntity()
{
size_t nEntity;
// リサイクル待ちエンティティがあればそこから1つ取り出す
if (recycleEntities.size())
{
nEntity = recycleEntities.back();
recycleEntities.pop_back();
}
// 無ければ新規発行
else
{
nEntity = nextID;
nextID++;
}
// エンティティを有効にする
if (entityToActive.size() <= nEntity)
{
entityToActive.resize(nEntity + 1, false);
}
entityToActive[nEntity] = true;
// 生成したエンティティを返す
return nEntity;
}
// コンポーネントを追加する
template<typename CompType>
CompType* AddComponent(const size_t a_entity)
{
// コンポーネントの型のIDを取得
size_t type = ComponentPool<CompType>::GetID();
// 初めて生成するコンポーネントなら
if (!typeToComponents[type])
{
// ComponentPoolを実体化する
typeToComponents[type] = std::make_shared<ComponentPool<CompType>>(4096);
}
// 追加するコンポーネントを格納するコンテナクラスを取得
std::shared_ptr<ComponentPool<CompType>> spCompPool = std::static_pointer_cast<ComponentPool<CompType>>(typeToComponents[type]);
// コンポーネントを追加し取得
CompType* pResultComp = spCompPool->AddComponent(a_entity);
// コンポーネントを追加する前のアーキタイプを取得
Archetype arch;
if (entityToArchetype.size() > a_entity)
{
arch = entityToArchetype[a_entity];
// 前アーキタイプのエンティティリストからエンティティを削除
archToEntities[arch].Remove(a_entity);
}
else
{
entityToArchetype.resize(a_entity + 1, Archetype());
//今回がコンポーネントを初めて追加するエンティティなら
//上記の削除処理はしない
}
// アーキタイプを編集
arch.set(type);
if(archToEntities.find(arch) == archToEntities.end())
{
// 新しいアーキタイプならエンティティリストを初期化
archToEntities[arch] = EntityContainer();
}
// 新しいアーキタイプをセット
archToEntities[arch].Add(a_entity);
entityToArchetype[a_entity] = arch;
// 追加したコンポーネントを返す
return pResultComp;
}
//コンポーネントを取得する
template<typename CompType>
CompType* GetComponent(const size_t entity)
{
// コンポーネントの型のIDを取得
size_t type = ComponentPool<CompType>::GetID();
// 追加するコンポーネントを格納するコンテナクラスを取得
std::shared_ptr<ComponentPool<CompType>> spCompPool = std::static_pointer_cast<ComponentPool<CompType>>(typeToComponents[type]);
return spCompPool->GetComponent(entity);
}
// コンポーネントを削除する
template<typename CompType>
void RemvoeComponent(const size_t a_entity)
{
// コンポーネントの型のIDを取得
size_t type = ComponentPool<CompType>::GetID();
Archetype arch;
// コンポーネントを削除する前のアーキタイプを取得
if (entityToArchetype.size() > a_entity)
{
arch = entityToArchetype[a_entity];
}
else
{
return;
}
// エンティティが削除するコンポーネントを持ってなかったらreturn
if (!arch.test(type))
{
return;
}
// 前アーキタイプのエンティティリストからエンティティを削除
archToEntities[arch].Remove(a_entity);
// アーキタイプを編集
arch.reset(type);
// 新しいアーキタイプをセット
archToEntities[arch].Add(a_entity);
entityToArchetype[a_entity] = arch;
}
// エンティティを無効にする
inline void RemoveEntity(const size_t a_entity)
{
entityToActive[a_entity] = false;
archToEntities[entityToArchetype[a_entity]].Remove(a_entity);
recycleEntities.emplace_back(a_entity);
}
// 処理を実行する
template<typename ...CompType>
void RunFunction(std::function<void(CompType&...)> a_func)
{
// 処理に必要なコンポーネントのアーキタイプを取得
Archetype arch;
(arch.set(ComponentPool<CompType>::GetID()), ...);
// 処理に必要なアーキタイプを含むアーキタイプを持つエンティティのリストを検索
for (auto&& entities : archToEntities)
{
if ((entities.first & arch) == arch)
{
for (auto&& entity : entities.second.GetEntities())
{
a_func(std::static_pointer_cast<ComponentPool<CompType>>
(typeToComponents[ComponentPool<CompType>::GetID()])->components[entity]...);
}
}
}
}
// エンティティを管理するためのコンテナクラス
class EntityContainer
{
private:
std::vector<size_t> entities;
std::vector<size_t> entityToIndex;
public:
// エンティティを追加
inline void Add(const size_t a_entity)
{
entities.emplace_back(a_entity);
if (entityToIndex.size() <= a_entity)
{
entityToIndex.resize(a_entity + 1);
}
entityToIndex[a_entity] = entities.size() - 1;
}
// エンティティを削除
inline void Remove(const size_t a_entity)
{
if (entityToIndex.size() < a_entity)
{
return;
}
size_t backIndex = entities.size() - 1;
size_t backEntity = entities.back();
size_t removeIndex = entityToIndex[a_entity];
// 削除する要素が最後の要素でなければ
if (a_entity != entities.back())
{
entities[removeIndex] = backEntity;
entityToIndex[backIndex] = removeIndex;
}
// 最後尾のEntityを削除
entities.pop_back();
}
inline const std::vector<size_t>& GetEntities()const noexcept
{
return entities;
}
};
// ComponentPoolを同じコンテナで扱うための基底クラス
class IComponentPool
{
public:
private:
};
// コンポーネントを管理するコンテナクラス
template<typename CompType>
class ComponentPool :public IComponentPool
{
private:
friend class ECSManager;
// コンポーネントのインスタンスを管理するコンテナ
std::vector<CompType> components;
// このコンテナクラスが管理するコンポーネントが持つ一意なID
static size_t compTypeID;
public:
// コンテナのメモリを確保
ComponentPool(const size_t a_size)
{
components.reserve(a_size);
}
// コンポーネントを追加
inline CompType* AddComponent(const size_t a_entity)
{
if (components.size() <= a_entity)
{
components.resize(a_entity + 1, CompType(0));
}
components[a_entity] = CompType(a_entity);
return &components[a_entity];
}
// コンポーネントを取得する
inline CompType* GetComponent(const size_t a_entity)noexcept
{
// エンティティが有効なら
if (components.size() >= a_entity)
{
return &components[a_entity];
}
return nullptr;
}
// CompTypeIDを取得する関数
static inline const size_t GetID()
{
// この関数を初めて読んだ時にIDを発行
if (!ComponentPool<CompType>::compTypeID)
{
ComponentPool<CompType>::compTypeID = ++ECSManager::nextCcompTypeID;
}
return ComponentPool<CompType>::compTypeID;
}
};
// 特定のコンポーネントの組み合わせを持つエンティティIDのリストを取得する
template<typename ...CompType>
std::vector<size_t> GetEntitiesWithComponents() const
{
std::vector<size_t> resultEntities;
// 検索したいコンポーネントのアーキタイプを作成
Archetype searchArch;
(searchArch.set(ComponentPool<CompType>::GetID()), ...); // パック展開でビットをセット
// 全ての既存のアーキタイプを反復処理
for (const auto& pair : archToEntities) // pair.first は Archetype, pair.second は EntityContainer
{
// 現在のアーキタイプが検索したいアーキタイプを「含む」かチェック
// つまり、要求されたコンポーネントをすべて持っているか
if ((pair.first & searchArch) == searchArch)
{
// 条件を満たすアーキタイプに属するエンティティIDのリストを取得
// これらのエンティティはすべて有効であると仮定するか、ここで有効性もチェック
for (size_t entityId : pair.second.GetEntities())
{
// エンティティが実際に有効なエンティティであるか最終チェック
if (entityId < entityToActive.size() && entityToActive[entityId]) {
resultEntities.push_back(entityId);
}
}
}
}
return resultEntities;
}
private:
// コンポーネントをタイプ別で管理するコンテナ
std::unordered_map<size_t, std::shared_ptr<IComponentPool>> typeToComponents;
// エンティティとアーキタイプを紐づけるコンテナ
std::vector<Archetype> entityToArchetype;
// エンティティと有効フラグを紐づけるコンテナ
std::vector<bool> entityToActive;
// エンティティをアーキタイプごとに分割したコンテナ
std::unordered_map<Archetype, EntityContainer> archToEntities;
// 次に生成するエンティティのID
size_t nextID = 0;
// 再利用待ちのEntityのリスト
std::vector<size_t> recycleEntities;
// 次に発行するCompTypeID
static size_t nextCcompTypeID;
};
// スタティック変数の初期化
inline size_t ECSManager::nextCcompTypeID = 0;
template<typename CompType>
inline size_t ECSManager::ComponentPool<CompType>::compTypeID = 0;