Plasma Engine  2.0
Loading...
Searching...
No Matches
World.h
1#pragma once
2
3#include <Core/World/Implementation/WorldData.h>
4
5struct plEventMessage;
7
21class PL_CORE_DLL plWorld final
22{
23public:
25 plWorld(plWorldDesc& ref_desc);
26 ~plWorld();
27
29 void Clear();
30
32 plStringView GetName() const;
33
35 plUInt32 GetIndex() const;
36
39
41 plGameObjectHandle CreateObject(const plGameObjectDesc& desc);
42
44 plGameObjectHandle CreateObject(const plGameObjectDesc& desc, plGameObject*& out_pObject);
45
52 void DeleteObjectNow(const plGameObjectHandle& hObject, bool bAlsoDeleteEmptyParents = true);
53
58 void DeleteObjectDelayed(const plGameObjectHandle& hObject, bool bAlsoDeleteEmptyParents = true);
59
62 const plEvent<const plGameObject*>& GetObjectDeletionEvent() const;
63
65 bool IsValidObject(const plGameObjectHandle& hObject) const;
66
68 [[nodiscard]] bool TryGetObject(const plGameObjectHandle& hObject, plGameObject*& out_pObject);
69
71 [[nodiscard]] bool TryGetObject(const plGameObjectHandle& hObject, const plGameObject*& out_pObject) const;
72
74 [[nodiscard]] bool TryGetObjectWithGlobalKey(const plTempHashedString& sGlobalKey, plGameObject*& out_pObject);
75
77 [[nodiscard]] bool TryGetObjectWithGlobalKey(const plTempHashedString& sGlobalKey, const plGameObject*& out_pObject) const;
78
79
81 plUInt32 GetObjectCount() const;
82
85
88
93
94 enum TraversalMethod
95 {
96 BreadthFirst,
97 DepthFirst
98 };
99
102 void Traverse(VisitorFunc visitorFunc, TraversalMethod method = DepthFirst);
103
107
109 template <typename ModuleType>
110 ModuleType* GetOrCreateModule();
111
113 plWorldModule* GetOrCreateModule(const plRTTI* pRtti);
114
116 template <typename ModuleType>
118
120 void DeleteModule(const plRTTI* pRtti);
121
123 template <typename ModuleType>
124 ModuleType* GetModule();
125
127 template <typename ModuleType>
128 const ModuleType* GetModule() const;
129
131 template <typename ModuleType>
132 const ModuleType* GetModuleReadOnly() const;
133
135 plWorldModule* GetModule(const plRTTI* pRtti);
136
138 const plWorldModule* GetModule(const plRTTI* pRtti) const;
139
143
145 template <typename ManagerType>
146 ManagerType* GetOrCreateComponentManager();
147
149 plComponentManagerBase* GetOrCreateManagerForComponentType(const plRTTI* pComponentRtti);
150
152 template <typename ManagerType>
153 void DeleteComponentManager();
154
156 template <typename ManagerType>
157 ManagerType* GetComponentManager();
158
160 template <typename ManagerType>
161 const ManagerType* GetComponentManager() const;
162
164 plComponentManagerBase* GetManagerForComponentType(const plRTTI* pComponentRtti);
165
167 const plComponentManagerBase* GetManagerForComponentType(const plRTTI* pComponentRtti) const;
168
170 bool IsValidComponent(const plComponentHandle& hComponent) const;
171
173 template <typename ComponentType>
174 [[nodiscard]] bool TryGetComponent(const plComponentHandle& hComponent, ComponentType*& out_pComponent);
175
177 template <typename ComponentType>
178 [[nodiscard]] bool TryGetComponent(const plComponentHandle& hComponent, const ComponentType*& out_pComponent) const;
179
184 plComponentInitBatchHandle CreateComponentInitBatch(plStringView sBatchName, bool bMustFinishWithinOneFrame = true);
185
187 void DeleteComponentInitBatch(const plComponentInitBatchHandle& hBatch);
188
191 void BeginAddingComponentsToInitBatch(const plComponentInitBatchHandle& hBatch);
192
194 void EndAddingComponentsToInitBatch(const plComponentInitBatchHandle& hBatch);
195
197 void SubmitComponentInitBatch(const plComponentInitBatchHandle& hBatch);
198
201 bool IsComponentInitBatchCompleted(const plComponentInitBatchHandle& hBatch, double* pCompletionFactor = nullptr);
202
205 void CancelComponentInitBatch(const plComponentInitBatchHandle& hBatch);
206
210
212 void SendMessage(const plGameObjectHandle& hReceiverObject, plMessage& ref_msg);
213
215 void SendMessageRecursive(const plGameObjectHandle& hReceiverObject, plMessage& ref_msg);
216
218 void PostMessage(const plGameObjectHandle& hReceiverObject, const plMessage& msg, plTime delay,
220
223 void PostMessageRecursive(const plGameObjectHandle& hReceiverObject, const plMessage& msg, plTime delay,
225
227 void SendMessage(const plComponentHandle& hReceiverComponent, plMessage& ref_msg);
228
230 void PostMessage(const plComponentHandle& hReceiverComponent, const plMessage& msg, plTime delay,
232
238 void FindEventMsgHandlers(const plMessage& msg, plGameObject* pSearchObject, plDynamicArray<plComponent*>& out_components);
239
241 void FindEventMsgHandlers(const plMessage& msg, const plGameObject* pSearchObject, plDynamicArray<const plComponent*>& out_components) const;
242
244
246 void SetWorldSimulationEnabled(bool bEnable);
247
249 bool GetWorldSimulationEnabled() const;
250
253 void Update();
254
256 const plSharedPtr<plTask>& GetUpdateTask();
257
259 plUInt32 GetUpdateCounter() const;
260
262 plSpatialSystem* GetSpatialSystem();
263
265 const plSpatialSystem* GetSpatialSystem() const;
266
267
271 void GetCoordinateSystem(const plVec3& vGlobalPosition, plCoordinateSystem& out_coordinateSystem) const;
272
274 void SetCoordinateSystemProvider(const plSharedPtr<plCoordinateSystemProvider>& pProvider);
275
277 plCoordinateSystemProvider& GetCoordinateSystemProvider();
278
280 const plCoordinateSystemProvider& GetCoordinateSystemProvider() const;
281
282
284 plClock& GetClock();
285
287 const plClock& GetClock() const;
288
291 plRandom& GetRandomNumberGenerator();
292
293
295 plAllocator* GetAllocator();
296
298 plInternal::WorldLargeBlockAllocator* GetBlockAllocator();
299
301 plDoubleBufferedLinearAllocator* GetStackAllocator();
302
305 plInternal::WorldData::ReadMarker& GetReadMarker() const;
306
308 plInternal::WorldData::WriteMarker& GetWriteMarker();
309
311 void SetMaxInitializationTimePerFrame(plTime maxInitTime);
312
314 void SetUserData(void* pUserData);
315
317 void* GetUserData() const;
318
319 using ReferenceResolver = plDelegate<plGameObjectHandle(const void*, plComponentHandle hThis, plStringView sProperty)>;
320
324 void SetGameObjectReferenceResolver(const ReferenceResolver& resolver);
325
327 const ReferenceResolver& GetGameObjectReferenceResolver() const;
328
329 using ResourceReloadContext = plInternal::WorldData::ResourceReloadContext;
331
333 void AddResourceReloadFunction(plTypelessResourceHandle hResource, plComponentHandle hComponent, void* pUserData, ResourceReloadFunc function);
334 void RemoveResourceReloadFunction(plTypelessResourceHandle hResource, plComponentHandle hComponent, void* pUserData);
335
338 static constexpr plUInt64 GetMaxNumGameObjects();
339 static constexpr plUInt64 GetMaxNumHierarchyLevels();
340 static constexpr plUInt64 GetMaxNumComponentsPerType();
341 static constexpr plUInt64 GetMaxNumWorldModules();
342 static constexpr plUInt64 GetMaxNumComponentTypes();
343 static constexpr plUInt64 GetMaxNumWorlds();
345
346public:
348 static plUInt32 GetWorldCount();
349
351 static plWorld* GetWorld(plUInt32 uiIndex);
352
354 static plWorld* GetWorld(const plGameObjectHandle& hObject);
355
357 static plWorld* GetWorld(const plComponentHandle& hComponent);
358
359private:
360 friend class plGameObject;
361 friend class plWorldModule;
362 friend class plComponentManagerBase;
363 friend class plComponent;
364 PL_ALLOW_PRIVATE_PROPERTIES(plWorld);
365
366 plGameObject* Reflection_TryGetObjectWithGlobalKey(plTempHashedString sGlobalKey);
367 plClock* Reflection_GetClock();
368
369 void CheckForReadAccess() const;
370 void CheckForWriteAccess() const;
371
372 plGameObject* GetObjectUnchecked(plUInt32 uiIndex) const;
373
374 void SetParent(plGameObject* pObject, plGameObject* pNewParent,
375 plGameObject::TransformPreservation preserve = plGameObject::TransformPreservation::PreserveGlobal);
376 void LinkToParent(plGameObject* pObject);
377 void UnlinkFromParent(plGameObject* pObject);
378
379 void SetObjectGlobalKey(plGameObject* pObject, const plHashedString& sGlobalKey);
380 plStringView GetObjectGlobalKey(const plGameObject* pObject) const;
381
382 void PostMessage(const plGameObjectHandle& receiverObject, const plMessage& msg, plObjectMsgQueueType::Enum queueType, plTime delay, bool bRecursive) const;
383 void ProcessQueuedMessage(const plInternal::WorldData::MessageQueue::Entry& entry);
384 void ProcessQueuedMessages(plObjectMsgQueueType::Enum queueType);
385
386 template <typename World, typename GameObject, typename Component>
387 static void FindEventMsgHandlers(World& world, const plMessage& msg, GameObject pSearchObject, plDynamicArray<Component>& out_components);
388
389 void RegisterUpdateFunction(const plWorldModule::UpdateFunctionDesc& desc);
390 void DeregisterUpdateFunction(const plWorldModule::UpdateFunctionDesc& desc);
391 void DeregisterUpdateFunctions(plWorldModule* pModule);
392
394 void AddComponentToInitialize(plComponentHandle hComponent);
395
396 void UpdateFromThread();
397 void UpdateSynchronous(const plArrayPtr<plInternal::WorldData::RegisteredUpdateFunction>& updateFunctions);
398 void UpdateAsynchronous();
399
400 // returns if the batch was completely initialized
401 bool ProcessInitializationBatch(plInternal::WorldData::InitBatch& batch, plTime endTime);
402 void ProcessComponentsToInitialize();
403 void ProcessUpdateFunctionsToRegister();
404 plResult RegisterUpdateFunctionInternal(const plWorldModule::UpdateFunctionDesc& desc);
405
406 void DeleteDeadObjects();
407 void DeleteDeadComponents();
408
409 void PatchHierarchyData(plGameObject* pObject, plGameObject::TransformPreservation preserve);
410 void RecreateHierarchyData(plGameObject* pObject, bool bWasDynamic);
411
412 void ProcessResourceReloadFunctions();
413
414 bool ReportErrorWhenStaticObjectMoves() const;
415
416 float GetInvDeltaSeconds() const;
417
418 plSharedPtr<plTask> m_pUpdateTask;
419
421
422 using QueuedMsgMetaData = plInternal::WorldData::QueuedMsgMetaData;
423
424 plUInt32 m_uiIndex;
426};
427
428PL_DECLARE_REFLECTABLE_TYPE(PL_CORE_DLL, plWorld);
429
430#include <Core/World/Implementation/World_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
A clock that can be speed up, slowed down, paused, etc. Useful for updating game logic,...
Definition Clock.h:15
Base class of all component types.
Definition Component.h:25
Definition Declarations.h:363
Base class for all component managers. Do not derive directly from this class, but derive from plComp...
Definition ComponentManager.h:21
Definition CoordinateSystem.h:18
A double buffered stack allocator.
Definition FrameAllocator.h:7
Definition DynamicArray.h:81
Definition Event.h:177
Base class for components that want to handle 'event messages'.
Definition EventMessageHandlerComponent.h:14
This class represents an object inside the world.
Definition GameObject.h:32
TransformPreservation
Defines during re-parenting what transform is going to be preserved.
Definition GameObject.h:165
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
Definition WorldData.h:75
Definition WorldData.h:273
Definition WorldData.h:286
Definition WorldData.h:18
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
A random number generator. Currently uses the WELL512 algorithm.
Definition Random.h:9
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
Definition SpatialSystem.h:10
Wraps a C-style array, which has a fixed size at compile-time, with a more convenient interface.
Definition StaticArray.h:13
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
The typeless implementation of resource handles. A typed interface is provided by plTypedResourceHand...
Definition ResourceHandle.h:32
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
const ManagerType * GetComponentManager() const
Returns the instance to the given component manager type.
ModuleType * GetModule()
Returns the instance to the given module type or derived types.
const ModuleType * GetModuleReadOnly() const
Returns the instance to the given module type or derived types.
void DeleteModule()
Deletes the module of the given type or derived types.
ModuleType * GetOrCreateModule()
Creates an instance of the given module type or derived type or returns a pointer to an already exist...
const ModuleType * GetModule() const
Returns the instance to the given module type or derived types.
ManagerType * GetComponentManager()
Returns the instance to the given component manager type.
Definition WorldModule.h:10
A handle to a component.
Definition Declarations.h:138
Definition CoordinateSystem.h:9
Base class for all messages that are sent as 'events'.
Definition EventMessage.h:8
Describes the initial state of a game object.
Definition GameObjectDesc.h:11
A handle to a game object.
Definition Declarations.h:76
Enum
Definition Declarations.h:274
@ NextFrame
Process the message in the PreAsync phase of the next frame.
Definition Declarations.h:277
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
Describes the initial state of a world.
Definition WorldDesc.h:14
Description of an update function that can be registered at the world.
Definition WorldModule.h:43