Skylicht Engine
Loading...
Searching...
No Matches
CGameObject.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "pch.h"
28#include "Components/CComponentSystem.h"
29#include "Components/CNullComponent.h"
30#include "Components/CDependentComponent.h"
31#include "Transform/CTransformEuler.h"
32#include "Transform/CTransformMatrix.h"
33#include "Serializable/CObjectSerializable.h"
34
35namespace Skylicht
36{
37 class CScene;
38 class CZone;
39 class CEntity;
40 class CEntityManager;
41
51 class SKYLICHT_API CGameObject
52 {
53 friend class CDependentComponent;
54
55 protected:
56 std::string m_objectID;
57
58 std::wstring m_name;
59 std::wstring m_defaultName;
60 std::string m_namec;
61
62 bool m_static;
63 bool m_enable;
64 bool m_visible;
65 bool m_lock;
66 bool m_isContainer;
67
68 bool m_editorObject;
69 bool m_enableEditorChange;
70 bool m_enableEditorSelect;
71 bool m_enableEndUpdate;
72
73 u32 m_cullingLayer;
74
75 CEntity* m_entity;
76 CGameObject* m_parent;
77 CZone* m_zone;
78
79 void* m_tagData;
80 int m_tagDataInt;
81 std::string m_tagDataString;
82
83 std::vector<CComponentSystem*> m_components;
84
85 CTransform* m_transform;
86 CTransformEuler* m_transformEuler;
87 CTransformMatrix* m_transformMatrix;
88
89 std::string m_templateId;
90 std::string m_templateAsset;
91 std::string m_templateObjectId;
92 bool m_templateChanged;
93
94 public:
95 CGameObject(CGameObject* parent, CZone* zone);
96
97 virtual ~CGameObject();
98
99 protected:
100 void initNull();
101
102 public:
103 CEntity* createEntity();
104
105 void destroyEntity();
106
107 void updateEntityParent();
108
109 virtual void setID(const char* id)
110 {
111 m_objectID = id;
112 }
113
114 inline std::string& getID()
115 {
116 return m_objectID;
117 }
118
119 inline const wchar_t* getName()
120 {
121 return m_name.c_str();
122 }
123
124 const char* getNameA();
125
126 inline const wchar_t* getDefaultName()
127 {
128 return m_defaultName.c_str();
129 }
130
131 inline void setName(const wchar_t* lpName)
132 {
133 m_name = lpName;
134
135 if (m_defaultName == L"")
136 m_defaultName = lpName;
137 }
138
139 void setName(const char* lpName);
140
141 inline bool isTemplateAsset()
142 {
143 return !m_templateAsset.empty();
144 }
145
146 inline const char* getTemplateAsset()
147 {
148 return m_templateAsset.c_str();
149 }
150
151 inline void setTemplateAsset(const char* asset)
152 {
153 m_templateAsset = asset;
154 }
155
156 inline void unpackTemplate()
157 {
158 m_templateAsset.clear();
159 m_templateId.clear();
160 }
161
162 inline const char* getTemplateID()
163 {
164 return m_templateId.c_str();
165 }
166
167 inline void setTemplateID(const char* id)
168 {
169 m_templateId = id;
170 }
171
172 inline const char* getTemplateObjectID()
173 {
174 return m_templateObjectId.c_str();
175 }
176
177 inline void setTemplateObjectID(const char* id)
178 {
179 m_templateObjectId = id;
180 }
181
182 inline bool isTemplateObject()
183 {
184 return !m_templateId.empty();
185 }
186
187 CGameObject* getParentTemplate();
188
189 virtual void setTemplateChanged(bool b);
190
191 inline bool isTemplateChanged()
192 {
193 return m_templateChanged;
194 }
195
196 inline CGameObject* getParent()
197 {
198 return m_parent;
199 }
200
201 inline CZone* getZone()
202 {
203 return m_zone;
204 }
205
206 CScene* getScene();
207
208 inline void setParent(CGameObject* p)
209 {
210 m_parent = p;
211 }
212
213 inline CEntity* getEntity()
214 {
215 return m_entity;
216 }
217
218 CEntityManager* getEntityManager();
219
220 inline CTransform* getTransform()
221 {
222 return m_transform;
223 }
224
225 inline CTransformEuler* getTransformEuler()
226 {
227 return m_transformEuler;
228 }
229
230 inline CTransformMatrix* getTransformMatrix()
231 {
232 return m_transformMatrix;
233 }
234
235 void setupMatrixTransform();
236
237 void setupEulerTransform();
238
239 core::vector3df getPosition();
240
241 core::quaternion getRotation();
242
243 core::vector3df getUp();
244
245 core::vector3df getFront();
246
247 const core::matrix4& getWorldTransform();
248
249 core::matrix4 calcWorldTransform();
250
251 inline bool isEnable()
252 {
253 return m_enable;
254 }
255
256 inline bool isEnableEndUpdate()
257 {
258 return m_enableEndUpdate;
259 }
260
261 inline bool isVisible()
262 {
263 return m_visible;
264 }
265
266 bool isLock();
267
268 inline bool isSelfLock()
269 {
270 return m_lock;
271 }
272
273 virtual void setEnable(bool b)
274 {
275 m_enable = b;
276 }
277
278 virtual void setEnableEndUpdate(bool b)
279 {
280 m_enableEndUpdate = b;
281 }
282
283 virtual void setStatic(bool b)
284 {
285 m_static = b;
286 }
287
288 virtual bool isStatic()
289 {
290 return m_static;
291 }
292
293 inline void setLock(bool b)
294 {
295 m_lock = b;
296 }
297
298 virtual void setVisible(bool b);
299
300 inline bool isContainer()
301 {
302 return m_isContainer;
303 }
304
305 void setCullingLayer(u32 layer);
306
307 void setCullingLayerOnOff(u32 value, bool on);
308
309 inline u32 getCullingLayer()
310 {
311 return m_cullingLayer;
312 }
313
314 inline void setEditorObject(bool b)
315 {
316 m_editorObject = b;
317 }
318
319 inline bool isEditorObject()
320 {
321 return m_editorObject;
322 }
323
324 inline void tagData(void* data)
325 {
326 m_tagData = data;
327 }
328
329 inline void tagData(int data)
330 {
331 m_tagDataInt = data;
332 }
333
334 inline void tagData(std::string& s)
335 {
336 m_tagDataString = s;
337 }
338
339 inline void* getTagData()
340 {
341 return m_tagData;
342 }
343
344 inline int getTagDataInt()
345 {
346 return m_tagDataInt;
347 }
348
349 const char* getTagDataString()
350 {
351 return m_tagDataString.c_str();
352 }
353
354 virtual void updateObject();
355
356 virtual void endUpdate();
357
358 virtual void remove();
359
360 template<class T>
361 T* addComponent();
362
363 template<class T>
364 T* getComponent();
365
366 template<class T>
367 bool removeComponent();
368
369 bool removeComponent(CComponentSystem* comp);
370
371 void releaseAllComponent();
372
373 CComponentSystem* addComponentByTypeName(const char* name);
374
375 CComponentSystem* getComponentByTypeName(const char* name);
376
377 inline std::vector<CComponentSystem*>& getListComponent()
378 {
379 return m_components;
380 }
381
382 int getComponentPosition(CComponentSystem* comp);
383
384 inline int getComponentCount()
385 {
386 return (int)m_components.size();
387 }
388
389 inline CComponentSystem* getComponentByPos(int i)
390 {
391 return m_components[i];
392 }
393
394 void moveComponentUp(CComponentSystem* comp);
395
396 void moveComponentDown(CComponentSystem* comp);
397
398 int getSerializableComponentCount();
399
400 void moveSerializableComponentUp(CComponentSystem* comp);
401
402 void moveSerializableComponentDown(CComponentSystem* comp);
403
404 void sortComponent(std::vector<std::string>& templateOrder);
405
406 inline void enableEditorChange(bool b)
407 {
408 m_enableEditorChange = b;
409 }
410
411 inline void enableEditorSelect(bool b)
412 {
413 m_enableEditorSelect = b;
414 }
415
416 inline bool isEnableEditorChange()
417 {
418 return m_enableEditorChange;
419 }
420
421 inline bool isEnableEditorSelect()
422 {
423 return m_enableEditorSelect;
424 }
425
426 void startComponent();
427
428 CObjectSerializable* createSerializable();
429
430 void loadSerializable(CObjectSerializable* object);
431
432 DECLARE_GETTYPENAME(CGameObject)
433 };
434
435 // typedef for array object
436 typedef std::vector<CGameObject*> ArrayGameObject;
437 typedef std::vector<CGameObject*>::iterator ArrayGameObjectIter;
438
439 // typedef for array component system
440 typedef std::vector<CComponentSystem*> ArrayComponent;
441 typedef std::vector<CComponentSystem*>::iterator ArrayComponentIter;
442
443 template<class T>
444 T* CGameObject::addComponent()
445 {
446 T* newComp = new T();
447 CComponentSystem* compSystem = dynamic_cast<CComponentSystem*>(newComp);
448 if (compSystem == NULL)
449 {
450 char exceptionInfo[512];
451 sprintf(exceptionInfo, "CGameObject::addComponent %s must inherit CComponentSystem", typeid(T).name());
452 os::Printer::log(exceptionInfo);
453 delete newComp;
454 return NULL;
455 }
456
457 m_components.push_back(compSystem);
458
459 compSystem->setOwner(this);
460 compSystem->initComponent();
461
462 CDependentComponent::createGetInstance()->createDependentComponent(compSystem);
463 return newComp;
464 }
465
466 template<class T>
467 T* CGameObject::getComponent()
468 {
469 ArrayComponentIter i = m_components.begin(), end = m_components.end();
470 while (i != end)
471 {
472 T* c = dynamic_cast<T*>(*i);
473 if (c != NULL)
474 return c;
475 ++i;
476 }
477 return NULL;
478 }
479
480 template<class T>
481 bool CGameObject::removeComponent()
482 {
483 ArrayComponentIter i = m_components.begin(), end = m_components.end();
484 while (i != end)
485 {
486 T* c = dynamic_cast<T*>(*i);
487 if (c != NULL)
488 {
489 m_components.erase(i);
490 delete c;
491 return true;
492 }
493 ++i;
494 }
495
496 return false;
497 }
498}
This is an abstract class that describes a component that is called to update continuously....
Definition CComponentSystem.h:43
This is the object class that describes an entity.
Definition CEntity.h:58
This object class manages all entities within a scene.
Definition CEntityManager.h:89
Definition CObjectSerializable.h:36
This object class manages all other objects, it represents the data of a scene.
Definition CScene.h:103
This is object class for classes that describe a GameObject's Transform.
Definition CTransformEuler.h:47
An abstract object class for classes that describe a GameObject's Transform.
Definition CTransform.h:47
This is an object class that describes a transform, but the input data is a matrix.
Definition CTransformMatrix.h:54
It's the primary container object for the CScene; all CGameObject have a CZone as their root parent.
Definition CZone.h:37
Quaternion class for representing rotations.
Definition quaternion.h:27
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition matrix4.h:2241
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58