CSEngine
Loading...
Searching...
No Matches
SGameObject.h
1#pragma once
2
3#include <algorithm>
4#include <list>
5#include <utility>
6#include "../SObject.h"
7
8#include "../Util/Interface/TransformInterface.h"
9#include "../Component/SComponent.h"
10#include "sqrat/sqratUtil.h"
11#include "../Util/MoreString.h"
12#include "Base/ReflectionRef.h"
13
14namespace CSE {
15
16 class SComponent;
17
18 class SGameObject : public SObject {
19 public:
20 enum STATUS {
21 IDLE = 0, INIT = 1, DESTROY = -1, UNKOWN = -2
22 };
23 public:
25
26 explicit SGameObject(std::string name);
27
28 explicit SGameObject(std::string name, std::string hash);
29
30 ~SGameObject() override;
31
32 virtual void Init();
33
34 virtual void Tick(float elapsedTime);
35
36 void Exterminate() override;
37
41 void Destroy() override;
42
43 void SetUndestroyable(bool enable) override;
44
45 void AddChild(SGameObject* object);
46
47 void RemoveChildren(bool isAllLevel = false);
48
49 void RemoveChild(SGameObject* object);
50
51 SGameObject* GetParent() const;
52
53 void SetParent(SGameObject* object);
54
55 void RemoveParent();
56
57 const std::list<SGameObject*>& GetChildren() const;
58
63 void AddComponent(SComponent* component);
64
65 template <class T>
66 T* GetComponent();
67
68 template <class T>
69 T* GetComponentByHash(const std::string& id) const;
70
71 SComponent* GetSComponentByHash(const std::string& hash) const;
72
73 const std::list<SComponent*>& GetComponents() const;
74
75 HSQOBJECT GetCustomComponent(const char* className);
76
77 void DeleteComponent(SComponent* component);
78
79 template <class T>
80 T* CreateComponent();
81
82 SComponent* CreateComponent(const char* type);
83
84 SGameObject* Find(std::string name) const;
85
86 SGameObject* FindLocalByID(const std::string& id);
87
88 static SGameObject* FindByID(std::string id);
89
90 static SGameObject* FindByHash(const std::string& hash);
91
92
93 std::string GetName() const {
94 return m_name;
95 }
96
97 std::string GetID() const;
98
99 std::string GetID(const SComponent* component) const;
100
101 void SetName(std::string name) {
102 m_name = std::move(name);
103 }
104
105 TransformInterface* GetTransform() const {
106 return m_transform;
107 }
108
109 STATUS GetStatus() const {
110 return m_status;
111 }
112
113 bool GetIsEnable() const;
114
115 void SetIsEnable(bool is_enable);
116
117 void SetHash(std::string& hash) override;
118
119 private:
120 void UpdateComponent(float elapsedTime);
121
122 private:
123 std::list<SGameObject*> m_children;
124 SGameObject* m_parent = nullptr;
125
126 std::list<SComponent*> m_components;
127 std::string m_name;
128 TransformInterface* m_transform;
129 bool isEnable = true;
130 STATUS m_status = INIT;
131 bool m_isPrefab = false;
132 std::string m_resourceID;
133 public:
134 bool isPrefab(bool OnlyThisObject = false) const;
135
136 void SetIsPrefab(bool m_isPrefab);
137
138 std::string GetResourceID() const;
139
140 void SetResourceID(const std::string& resID, bool setChildren = false);
141 };
142
143
144 template <class T>
145 T* SGameObject::GetComponent() {
146 const auto& ref = ReflectionRef<T>();
147 for (const auto& component : m_components) {
148 if (component == nullptr) continue;
149 if (ref.IsSameClass(component)) {
150 return static_cast<T*>(component);
151 }
152 }
153
154 return nullptr;
155 }
156
157 template <class T>
158 T* SGameObject::GetComponentByHash(const std::string& id) const {
159 return static_cast<T*>(GetSComponentByHash(id));
160 }
161
162 template <class T>
163 T* SGameObject::CreateComponent() {
164 T* component = new T(this);
165 AddComponent(component);
166 if (m_status == IDLE)
167 component->Init();
168 return component;
169 }
170}
void AddComponent(SComponent *component)
컴포넌트를 이 오브젝트에 추가합니다.
void Destroy() override
자동 삭제가 아닌 특정한 상황에서 삭제될 때 호출되는 함수