-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.h
More file actions
78 lines (64 loc) · 1.45 KB
/
Entity.h
File metadata and controls
78 lines (64 loc) · 1.45 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
#pragma once
#include <memory>
#include <string>
#include "Components.h"
#include <tuple>
typedef std::tuple<
CTransform,
CLifespan,
CInput,
CState,
CGravity,
CBoundingBox,
CAnimation,
CSprite
> ComponentTuple;
//container for components
//some unique data like id tag alive
class Entity
{
//Entity Manager now can call the private constructor
friend class EntityManager;
bool m_active = true;
size_t m_id = 0;
std::string m_tag = "Default";
ComponentTuple m_components;
Entity(const size_t id, const std::string& tag);
//private:
//old approach
//components
//std::shared_ptr<CTransform> cTransform;
//std::shared_ptr<CShape> cShape;
//std::shared_ptr<CCollision> cCollision;
//std::shared_ptr<CInput> cInput;
//std::shared_ptr<CScore> cScore;
//std::shared_ptr<CLifespan> cLifespan;
public:
bool isActive() const;
const std::string& tag() const;
const size_t id() const;
void destroy();
template <typename T>
bool hasComponent() const {
return getComponent<T>().has;
}
template<typename T>
T& getComponent() {
return std::get<T>(m_components);
}
template<typename T>
const T& getComponent() const {
return std::get<T>(m_components);
}
template <typename T>
void removeComponent() {
getComponent<T>() = T();
}
template<typename T, typename... TArgs>
T& addComponent(TArgs&&... mArgs) {
auto& component = getComponent<T>();
component = T(std::forward<TArgs>(mArgs)...);
component.has = true;
return component;
}
};