-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentPool.h
More file actions
61 lines (49 loc) · 1.17 KB
/
ComponentPool.h
File metadata and controls
61 lines (49 loc) · 1.17 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
#pragma once
#include"ECSManager.h"
// コンポーネントを管理するコンテナクラス
template<typename CompType>
class ComponentPool
{
private:
friend class ECSManager;
// コンポーネントのインスタンスを管理するコンテナ
std::vector<CompType> components;
// このコンテナクラスが管理するコンポーネントが持つ一意なID
static size_t compTypeID;
public:
// コンテナのメモリを確保
ComponentPool(const size_t a_size)
{
components.resize(a_size);
}
// CompTypeIDを取得する関数
static inline const size_t GetID()
{
// この関数を初めて読んだ時にIDを発行
if (!ComponentPool<CompType>::compTypeID)
{
ComponentPool<CompType>::compTypeID = ++ECSManager::nextCcompTypeID;
}
return ComponentPool<CompType>::compTypeID;
}
// コンポーネントを追加
inline CompType* AddComponent(const size_t a_entity)
{
if (components.size() < a_entity)
{
components.resize(a_entity, CompType());
}
components[a_entity] = CompType();
return &components[a_entity];
}
// コンポーネントを取得する
inline CompType* GetComponent(const size_t a_entity)noexcept
{
// エンティティが有効なら
if (components.size() >= a_entity)
{
return &components[a_entity];
}
return nullptr;
}
};