Plasma Engine  2.0
Loading...
Searching...
No Matches
GameObject.h
1#pragma once
2
4
5#include <Foundation/Containers/HybridArray.h>
6#include <Foundation/Containers/SmallArray.h>
7#include <Foundation/SimdMath/SimdConversion.h>
8#include <Foundation/Time/Time.h>
9#include <Foundation/Types/TagSet.h>
10
11#include <Core/World/ComponentManager.h>
12#include <Core/World/GameObjectDesc.h>
13
14// Avoid conflicts with windows.h
15#ifdef SendMessage
16# undef SendMessage
17#endif
18
19enum class plVisibilityState : plUInt8;
20
31class PL_CORE_DLL plGameObject final
32{
33private:
34 enum
35 {
36#if PL_ENABLED(PL_PLATFORM_32BIT)
37 NUM_INPLACE_COMPONENTS = 12
38#else
39 NUM_INPLACE_COMPONENTS = 6
40#endif
41 };
42
43 friend class plWorld;
44 friend class plInternal::WorldData;
45 friend class plMemoryUtils;
46
48 plGameObject(const plGameObject& other);
50
51 void operator=(const plGameObject& other);
52
53public:
55 class PL_CORE_DLL ConstChildIterator
56 {
57 public:
58 const plGameObject& operator*() const;
59 const plGameObject* operator->() const;
60
61 operator const plGameObject*() const;
62
64 void Next();
65
67 bool IsValid() const;
68
70 void operator++();
71
72 private:
73 friend class plGameObject;
74
75 ConstChildIterator(plGameObject* pObject, const plWorld* pWorld);
76
77 plGameObject* m_pObject = nullptr;
78 const plWorld* m_pWorld = nullptr;
79 };
80
81 class PL_CORE_DLL ChildIterator : public ConstChildIterator
82 {
83 public:
84 plGameObject& operator*();
85 plGameObject* operator->();
86
87 operator plGameObject*();
88
89 private:
90 friend class plGameObject;
91
92 ChildIterator(plGameObject* pObject, const plWorld* pWorld);
93 };
94
96 plGameObjectHandle GetHandle() const;
97
99 void MakeDynamic();
100
102 void MakeStatic();
103
105 bool IsDynamic() const;
106
108 bool IsStatic() const;
109
121 void SetActiveFlag(bool bEnabled);
122
127 bool GetActiveFlag() const;
128
136 bool IsActive() const;
137
140
142 bool WasCreatedByPrefab() const { return m_Flags.IsSet(plObjectFlags::CreatedByPrefab); }
143
145 void SetName(plStringView sName);
146 void SetName(const plHashedString& sName);
147 plStringView GetName() const;
148 bool HasName(const plTempHashedString& sName) const;
149
151 void SetGlobalKey(plStringView sGlobalKey);
152 void SetGlobalKey(const plHashedString& sGlobalKey);
153 plStringView GetGlobalKey() const;
154
156 void EnableChildChangesNotifications();
157 void DisableChildChangesNotifications();
158
160 void EnableParentChangesNotifications();
161 void DisableParentChangesNotifications();
162
165 {
166 PreserveLocal,
167 PreserveGlobal
168 };
169
171 void SetParent(const plGameObjectHandle& hParent, plGameObject::TransformPreservation preserve = TransformPreservation::PreserveGlobal);
172
174 plGameObject* GetParent();
175
177 const plGameObject* GetParent() const;
178
180 void AddChild(const plGameObjectHandle& hChild, plGameObject::TransformPreservation preserve = TransformPreservation::PreserveGlobal);
181
183 void AddChildren(const plArrayPtr<const plGameObjectHandle>& children, plGameObject::TransformPreservation preserve = TransformPreservation::PreserveGlobal);
184
186 void DetachChild(const plGameObjectHandle& hChild, plGameObject::TransformPreservation preserve = TransformPreservation::PreserveGlobal);
187
189 void DetachChildren(const plArrayPtr<const plGameObjectHandle>& children, plGameObject::TransformPreservation preserve = TransformPreservation::PreserveGlobal);
190
192 plUInt32 GetChildCount() const;
193
195 ChildIterator GetChildren();
196
198 ConstChildIterator GetChildren() const;
199
201 plGameObject* FindChildByName(const plTempHashedString& sName, bool bRecursive = true);
202
210 plGameObject* FindChildByPath(plStringView sPath);
211
219 plGameObject* SearchForChildByNameSequence(plStringView sObjectSequence, const plRTTI* pExpectedComponent = nullptr);
220
222 void SearchForChildrenByNameSequence(plStringView sObjectSequence, const plRTTI* pExpectedComponent, plHybridArray<plGameObject*, 8>& out_objects);
223
224 plWorld* GetWorld();
225 const plWorld* GetWorld() const;
226
227
230 {
231 None,
232 UpdateImmediately,
233 };
234
239 void SetLocalPosition(plVec3 vPosition);
240 plVec3 GetLocalPosition() const;
241
242 void SetLocalRotation(plQuat qRotation);
243 plQuat GetLocalRotation() const;
244
245 void SetLocalScaling(plVec3 vScaling);
246 plVec3 GetLocalScaling() const;
247
248 void SetLocalUniformScaling(float fScaling);
249 float GetLocalUniformScaling() const;
250
251 plTransform GetLocalTransform() const;
252
253 void SetGlobalPosition(const plVec3& vPosition);
254 plVec3 GetGlobalPosition() const;
255
256 void SetGlobalRotation(const plQuat& qRotation);
257 plQuat GetGlobalRotation() const;
258
259 void SetGlobalScaling(const plVec3& vScaling);
260 plVec3 GetGlobalScaling() const;
261
262 void SetGlobalTransform(const plTransform& transform);
263 plTransform GetGlobalTransform() const;
264
266 plTransform GetLastGlobalTransform() const;
267
268 // Simd variants of above methods
269 void SetLocalPosition(const plSimdVec4f& vPosition, UpdateBehaviorIfStatic updateBehavior = UpdateBehaviorIfStatic::UpdateImmediately);
270 const plSimdVec4f& GetLocalPositionSimd() const;
271
272 void SetLocalRotation(const plSimdQuat& qRotation, UpdateBehaviorIfStatic updateBehavior = UpdateBehaviorIfStatic::UpdateImmediately);
273 const plSimdQuat& GetLocalRotationSimd() const;
274
275 void SetLocalScaling(const plSimdVec4f& vScaling, UpdateBehaviorIfStatic updateBehavior = UpdateBehaviorIfStatic::UpdateImmediately);
276 const plSimdVec4f& GetLocalScalingSimd() const;
277
278 void SetLocalUniformScaling(const plSimdFloat& fScaling, UpdateBehaviorIfStatic updateBehavior = UpdateBehaviorIfStatic::UpdateImmediately);
279 plSimdFloat GetLocalUniformScalingSimd() const;
280
281 plSimdTransform GetLocalTransformSimd() const;
282
283 void SetGlobalPosition(const plSimdVec4f& vPosition);
284 const plSimdVec4f& GetGlobalPositionSimd() const;
285
286 void SetGlobalRotation(const plSimdQuat& qRotation);
287 const plSimdQuat& GetGlobalRotationSimd() const;
288
289 void SetGlobalScaling(const plSimdVec4f& vScaling);
290 const plSimdVec4f& GetGlobalScalingSimd() const;
291
292 void SetGlobalTransform(const plSimdTransform& transform);
293 const plSimdTransform& GetGlobalTransformSimd() const;
294
295 const plSimdTransform& GetLastGlobalTransformSimd() const;
296
298 plVec3 GetGlobalDirForwards() const;
300 plVec3 GetGlobalDirRight() const;
302 plVec3 GetGlobalDirUp() const;
303
304#if PL_ENABLED(PL_GAMEOBJECT_VELOCITY)
308 void SetLastGlobalTransform(const plSimdTransform& transform);
309
311 plVec3 GetLinearVelocity() const;
312
314 plVec3 GetAngularVelocity() const;
315#endif
316
318 void UpdateGlobalTransform();
319
322 void EnableStaticTransformChangesNotifications();
323 void DisableStaticTransformChangesNotifications();
324
325
326 plBoundingBoxSphere GetLocalBounds() const;
327 plBoundingBoxSphere GetGlobalBounds() const;
328
329 const plSimdBBoxSphere& GetLocalBoundsSimd() const;
330 const plSimdBBoxSphere& GetGlobalBoundsSimd() const;
331
333 void UpdateLocalBounds();
334
337 void UpdateGlobalBounds();
338
340 void UpdateGlobalTransformAndBounds();
341
342
344 plSpatialDataHandle GetSpatialData() const;
345
347 void EnableComponentChangesNotifications();
348 void DisableComponentChangesNotifications();
349
351 template <typename T>
352 bool TryGetComponentOfBaseType(T*& out_pComponent);
353
355 template <typename T>
356 bool TryGetComponentOfBaseType(const T*& out_pComponent) const;
357
359 bool TryGetComponentOfBaseType(const plRTTI* pType, plComponent*& out_pComponent);
360
362 bool TryGetComponentOfBaseType(const plRTTI* pType, const plComponent*& out_pComponent) const;
363
365 template <typename T>
366 void TryGetComponentsOfBaseType(plDynamicArray<T*>& out_components);
367
369 template <typename T>
370 void TryGetComponentsOfBaseType(plDynamicArray<const T*>& out_components) const;
371
373 void TryGetComponentsOfBaseType(const plRTTI* pType, plDynamicArray<plComponent*>& out_components);
374
376 void TryGetComponentsOfBaseType(const plRTTI* pType, plDynamicArray<const plComponent*>& out_components) const;
377
379 plArrayPtr<plComponent* const> GetComponents();
380
382 plArrayPtr<const plComponent* const> GetComponents() const;
383
386 plUInt16 GetComponentVersion() const;
387
388
390 bool SendMessage(plMessage& ref_msg);
391
393 bool SendMessage(plMessage& ref_msg) const;
394
396 bool SendMessageRecursive(plMessage& ref_msg);
397
399 bool SendMessageRecursive(plMessage& ref_msg) const;
400
401
403 void PostMessage(const plMessage& msg, plTime delay, plObjectMsgQueueType::Enum queueType = plObjectMsgQueueType::NextFrame) const;
404
406 void PostMessageRecursive(const plMessage& msg, plTime delay, plObjectMsgQueueType::Enum queueType = plObjectMsgQueueType::NextFrame) const;
407
431 bool SendEventMessage(plMessage& ref_msg, const plComponent* pSenderComponent);
432
434 bool SendEventMessage(plMessage& ref_msg, const plComponent* pSenderComponent) const;
435
440 void PostEventMessage(plMessage& ref_msg, const plComponent* pSenderComponent, plTime delay, plObjectMsgQueueType::Enum queueType = plObjectMsgQueueType::NextFrame) const;
441
442
444 const plTagSet& GetTags() const;
445
447 void SetTags(const plTagSet& tags);
448
450 void SetTag(const plTag& tag);
451
453 void RemoveTag(const plTag& tag);
454
459 const plUInt16& GetTeamID() const { return m_uiTeamID; }
460
462 void SetTeamID(plUInt16 uiId);
463
475 plUInt32 GetStableRandomSeed() const;
476
482 void SetStableRandomSeed(plUInt32 uiSeed);
483
491 plVisibilityState GetVisibilityState(plUInt32 uiNumFramesBeforeInvisible = 5) const;
492
493private:
494 friend class plComponentManagerBase;
495 friend class plGameObjectTest;
496
497 // only needed until reflection can deal with plStringView
498 void SetNameInternal(const char* szName);
499 const char* GetNameInternal() const;
500 void SetGlobalKeyInternal(const char* szKey);
501 const char* GetGlobalKeyInternal() const;
502
503 bool SendMessageInternal(plMessage& msg, bool bWasPostedMsg);
504 bool SendMessageInternal(plMessage& msg, bool bWasPostedMsg) const;
505 bool SendMessageRecursiveInternal(plMessage& msg, bool bWasPostedMsg);
506 bool SendMessageRecursiveInternal(plMessage& msg, bool bWasPostedMsg) const;
507
508 PL_ALLOW_PRIVATE_PROPERTIES(plGameObject);
509
510 // Add / Detach child used by the reflected property keep their local transform as
511 // updating that is handled by the editor.
512 void Reflection_AddChild(plGameObject* pChild);
513 void Reflection_DetachChild(plGameObject* pChild);
514 plHybridArray<plGameObject*, 8> Reflection_GetChildren() const;
515 void Reflection_AddComponent(plComponent* pComponent);
516 void Reflection_RemoveComponent(plComponent* pComponent);
517 plHybridArray<plComponent*, NUM_INPLACE_COMPONENTS> Reflection_GetComponents() const;
518
519 plObjectMode::Enum Reflection_GetMode() const;
520 void Reflection_SetMode(plObjectMode::Enum mode);
521
522 plGameObject* Reflection_GetParent() const;
523 void Reflection_SetGlobalPosition(const plVec3& vPosition);
524 void Reflection_SetGlobalRotation(const plQuat& qRotation);
525 void Reflection_SetGlobalScaling(const plVec3& vScaling);
526 void Reflection_SetGlobalTransform(const plTransform& transform);
527
528 bool DetermineDynamicMode(plComponent* pComponentToIgnore = nullptr) const;
529 void ConditionalMakeStatic(plComponent* pComponentToIgnore = nullptr);
530 void MakeStaticInternal();
531
532 void UpdateGlobalTransformAndBoundsRecursive();
533 void UpdateLastGlobalTransform();
534
535 void OnMsgDeleteGameObject(plMsgDeleteGameObject& msg);
536
537 void AddComponent(plComponent* pComponent);
538 void RemoveComponent(plComponent* pComponent);
539 void FixComponentPointer(plComponent* pOldPtr, plComponent* pNewPtr);
540
541 // Updates the active state of this object and all children and attached components recursively, depending on the enabled states.
542 void UpdateActiveState(bool bParentActive);
543
544 void SendNotificationMessage(plMessage& msg);
545
546 struct PL_CORE_DLL alignas(16) TransformationData
547 {
548 PL_DECLARE_POD_TYPE();
549
550 plGameObject* m_pObject;
551 TransformationData* m_pParentData;
552
553#if PL_ENABLED(PL_PLATFORM_32BIT)
554 plUInt64 m_uiPadding;
555#endif
556
557 plSimdVec4f m_localPosition;
558 plSimdQuat m_localRotation;
559 plSimdVec4f m_localScaling; // x,y,z = non-uniform scaling, w = uniform scaling
560
561 plSimdTransform m_globalTransform;
562
563#if PL_ENABLED(PL_GAMEOBJECT_VELOCITY)
564 plSimdTransform m_lastGlobalTransform;
565#endif
566
567 plSimdBBoxSphere m_localBounds; // m_BoxHalfExtents.w != 0 indicates that the object should be always visible
568 plSimdBBoxSphere m_globalBounds;
569
570 plSpatialDataHandle m_hSpatialData;
571 plUInt32 m_uiSpatialDataCategoryBitmask;
572
573 plUInt32 m_uiStableRandomSeed = 0;
574
575#if PL_ENABLED(PL_GAMEOBJECT_VELOCITY)
576 plUInt32 m_uiLastGlobalTransformUpdateCounter = 0;
577#else
578 plUInt32 m_uiPadding2[1];
579#endif
580
582 void UpdateLocalTransform();
583
586 void UpdateGlobalTransformRecursive(plUInt32 uiUpdateCounter);
587
590 void UpdateGlobalTransformNonRecursive(plUInt32 uiUpdateCounter);
591
594 void UpdateGlobalTransformWithoutParent(plUInt32 uiUpdateCounter);
595
598 void UpdateGlobalTransformWithParent(plUInt32 uiUpdateCounter);
599
600 void UpdateGlobalBounds(plSpatialSystem* pSpatialSystem);
601 void UpdateGlobalBounds();
602 void UpdateGlobalBoundsAndSpatialData(plSpatialSystem& ref_spatialSystem);
603
604 void UpdateLastGlobalTransform(plUInt32 uiUpdateCounter);
605
606 void RecreateSpatialData(plSpatialSystem& ref_spatialSystem);
607 };
608
609 plGameObjectId m_InternalId;
610 plHashedString m_sName;
611
612#if PL_ENABLED(PL_PLATFORM_32BIT)
613 plUInt32 m_uiNamePadding;
614#endif
615
617
618 plUInt32 m_uiParentIndex = 0;
619 plUInt32 m_uiFirstChildIndex = 0;
620 plUInt32 m_uiLastChildIndex = 0;
621
622 plUInt32 m_uiNextSiblingIndex = 0;
623 plUInt32 m_uiPrevSiblingIndex = 0;
624 plUInt32 m_uiChildCount = 0;
625
626 plUInt16 m_uiHierarchyLevel = 0;
627
629 plUInt16 m_uiTeamID = 0;
630
631 TransformationData* m_pTransformationData = nullptr;
632
633#if PL_ENABLED(PL_PLATFORM_32BIT)
634 plUInt32 m_uiPadding = 0;
635#endif
636
638
639 struct ComponentUserData
640 {
641 plUInt16 m_uiVersion;
642 plUInt16 m_uiUnused;
643 };
644
645 plTagSet m_Tags;
646};
647
648PL_DECLARE_REFLECTABLE_TYPE(PL_CORE_DLL, plGameObject);
649
650#include <Core/World/Implementation/GameObject_inl.h>
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Base class of all component types.
Definition Component.h:25
Base class for all component managers. Do not derive directly from this class, but derive from plComp...
Definition ComponentManager.h:21
Definition DynamicArray.h:81
Definition GameObject.h:82
Iterates over all children of one object.
Definition GameObject.h:56
This class represents an object inside the world.
Definition GameObject.h:32
bool TryGetComponentOfBaseType(T *&out_pComponent)
Tries to find a component of the given base type in the objects components list and returns the first...
UpdateBehaviorIfStatic
Defines update behavior for global transforms when changing the local transform on a static game obje...
Definition GameObject.h:230
const plUInt16 & GetTeamID() const
Returns the 'team ID' that was given during creation (/see plGameObjectDesc)
Definition GameObject.h:459
bool WasCreatedByPrefab() const
Checks whether the plObjectFlags::CreatedByPrefab flag is set on this object.
Definition GameObject.h:142
bool TryGetComponentOfBaseType(const T *&out_pComponent) const
Tries to find a component of the given base type in the objects components list and returns the first...
TransformPreservation
Defines during re-parenting what transform is going to be preserved.
Definition GameObject.h:165
void SetCreatedByPrefab()
Adds plObjectFlags::CreatedByPrefab to the object. See the flag for details.
Definition GameObject.h:139
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition WorldData.h:18
This class provides functions to work on raw memory.
Definition MemoryUtils.h:26
Base class for all message types. Each message type has it's own id which is used to dispatch message...
Definition Message.h:22
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
Definition SimdBBoxSphere.h:6
Definition SimdFloat.h:7
Definition SimdQuat.h:6
Definition SimdTransform.h:6
A 4-component SIMD vector class.
Definition SimdVec4f.h:8
Implementation of a dynamically growing array with in-place storage and small memory overhead.
Definition SmallArray.h:17
Definition Declarations.h:353
Definition SpatialSystem.h:10
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
The tag class stores the necessary lookup information for a single tag which can be used in conjuncti...
Definition Tag.h:16
A dynamic collection of tags featuring fast lookups.
Definition TagSet.h:23
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
A handle to a game object.
Definition Declarations.h:76
Internal game object id used by plGameObjectHandle.
Definition Declarations.h:43
Definition DeleteObjectMessage.h:7
@ CreatedByPrefab
Such flagged objects and components are ignored during scene export (see plWorldWriter) and will be r...
Definition Declarations.h:191
Enum
Definition Declarations.h:274
@ NextFrame
Process the message in the PreAsync phase of the next frame.
Definition Declarations.h:277
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12