-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArchetype.h
69 lines (55 loc) · 1.74 KB
/
Archetype.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
#pragma once
#include <unordered_map>
#include <unordered_set>
#include "Types.h"
#include "VectorStorage.h"
// custom specialization of std::hash can be injected in namespace std
namespace std
{
template<> struct hash<ArchSignature>
{
std::size_t operator()(ArchSignature const& s) const noexcept
{
std::size_t hash = 0;
for (auto && i : s) hash ^= std::hash<ComponentID>()(i);
return hash;
}
};
}
class Archetype
{
public:
explicit Archetype();
explicit Archetype(const ArchSignature& Base);
Archetype(const Archetype& obj) = delete;
~Archetype();
void CopyEntity(const EntityID& Entity, const Archetype* Source, ComponentID AddedType, const void* AddedValue);
void FastDelete(const EntityID& ID);
template<typename T>
void SetValue(const EntityID& ID, const T& Data);
void SetValue(const EntityID& ID, const ComponentID& CmpID, const void* Data);
template<typename T>
T* GetValue(const EntityID& ID);
void* GetValue(const EntityID& ID, const ComponentID& CmpID) const;
const ArchSignature* GetSignature() const;
VectorStorage<EntityID>* GetEntityIDs() const;
private:
int ID;
ArchSignature Signature;
std::unordered_map<EntityID, int> EntityToIndex;
std::unordered_map<ComponentID, int> CmpToStoreIndex;
VectorStorage<EntityID>* EntityIDStorage;
std::vector<IStorage*> CmpStorage;
};
template <typename T>
void Archetype::SetValue(const EntityID& ID, const T& Data)
{
auto CmpType = GetComponent<T>();
SetValue(ID, CmpType, &Data);
}
template <typename T>
T* Archetype::GetValue(const EntityID& ID)
{
auto CmpType = GetComponent<T>();
return static_cast<T*>(GetValue(ID, CmpType));
}