CSEngine
Loading...
Searching...
No Matches
SGameObject.cpp
1#include "SGameObject.h"
2
3#include <utility>
4
5#include "../Manager/MemoryMgr.h"
6#include "../Manager/GameObjectMgr.h"
7#include "../Component/TransformComponent.h"
8#include "../Component/CustomComponent.h"
9#include "../Manager/EngineCore.h"
10#include "../Util/Loader/XML/XML.h"
11
12using namespace CSE;
13
14SGameObject::SGameObject() {
15 CORE->GetCore(GameObjectMgr)->Register(this);
16 m_transform = CreateComponent<TransformComponent>();
17
18 SGameObject::Init();
19}
20
21SGameObject::SGameObject(std::string name) {
22 CORE->GetCore(GameObjectMgr)->Register(this);
23 m_name = std::move(name);
24 m_transform = CreateComponent<TransformComponent>();
25
26 SGameObject::Init();
27}
28
29SGameObject::SGameObject(std::string name, std::string hash) {
30 SObject::SetHash(hash);
31 CORE->GetCore(GameObjectMgr)->Register(this);
32 m_name = std::move(name);
33 m_transform = CreateComponent<TransformComponent>();
34
35 SGameObject::Init();
36}
37
38SGameObject::~SGameObject() = default;
39
40void SGameObject::Init() {
41 // for (auto component : m_components) {
42 // if (component == nullptr) continue;
43 //
44 // component->Init();
45 //
46 // }
47}
48
49void SGameObject::Tick(float elapsedTime) {
50 if (!isEnable) return;
51 if (isPrefab()) return;
52 UpdateComponent(elapsedTime);
53}
54
55void SGameObject::Exterminate() {
56 {
57 auto iter = m_components.begin();
58 while (iter != m_components.end()) {
59 auto component = *iter;
60 m_components.erase(iter++);
61
62 if (component == nullptr) continue;
63 CORE->GetCore(MemoryMgr)->ReleaseObject(component);
64 }
65 }
66 CORE->GetCore(GameObjectMgr)->Remove(this);
67}
68
70 {
71 auto iter = m_components.begin();
72 while (iter != m_components.end()) {
73 auto component = *iter;
74 m_components.erase(iter++);
75
76 if (component == nullptr) continue;
77 CORE->GetCore(MemoryMgr)->ReleaseObject(component);
78 }
79 }
80
81 m_components.clear();
82
83 if (m_parent != nullptr) {
84 RemoveParent();
85 }
86
87 {
88 auto iter = m_children.begin();
89 while (iter != m_children.end()) {
90 auto object = *iter;
91 m_children.erase(iter++);
92
93 if (object == nullptr) continue;
94 object->Destroy();
95 }
96 }
97 m_status = DESTROY;
98 CORE->GetCore(GameObjectMgr)->AddDestroyObject(this);
99}
100
101void SGameObject::AddChild(SGameObject* object) {
102 if (object == nullptr) return;
103 object->RemoveParent();
104 m_children.push_back(object);
105 object->m_parent = this;
106}
107
108void SGameObject::RemoveChildren(bool isAllLevel) {
109 for (const auto& child: m_children) {
110 child->RemoveChildren(isAllLevel);
111 }
112 m_children.clear();
113}
114
115void SGameObject::RemoveChild(SGameObject* object) {
116 if (object == nullptr) return;
117 m_children.remove(object);
118 object->m_parent = nullptr;
119}
120
121SGameObject* SGameObject::GetParent() const {
122 return m_parent;
123}
124
125void SGameObject::SetParent(SGameObject* object) {
126 object->AddChild(this);
127}
128
129void SGameObject::RemoveParent() {
130 if (m_parent == nullptr) return;
131 m_parent->RemoveChild(this);
132}
133
134const std::list<SGameObject*>& SGameObject::GetChildren() const {
135 return m_children;
136}
137
139 m_components.push_back(component);
140}
141
142const std::list<SComponent*>& SGameObject::GetComponents() const {
143 return m_components;
144}
145
146HSQOBJECT SGameObject::GetCustomComponent(const char* className) {
147 for (const auto& component: m_components) {
148 if (component == nullptr) continue;
149 if (dynamic_cast<CustomComponent*>(component)) {
150 auto customComponent = static_cast<CustomComponent*>(component);
151 if (customComponent->SGetClassName() != className) continue;
152 return customComponent->GetClassInstance().GetObject();
153 }
154 }
155 auto obj = HSQOBJECT();
156 obj._type = OT_NULL;
157
158 return obj;
159}
160
161void SGameObject::DeleteComponent(SComponent* component) {
162 m_components.remove(component);
163}
164
165SComponent* SGameObject::CreateComponent(const char* type) {
166 SComponent* component = static_cast<SComponent*>(ReflectionObject::NewObject(type));
167 component->SetGameObject(this);
168 AddComponent(component);
169 if (m_status == IDLE)
170 component->Init();
171 return component;
172}
173
174std::string SGameObject::GetID() const {
175 std::string id;
176 for (const SGameObject* node = const_cast<SGameObject*>(this);; node = node->GetParent()) {
177 if (node == nullptr) break;
178
179 id = node->GetName() + (id.empty() ? "" : "/") + id;
180 }
181
182 return id;
183}
184
185std::string SGameObject::GetID(const SComponent* component) const {
186 if (component == nullptr) return "";
187
188 const auto& object = component->GetGameObject();
189 return object->m_hash + "?" + component->GetClassType();
190}
191
192SGameObject* SGameObject::Find(std::string name) const {
193 return CORE->GetCore(GameObjectMgr)->Find(ConvertSpaceStr(std::move(name), true));
194}
195
196
197SGameObject* SGameObject::FindLocalByID(const std::string& id) {
198 const auto& currentId = GetID();
199 if (currentId == id) return this;
200
201 for (const auto& child: m_children) {
202 const auto& result = child->FindLocalByID(id);
203 if (result != nullptr) return result;
204 }
205 return nullptr;
206}
207
208SGameObject* SGameObject::FindByID(std::string id) {
209 return CORE->GetCore(GameObjectMgr)->FindByID(ConvertSpaceStr(std::move(id), true));
210}
211
212SGameObject* SGameObject::FindByHash(const std::string& hash) {
213 return CORE->GetCore(GameObjectMgr)->FindByHash(ConvertSpaceStr(hash, true));
214}
215
216bool SGameObject::GetIsEnable() const {
217 return isEnable;
218}
219
220void SGameObject::SetIsEnable(bool is_enable) {
221 isEnable = is_enable;
222 for (const auto& component: m_components) {
223 if (component == nullptr) continue;
224 component->SetIsEnable(is_enable);
225 }
226}
227
228void SGameObject::UpdateComponent(float elapsedTime) {
229 for (const auto& component: m_components) {
230 if (m_status < IDLE) return;
231 if (component == nullptr) continue;
232
233 if (m_status == INIT) {
234 component->Init();
235 continue;
236 }
237
238 if (component->GetIsEnable())
239 component->Tick(elapsedTime);
240 }
241
242 if (m_status == INIT) m_status = IDLE;
243}
244
245void SGameObject::SetUndestroyable(bool enable) {
246 SObject::SetUndestroyable(enable);
247
248 for (const auto& component: m_components) {
249 component->SetUndestroyable(enable);
250 }
251
252 for (const auto& child: m_children) {
253 child->SetUndestroyable(enable);
254 }
255}
256
257bool SGameObject::isPrefab(bool OnlyThisObject) const {
258 if (m_isPrefab || m_parent == nullptr || OnlyThisObject) return m_isPrefab;
259
260 return m_parent->isPrefab();
261}
262
263void SGameObject::SetIsPrefab(bool m_isPrefab) {
264 SGameObject::m_isPrefab = m_isPrefab;
265}
266
267std::string SGameObject::GetResourceID() const {
268 return m_resourceID;
269}
270
271void SGameObject::SetResourceID(const std::string& resID, bool setChildren) {
272 m_resourceID = resID;
273
274 if (setChildren) {
275 for (const auto& child: m_children) {
276 child->SetResourceID(resID, setChildren);
277 }
278 }
279}
280
281SComponent* SGameObject::GetSComponentByHash(const std::string& hash) const {
282 const auto& object = CORE->GetCore(GameObjectMgr)->FindByHash(ConvertSpaceStr(hash, true));
283 if (object == nullptr) return nullptr;
284
285 std::string componentName = split(hash, '?')[1];
286
287 for (const auto& component: object->m_components) {
288 if (componentName == component->GetClassType()) {
289 return component;
290 }
291 }
292 return nullptr;
293}
294
295void SGameObject::SetHash(std::string& hash) {
296 const std::string prevHash = std::string(m_hash);
297 SObject::SetHash(hash);
298 CORE->GetCore(GameObjectMgr)->ChangeHash(prevHash, hash);
299}
void AddComponent(SComponent *component)
컴포넌트를 이 오브젝트에 추가합니다.
void Destroy() override
자동 삭제가 아닌 특정한 상황에서 삭제될 때 호출되는 함수