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 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 CORE->GetCore(GameObjectMgr)->Remove(this);
57}
58
60 {
61 auto iter = m_components.begin();
62 while (iter != m_components.end()) {
63 auto component = *iter;
64 m_components.erase(iter++);
65
66 if (component == nullptr) continue;
67 CORE->GetCore(MemoryMgr)->ReleaseObject(component);
68 }
69 }
70
71 m_components.clear();
72
73 if (m_parent != nullptr) {
74 RemoveParent();
75 }
76
77 {
78 auto iter = m_children.begin();
79 while (iter != m_children.end()) {
80 auto object = *iter;
81 m_children.erase(iter++);
82
83 if (object == nullptr) continue;
84 object->Destroy();
85 }
86 }
87 m_status = DESTROY;
88 CORE->GetCore(GameObjectMgr)->AddDestroyObject(this);
89}
90
91void SGameObject::AddChild(SGameObject* object) {
92 if (object == nullptr) return;
93 object->RemoveParent();
94 m_children.push_back(object);
95 object->m_parent = this;
96}
97
98void SGameObject::RemoveChildren(bool isAllLevel) {
99 for (const auto& child: m_children) {
100 child->RemoveChildren(isAllLevel);
101 }
102 m_children.clear();
103}
104
105void SGameObject::RemoveChild(SGameObject* object) {
106 if (object == nullptr) return;
107 m_children.remove(object);
108 object->m_parent = nullptr;
109}
110
111SGameObject* SGameObject::GetParent() const {
112 return m_parent;
113}
114
115void SGameObject::SetParent(SGameObject* object) {
116 object->AddChild(this);
117}
118
119void SGameObject::RemoveParent() {
120 if (m_parent == nullptr) return;
121 m_parent->RemoveChild(this);
122}
123
124const std::list<SGameObject*>& SGameObject::GetChildren() const {
125 return m_children;
126}
127
129 auto str_class = component->GetClassType();
130 m_components.push_back(component);
131}
132
133const std::list<SComponent*>& SGameObject::GetComponents() const {
134 return m_components;
135}
136
137HSQOBJECT SGameObject::GetCustomComponent(const char* className) {
138 for (const auto& component: m_components) {
139 if (component == nullptr) continue;
140 if (dynamic_cast<CustomComponent*>(component)) {
141 auto customComponent = static_cast<CustomComponent*>(component);
142 if (customComponent->SGetClassName() != className) continue;
143 return customComponent->GetClassInstance().GetObject();
144 }
145 }
146 auto obj = HSQOBJECT();
147 obj._type = OT_NULL;
148
149 return obj;
150}
151
152void SGameObject::DeleteComponent(SComponent* component) {
153 m_components.remove(component);
154}
155
156std::string SGameObject::GetID() const {
157 std::string id;
158 for (const SGameObject* node = const_cast<SGameObject*>(this);; node = node->GetParent()) {
159 if (node == nullptr) break;
160
161 id = node->GetName() + (id.empty() ? "" : "/") + id;
162 }
163
164 return id;
165}
166
167std::string SGameObject::GetID(const SComponent* component) const {
168 if (component == nullptr) return "";
169
170 const auto& object = component->GetGameObject();
171 return object->m_hash + "?" + component->GetClassType();
172}
173
174SGameObject* SGameObject::Find(std::string name) const {
175 return CORE->GetCore(GameObjectMgr)->Find(ConvertSpaceStr(std::move(name), true));
176}
177
178
179SGameObject* SGameObject::FindLocalByID(const std::string& id) {
180 const auto& currentId = GetID();
181 if (currentId == id) return this;
182
183 for (const auto& child: m_children) {
184 const auto& result = child->FindLocalByID(id);
185 if (result != nullptr) return result;
186 }
187 return nullptr;
188}
189
190SGameObject* SGameObject::FindByID(std::string id) {
191 return CORE->GetCore(GameObjectMgr)->FindByID(ConvertSpaceStr(std::move(id), true));
192}
193
194SGameObject* SGameObject::FindByHash(const std::string& hash) {
195 return CORE->GetCore(GameObjectMgr)->FindByHash(ConvertSpaceStr(hash, true));
196}
197
198bool SGameObject::GetIsEnable() const {
199 return isEnable;
200}
201
202void SGameObject::SetIsEnable(bool is_enable) {
203 isEnable = is_enable;
204 for (const auto& component: m_components) {
205 if (component == nullptr) continue;
206 component->SetIsEnable(is_enable);
207 }
208}
209
210void SGameObject::UpdateComponent(float elapsedTime) {
211 for (const auto& component: m_components) {
212 if (m_status < IDLE) return;
213 if (component == nullptr) continue;
214
215 if (m_status == INIT) {
216 component->Init();
217 continue;
218 }
219
220 if (component->GetIsEnable())
221 component->Tick(elapsedTime);
222 }
223
224 if (m_status == INIT) m_status = IDLE;
225}
226
227void SGameObject::SetUndestroyable(bool enable) {
228 SObject::SetUndestroyable(enable);
229
230 for (const auto& component: m_components) {
231 component->SetUndestroyable(enable);
232 }
233
234 for (const auto& child: m_children) {
235 child->SetUndestroyable(enable);
236 }
237}
238
239bool SGameObject::isPrefab(bool OnlyThisObject) const {
240 if (m_isPrefab || m_parent == nullptr || OnlyThisObject) return m_isPrefab;
241
242 return m_parent->isPrefab();
243}
244
245void SGameObject::SetIsPrefab(bool m_isPrefab) {
246 SGameObject::m_isPrefab = m_isPrefab;
247}
248
249std::string SGameObject::GetResourceID() const {
250 return m_resourceID;
251}
252
253void SGameObject::SetResourceID(const std::string& resID, bool setChildren) {
254 m_resourceID = resID;
255
256 if (setChildren) {
257 for (const auto& child: m_children) {
258 child->SetResourceID(resID, setChildren);
259 }
260 }
261}
262
263SComponent* SGameObject::GetSComponentByHash(const std::string& hash) const {
264 const auto& object = CORE->GetCore(GameObjectMgr)->FindByHash(ConvertSpaceStr(hash, true));
265 if (object == nullptr) return nullptr;
266
267 std::string componentName = split(hash, '?')[1];
268
269 for (const auto& component: object->m_components) {
270 if (componentName == component->GetClassType()) {
271 return component;
272 }
273 }
274 return nullptr;
275}
276
277std::string GetMetaString(const SGameObject& object, unsigned int startIndex) {
278 std::string&& id = object.GetID().substr(startIndex);
279 std::string&& hash = object.GetHash();
280
281 std::string result = "<hash id=\"" + std::move(id) + "\">" + std::move(hash) + "</hash>";
282
283 const auto& children = object.GetChildren();
284 for (const auto& child: children) {
285 result += '\n' + GetMetaString(*child, startIndex);
286 }
287 return result;
288}
289
290std::string SGameObject::GenerateMeta() {
291 unsigned int startIndex = GetID().size() - GetName().size();
292 return GetMetaString(*this, startIndex);
293}
294
void AddComponent(SComponent *component)
컴포넌트를 이 오브젝트에 추가합니다.
void Destroy() override
자동 삭제가 아닌 특정한 상황에서 삭제될 때 호출되는 함수