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) {
30 SObject::SetHash(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() {
57 auto iter = m_components.begin();
58 while (iter != m_components.end()) {
59 auto component = *iter;
60 m_components.erase(iter++);
62 if (component ==
nullptr)
continue;
63 CORE->GetCore(
MemoryMgr)->ReleaseObject(component);
71 auto iter = m_components.begin();
72 while (iter != m_components.end()) {
73 auto component = *iter;
74 m_components.erase(iter++);
76 if (component ==
nullptr)
continue;
77 CORE->GetCore(
MemoryMgr)->ReleaseObject(component);
83 if (m_parent !=
nullptr) {
88 auto iter = m_children.begin();
89 while (iter != m_children.end()) {
91 m_children.erase(iter++);
93 if (
object ==
nullptr)
continue;
102 if (
object ==
nullptr)
return;
103 object->RemoveParent();
104 m_children.push_back(
object);
105 object->m_parent =
this;
108void SGameObject::RemoveChildren(
bool isAllLevel) {
109 for (
const auto& child: m_children) {
110 child->RemoveChildren(isAllLevel);
115void SGameObject::RemoveChild(
SGameObject*
object) {
116 if (
object ==
nullptr)
return;
117 m_children.remove(
object);
118 object->m_parent =
nullptr;
126 object->AddChild(
this);
129void SGameObject::RemoveParent() {
130 if (m_parent ==
nullptr)
return;
131 m_parent->RemoveChild(
this);
134const std::list<SGameObject*>& SGameObject::GetChildren()
const {
139 m_components.push_back(component);
142const std::list<SComponent*>& SGameObject::GetComponents()
const {
146HSQOBJECT SGameObject::GetCustomComponent(
const char* className) {
147 for (
const auto& component: m_components) {
148 if (component ==
nullptr)
continue;
151 if (customComponent->SGetClassName() != className)
continue;
152 return customComponent->GetClassInstance().GetObject();
155 auto obj = HSQOBJECT();
161void SGameObject::DeleteComponent(
SComponent* component) {
162 m_components.remove(component);
165SComponent* SGameObject::CreateComponent(
const char* type) {
167 component->SetGameObject(
this);
169 if (m_status == IDLE)
174std::string SGameObject::GetID()
const {
177 if (node ==
nullptr)
break;
179 id = node->GetName() + (
id.empty() ?
"" :
"/") + id;
185std::string SGameObject::GetID(
const SComponent* component)
const {
186 if (component ==
nullptr)
return "";
188 const auto&
object = component->GetGameObject();
189 return object->m_hash +
"?" + component->GetClassType();
192SGameObject* SGameObject::Find(std::string name)
const {
193 return CORE->GetCore(
GameObjectMgr)->Find(ConvertSpaceStr(std::move(name),
true));
197SGameObject* SGameObject::FindLocalByID(
const std::string&
id) {
198 const auto& currentId = GetID();
199 if (currentId ==
id)
return this;
201 for (
const auto& child: m_children) {
202 const auto& result = child->FindLocalByID(
id);
203 if (result !=
nullptr)
return result;
208SGameObject* SGameObject::FindByID(std::string
id) {
209 return CORE->GetCore(
GameObjectMgr)->FindByID(ConvertSpaceStr(std::move(
id),
true));
212SGameObject* SGameObject::FindByHash(
const std::string& hash) {
213 return CORE->GetCore(
GameObjectMgr)->FindByHash(ConvertSpaceStr(hash,
true));
216bool SGameObject::GetIsEnable()
const {
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);
228void SGameObject::UpdateComponent(
float elapsedTime) {
229 for (
const auto& component: m_components) {
230 if (m_status < IDLE)
return;
231 if (component ==
nullptr)
continue;
233 if (m_status == INIT) {
238 if (component->GetIsEnable())
239 component->Tick(elapsedTime);
242 if (m_status == INIT) m_status = IDLE;
245void SGameObject::SetUndestroyable(
bool enable) {
246 SObject::SetUndestroyable(enable);
248 for (
const auto& component: m_components) {
249 component->SetUndestroyable(enable);
252 for (
const auto& child: m_children) {
253 child->SetUndestroyable(enable);
257bool SGameObject::isPrefab(
bool OnlyThisObject)
const {
258 if (m_isPrefab || m_parent ==
nullptr || OnlyThisObject)
return m_isPrefab;
260 return m_parent->isPrefab();
263void SGameObject::SetIsPrefab(
bool m_isPrefab) {
264 SGameObject::m_isPrefab = m_isPrefab;
267std::string SGameObject::GetResourceID()
const {
271void SGameObject::SetResourceID(
const std::string& resID,
bool setChildren) {
272 m_resourceID = resID;
275 for (
const auto& child: m_children) {
276 child->SetResourceID(resID, setChildren);
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;
285 std::string componentName = split(hash,
'?')[1];
287 for (
const auto& component: object->m_components) {
288 if (componentName == component->GetClassType()) {
295void SGameObject::SetHash(std::string& hash) {
296 const std::string prevHash = std::string(m_hash);
297 SObject::SetHash(hash);
void AddComponent(SComponent *component)
컴포넌트를 이 오브젝트에 추가합니다.
void Destroy() override
자동 삭제가 아닌 특정한 상황에서 삭제될 때 호출되는 함수