-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.h
112 lines (84 loc) · 2.61 KB
/
World.h
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
#pragma once
#include "Component/IComponentBucket.h"
#include <memory>
#include <unordered_set>
#include <cassert>
#include <vector>
#include "System/System.h"
class World {
public:
Entity CreateEntity();
void ScheduleEntityDeletion(Entity ent);
template<typename T>
T& GetComponent(Entity ent) {
return GetCastedBucket<T>()->Get(ent);
}
template<typename T>
void SetComponent(Entity ent, T&& comp) {
assert(entities_.find(ent) != entities_.end());
auto comp_id = TypeId<T>::Get();
AddBucket<T>();
auto bucket = GetCastedBucket<T>();
bucket->Set(ent, std::forward<T>(comp));
ents_to_comps_[ent].insert(comp_id);
}
template<typename T, typename... CompsT>
bool HasComponents(Entity ent) {
if constexpr (sizeof...(CompsT) == 0)
return HasComponent<T>(ent);
else
return HasComponent<T>(ent) && HasComponents<CompsT...>(ent);
}
template<typename... CompsT>
std::vector<Entity> Filter() {
if constexpr (sizeof...(CompsT) == 0)
return {entities_.begin(), entities_.end()};
std::vector<Entity> res;
for (auto ent : entities_) {
if (HasComponents<CompsT...>(ent))
res.emplace_back(ent);
}
return res;
}
void Update(float dt = 0);
bool IsAlive(Entity ent);
template<typename T, typename... ArgsT>
void AddSystem(ArgsT&&... args) {
systems_.emplace_back(std::make_unique<T>(std::forward<ArgsT>(args)...));
}
private:
template<typename T>
bool HasComponent(Entity ent) {
return BucketExists<T>() && GetCastedBucket<T>()->HasEntity(ent);
}
template<typename T>
void AddBucket() {
auto id = TypeId<T>::Get();
if (!BucketExists<T>()) {
comp_buckets_[id] = std::make_shared<ComponentBucket<T>>();
}
}
template<typename T>
bool BucketExists() const {
auto id = TypeId<T>::Get();
return comp_buckets_.find(id) != comp_buckets_.end();
}
template<typename T>
auto GetCastedBucket() {
return std::static_pointer_cast<ComponentBucket<T>>(comp_buckets_[TypeId<T>::Get()]);
}
void DeleteComponent(Entity ent, CompIdT comp);
template<typename T>
void DeleteComponent(Entity ent) {
DeleteComponent(ent, TypeId<T>::Get());
}
void DeleteEntity(Entity ent);
void DeleteScheduled();
private:
std::unordered_set<Entity> entities_;
std::unordered_map<Entity, std::unordered_set<CompIdT>> ents_to_comps_;
Entity entity_counter_ = 0;
std::unordered_map<CompIdT, std::shared_ptr<IComponentBucket>> comp_buckets_;
std::unordered_set<Entity> to_die_list_;
std::vector<std::unique_ptr<ISystem>> systems_;
};