-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameObject.cpp
78 lines (61 loc) · 1.46 KB
/
GameObject.cpp
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
#include "GameObject.hpp"
GameObject::GameObject()
{
transform = new TransformComponent(this);
components.push_back(transform);
}
GameObject::~GameObject()
{
for (int i = 0; i < components.size(); i++) {
delete components.at(i);
}
components.clear();
}
void GameObject::update(GameState & gameState)
{
}
void GameObject::draw(GameState & gameState, Renderer & renderer)
{
}
TransformComponent * GameObject::getTransform()
{
return transform;
}
template<typename T>
bool GameObject::addComponent()
{
try {
static_assert(std::is_base_of<Component, T>::value, "type parameter of this class must derive from Component");
components.push_back(new T(this));
return true;
}
catch (...) {
std::cerr << "Failed to add component! Type " << typeid(T).name() << "is not a valid type!\n";
return false;
}
}
template<typename T>
T* GameObject::getComponent()
{
for (int i = 0; i < components.size(); i++) {
//Compare types
if (typeid(*components.at(i)).name() == typeid(T).name()) {
return components.at(i);
}
}
return NULL;
}
template<typename T>
bool GameObject::removeComponent()
{
for (int i = 0; i < components.size(); i++) {
//Compare types
if (typeid(*components.at(i)).name() == typeid(T).name()) {
Component* removedComponent = components.at(i);
components.erase(components.begin() + i);
delete removedComponent;
return true;
}
}
return false;
}