-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.h
More file actions
79 lines (59 loc) · 1.95 KB
/
Entity.h
File metadata and controls
79 lines (59 loc) · 1.95 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
#ifndef ENTITY_H
#define ENTITY_H
#include "utility/Bitset.h"
#include "utility/Utility.hpp"
#include "Component.h"
class Entity
{
public:
Entity() : mIsAlive(true), mComponents{} { for (size_t i = 0; i < MAX_COMPONENTS; i++) mComponents[i] = nullptr; }
~Entity() { for (Component *component : mComponents) delete component; }
template <typename T, typename U = T, typename... Args>
T &AddComponent(Args&&... args);
template <typename T, typename U = T>
T &AddComponent(T *component);
template <typename T>
bool HasComponent() const;
template <typename T>
T *GetComponent() const;
bool IsAlive() const { return mIsAlive; }
void Destroy() { mIsAlive = false; }
private:
static const unsigned int MAX_COMPONENTS = 32;
Component *mComponents[MAX_COMPONENTS]; // Entity owns components TODO: multiple components
Bitset<MAX_COMPONENTS> mComponentMask;
bool mIsAlive;
};
template <typename T, typename U, typename... Args>
T &Entity::AddComponent(Args&&... args)
{
static_assert(std::is_base_of<U,T>::value, "T is not a component derived from U");
T *component = new T(utility::template forward<Args>(args)...);
component->SetOwner(this);
mComponents[GetComponentID<U>()] = component; // TODO: multiple components
if (!mComponentMask.Test(GetComponentID<U>()))
mComponentMask.Set(GetComponentID<U>());
component->Init();
return *component;
}
template <typename T, typename U>
T &Entity::AddComponent(T *component)
{
static_assert(std::is_base_of<U,T>::value, "T is not a component derived from U");
mComponents[GetComponentID<U>()] = component; // TODO: multiple components
if (!mComponentMask.Test(GetComponentID<U>()))
mComponentMask.Set(GetComponentID<U>());
component->Init();
return *component;
}
template <typename T>
bool Entity::HasComponent() const
{
return mComponentMask[GetComponentID<T>()];
}
template <typename T>
T *Entity::GetComponent() const
{
return static_cast<T*>(mComponents[GetComponentID<T>()]);
}
#endif // ENTITY_H