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