-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorld.h
92 lines (71 loc) · 2.04 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
#pragma once
#include <functional>
#include "Types.h"
#include "Archetype.h"
#include "Component.h"
#include "ErrorHandling.h"
#include "System.h"
class Entity;
class SetQueue
{
public:
SetQueue(IStorage* Storage);
~SetQueue();
SetQueue(const SetQueue& obj) = delete;
void Enqueue(EntityID Entity, const void* Data) const;
void ForEach(std::function<void(EntityID&, void*)> Handler);
void Empty()
{
EntityIDs->Empty();
ComponentBuffer->Empty();
}
private:
VectorStorage<EntityID>* EntityIDs;
IStorage* ComponentBuffer;
};
class World
{
public:
World();
~World();
World(const World& obj) = delete;
Entity NewEntity();
template<typename T>
void Set(const EntityID& Entity, const T Data)
{
ComponentID Type = GetComponent<T>();
Set(Entity, Type, &Data);
}
void Set(EntityID Entity, ComponentID Type, const void* Data);
template<typename T>
T* Get(const EntityID& Entity)
{
ComponentID Type = GetComponent<T>();
void* R = Get(Entity, Type);
return static_cast<T*>(R);
}
void* Get(const EntityID& Entity, ComponentID Type);
template<typename T>
void Remove(const EntityID& Entity)
{
Remove(Entity, GetComponent<T>());
}
void Remove(const EntityID& Entity, ComponentID Type);
void Delete(const EntityID& Entity);
private:
Archetype* FindOrAddArchetype(const ArchSignature* Signature);
Archetype* ChangeEntityType(const EntityID& Entity, ComponentID Type, const void* Data);
public:
void Tick();
void AddSystem(System System);
private:
bool WorldLock = false;
EntityID NextEntityID = 1;
std::vector<Archetype*> Archetypes;
std::unordered_map<ArchSignature, size_t> ArchetypeLookup;
std::unordered_map<EntityID, size_t> EntityArchetypeLookup;
std::unordered_map<ComponentID, SetQueue*> SetQueues;
std::unordered_map<ComponentID, IStorage*> RemoveQueues;
VectorStorage<EntityID>* Graveyard;
std::vector<System> Systems;
};