1#include "SGameObject.h"
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"
14SGameObject::SGameObject() {
16 m_transform = CreateComponent<TransformComponent>();
21SGameObject::SGameObject(std::string name) {
23 m_name = std::move(name);
24 m_transform = CreateComponent<TransformComponent>();
29SGameObject::SGameObject(std::string name, std::string hash) {
32 m_name = std::move(name);
33 m_transform = CreateComponent<TransformComponent>();
38SGameObject::~SGameObject() =
default;
40void SGameObject::Init() {
49void SGameObject::Tick(
float elapsedTime) {
50 if (!isEnable)
return;
51 if (isPrefab())
return;
52 UpdateComponent(elapsedTime);
55void SGameObject::Exterminate() {
61 auto iter = m_components.begin();
62 while (iter != m_components.end()) {
63 auto component = *iter;
64 m_components.erase(iter++);
66 if (component ==
nullptr)
continue;
67 CORE->GetCore(
MemoryMgr)->ReleaseObject(component);
73 if (m_parent !=
nullptr) {
78 auto iter = m_children.begin();
79 while (iter != m_children.end()) {
81 m_children.erase(iter++);
83 if (
object ==
nullptr)
continue;
92 if (
object ==
nullptr)
return;
93 object->RemoveParent();
94 m_children.push_back(
object);
95 object->m_parent =
this;
98void SGameObject::RemoveChildren(
bool isAllLevel) {
99 for (
const auto& child: m_children) {
100 child->RemoveChildren(isAllLevel);
105void SGameObject::RemoveChild(
SGameObject*
object) {
106 if (
object ==
nullptr)
return;
107 m_children.remove(
object);
108 object->m_parent =
nullptr;
116 object->AddChild(
this);
119void SGameObject::RemoveParent() {
120 if (m_parent ==
nullptr)
return;
121 m_parent->RemoveChild(
this);
124const std::list<SGameObject*>& SGameObject::GetChildren()
const {
129 auto str_class = component->GetClassType();
130 m_components.push_back(component);
133const std::list<SComponent*>& SGameObject::GetComponents()
const {
137HSQOBJECT SGameObject::GetCustomComponent(
const char* className) {
138 for (
const auto& component: m_components) {
139 if (component ==
nullptr)
continue;
142 if (customComponent->SGetClassName() != className)
continue;
143 return customComponent->GetClassInstance().GetObject();
146 auto obj = HSQOBJECT();
152void SGameObject::DeleteComponent(
SComponent* component) {
153 m_components.remove(component);
156std::string SGameObject::GetID()
const {
159 if (node ==
nullptr)
break;
161 id = node->GetName() + (
id.empty() ?
"" :
"/") +
id;
167std::string SGameObject::GetID(
const SComponent* component)
const {
168 if (component ==
nullptr)
return "";
170 const auto&
object = component->GetGameObject();
171 return object->m_hash +
"?" + component->GetClassType();
174SGameObject* SGameObject::Find(std::string name)
const {
175 return CORE->GetCore(
GameObjectMgr)->Find(ConvertSpaceStr(std::move(name),
true));
179SGameObject* SGameObject::FindLocalByID(
const std::string&
id) {
180 const auto& currentId = GetID();
181 if (currentId ==
id)
return this;
183 for (
const auto& child: m_children) {
184 const auto& result = child->FindLocalByID(
id);
185 if (result !=
nullptr)
return result;
190SGameObject* SGameObject::FindByID(std::string
id) {
191 return CORE->GetCore(
GameObjectMgr)->FindByID(ConvertSpaceStr(std::move(
id),
true));
194SGameObject* SGameObject::FindByHash(
const std::string& hash) {
195 return CORE->GetCore(
GameObjectMgr)->FindByHash(ConvertSpaceStr(hash,
true));
198bool SGameObject::GetIsEnable()
const {
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);
210void SGameObject::UpdateComponent(
float elapsedTime) {
211 for (
const auto& component: m_components) {
212 if (m_status < IDLE)
return;
213 if (component ==
nullptr)
continue;
215 if (m_status == INIT) {
220 if (component->GetIsEnable())
221 component->Tick(elapsedTime);
224 if (m_status == INIT) m_status = IDLE;
227void SGameObject::SetUndestroyable(
bool enable) {
228 SObject::SetUndestroyable(enable);
230 for (
const auto& component: m_components) {
231 component->SetUndestroyable(enable);
234 for (
const auto& child: m_children) {
235 child->SetUndestroyable(enable);
239bool SGameObject::isPrefab(
bool OnlyThisObject)
const {
240 if (m_isPrefab || m_parent ==
nullptr || OnlyThisObject)
return m_isPrefab;
242 return m_parent->isPrefab();
245void SGameObject::SetIsPrefab(
bool m_isPrefab) {
246 SGameObject::m_isPrefab = m_isPrefab;
249std::string SGameObject::GetResourceID()
const {
253void SGameObject::SetResourceID(
const std::string& resID,
bool setChildren) {
254 m_resourceID = resID;
257 for (
const auto& child: m_children) {
258 child->SetResourceID(resID, setChildren);
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;
267 std::string componentName = split(hash,
'?')[1];
269 for (
const auto& component: object->m_components) {
270 if (componentName == component->GetClassType()) {
277std::string GetMetaString(
const SGameObject&
object,
unsigned int startIndex) {
278 std::string&&
id =
object.GetID().substr(startIndex);
279 std::string&& hash =
object.GetHash();
281 std::string result =
"<hash id=\"" + std::move(
id) +
"\">" + std::move(hash) +
"</hash>";
283 const auto& children =
object.GetChildren();
284 for (
const auto& child: children) {
285 result +=
'\n' + GetMetaString(*child, startIndex);
290std::string SGameObject::GenerateMeta() {
291 unsigned int startIndex = GetID().size() - GetName().size();
292 return GetMetaString(*
this, startIndex);
void AddComponent(SComponent *component)
컴포넌트를 이 오브젝트에 추가합니다.
void Destroy() override
자동 삭제가 아닌 특정한 상황에서 삭제될 때 호출되는 함수