-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
103 lines (92 loc) · 2.8 KB
/
GameObject.cpp
File metadata and controls
103 lines (92 loc) · 2.8 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef LINEARCHEXT_GAMEOBJECT_CPP
#define LINEARCHEXT_GAMEOBJECT_CPP
#include<list>
#include "GameObject.hpp"
#include "Transform.hpp"
#include <typeinfo>
#include <stdexcept>
#include <string>
#include "Component.hpp"
#include "Collider.hpp"
#include "Rigidbody.hpp"
#include "Renderer.hpp"
namespace LinearchExt{
std::list<GameObject> gameObjects0;
std::list<GameObject> GameObject::gameObjects = gameObjects0;
void GameObject::SetActive(bool active){
isActive = active;
}
Collider* GameObject::GetCollider(){
return collider;
}
Rigidbody* GameObject::GetRigidbody(){
return rigidbody;
}
Transform* GameObject::GetTransform(){
return transform;
}
Renderer* GameObject::GetRenderer(){
return renderer;
}
GameObject* GameObject::GetGameObject(){
return this;
}
GameObject::GameObject(){
GameObject::gameObjects.push_back(*this);
Transform(this);
}
GameObject::~GameObject(){
GameObject::gameObjects.remove(*this);
}
bool GameObject::IsActiveInHierarchy(){
return isActive;
}
bool GameObject::operator==(const GameObject& b){
return this==&b;
}
bool GameObject::operator!=(const GameObject& b){
return this!=&b;
}
bool operator==(const GameObject& a, const GameObject& b){
return &a == &b;
}
bool operator!=(const GameObject& a, const GameObject& b){
return &a != &b;
}
template <typename T> T* GameObject::GetComponent(){
std::type_info id = typeid(T);
for (std::list<Component>::iterator it = components.begin(); it != components.end(); ++it){
if(typeid(*it) == id){
return &(*it);
}
}
return 0;
}
template <typename T> T* GameObject::AddComponent(){
if (!Component::IsComponent<T>()){
throw std::runtime_error((std::string)typeid(T).name() + (std::string)" is not a LinearchExt component.");
return 0;
}
std::type_info id = typeid(T);
for (std::list<Component>::iterator it = components.begin(); it != components.end(); ++it){
if(typeid(*it) == id){
throw std::runtime_error("GameObject " + name + " already has component of type " + (std::string)typeid(T).name() + ".");
return &(*it);
}
}
T t(this);
components.push_back(t);
return &t;
}
template <typename T> bool GameObject::RemoveComponent(){
std::type_info id = typeid(T);
for (std::list<Component>::iterator it = components.begin(); it != components.end(); ++it){
if(typeid(*it) == id){
components.remove(*it);
return 1;
}
}
return 0;
}
}
#endif