20 return m_Data.m_ObjectDeletionEvent;
26 PL_ASSERT_DEV(hObject.IsInvalidated() || hObject.m_InternalId.m_WorldIndex == m_uiIndex,
27 "Object does not belong to this world. Expected world id {0} got id {1}", m_uiIndex, hObject.m_InternalId.m_WorldIndex);
29 return m_Data.m_Objects.
Contains(hObject);
35 PL_ASSERT_DEV(hObject.IsInvalidated() || hObject.m_InternalId.m_WorldIndex == m_uiIndex,
36 "Object does not belong to this world. Expected world id {0} got id {1}", m_uiIndex, hObject.m_InternalId.m_WorldIndex);
38 return m_Data.m_Objects.
TryGetValue(hObject, out_pObject);
44 PL_ASSERT_DEV(hObject.IsInvalidated() || hObject.m_InternalId.m_WorldIndex == m_uiIndex,
45 "Object does not belong to this world. Expected world id {0} got id {1}", m_uiIndex, hObject.m_InternalId.m_WorldIndex);
48 bool bResult = m_Data.m_Objects.
TryGetValue(hObject, pObject);
49 out_pObject = pObject;
59 out_pObject = m_Data.m_Objects[id];
72 out_pObject = m_Data.m_Objects[id];
83 return static_cast<plUInt32
>(m_Data.m_Objects.
GetCount() - 1);
88 CheckForWriteAccess();
100 CheckForWriteAccess();
102 if (method == DepthFirst)
104 m_Data.TraverseDepthFirst(visitorFunc);
108 m_Data.TraverseBreadthFirst(visitorFunc);
112template <
typename ModuleType>
115 static_assert(PL_IS_DERIVED_FROM_STATIC(
plWorldModule, ModuleType),
"Not a valid module type");
117 return plStaticCast<ModuleType*>(
GetOrCreateModule(plGetStaticRTTI<ModuleType>()));
120template <
typename ModuleType>
123 static_assert(PL_IS_DERIVED_FROM_STATIC(
plWorldModule, ModuleType),
"Not a valid module type");
128template <
typename ModuleType>
131 static_assert(PL_IS_DERIVED_FROM_STATIC(
plWorldModule, ModuleType),
"Not a valid module type");
133 return plStaticCast<ModuleType*>(
GetModule(plGetStaticRTTI<ModuleType>()));
136template <
typename ModuleType>
139 static_assert(PL_IS_DERIVED_FROM_STATIC(
plWorldModule, ModuleType),
"Not a valid module type");
141 return plStaticCast<const ModuleType*>(
GetModule(plGetStaticRTTI<ModuleType>()));
144template <
typename ModuleType>
150template <
typename ManagerType>
153 static_assert(PL_IS_DERIVED_FROM_STATIC(
plComponentManagerBase, ManagerType),
"Not a valid component manager type");
155 CheckForWriteAccess();
157 const plWorldModuleTypeId uiTypeId = ManagerType::TypeId();
160 ManagerType* pModule =
static_cast<ManagerType*
>(m_Data.m_Modules[uiTypeId]);
161 if (pModule ==
nullptr)
163 pModule = PL_NEW(&m_Data.m_Allocator, ManagerType,
this);
166 m_Data.m_Modules[uiTypeId] = pModule;
167 m_Data.m_ModulesToStartSimulation.
PushBack(pModule);
180template <
typename ManagerType>
183 static_assert(PL_IS_DERIVED_FROM_STATIC(
plComponentManagerBase, ManagerType),
"Not a valid component manager type");
185 CheckForWriteAccess();
187 const plWorldModuleTypeId uiTypeId = ManagerType::TypeId();
188 if (uiTypeId < m_Data.m_Modules.
GetCount())
190 if (ManagerType* pModule =
static_cast<ManagerType*
>(m_Data.m_Modules[uiTypeId]))
192 m_Data.m_Modules[uiTypeId] =
nullptr;
195 DeregisterUpdateFunctions(pModule);
196 PL_DELETE(&m_Data.m_Allocator, pModule);
201template <
typename ManagerType>
204 static_assert(PL_IS_DERIVED_FROM_STATIC(
plComponentManagerBase, ManagerType),
"Not a valid component manager type");
206 CheckForWriteAccess();
208 const plWorldModuleTypeId uiTypeId = ManagerType::TypeId();
209 if (uiTypeId < m_Data.m_Modules.
GetCount())
211 return plStaticCast<ManagerType*>(m_Data.m_Modules[uiTypeId]);
217template <
typename ManagerType>
220 static_assert(PL_IS_DERIVED_FROM_STATIC(
plComponentManagerBase, ManagerType),
"Not a valid component manager type");
222 CheckForReadAccess();
224 const plWorldModuleTypeId uiTypeId = ManagerType::TypeId();
225 if (uiTypeId < m_Data.m_Modules.
GetCount())
227 return plStaticCast<const ManagerType*>(m_Data.m_Modules[uiTypeId]);
237 return plStaticCast<plComponentManagerBase*>(
GetModule(pComponentRtti));
244 return plStaticCast<const plComponentManagerBase*>(
GetModule(pComponentRtti));
249 CheckForReadAccess();
250 const plWorldModuleTypeId uiTypeId = hComponent.m_InternalId.m_TypeId;
252 if (uiTypeId < m_Data.m_Modules.
GetCount())
254 if (
const plWorldModule* pModule = m_Data.m_Modules[uiTypeId])
263template <
typename ComponentType>
266 CheckForWriteAccess();
267 static_assert(PL_IS_DERIVED_FROM_STATIC(
plComponent, ComponentType),
"Not a valid component type");
269 const plWorldModuleTypeId uiTypeId = hComponent.m_InternalId.m_TypeId;
271 if (uiTypeId < m_Data.m_Modules.
GetCount())
277 out_pComponent = plDynamicCast<ComponentType*>(pComponent);
278 return bResult && out_pComponent !=
nullptr;
285template <
typename ComponentType>
288 CheckForReadAccess();
289 static_assert(PL_IS_DERIVED_FROM_STATIC(
plComponent, ComponentType),
"Not a valid component type");
291 const plWorldModuleTypeId uiTypeId = hComponent.m_InternalId.m_TypeId;
293 if (uiTypeId < m_Data.m_Modules.
GetCount())
295 if (
const plWorldModule* pModule = m_Data.m_Modules[uiTypeId])
299 out_pComponent = plDynamicCast<const ComponentType*>(pComponent);
300 return bResult && out_pComponent !=
nullptr;
309 CheckForWriteAccess();
318#if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
319 if (ref_msg.GetDebugMessageRouting())
321 plLog::Warning(
"plWorld::SendMessage: The receiver plGameObject for message of type '{0}' does not exist.", ref_msg.
GetId());
329 CheckForWriteAccess();
338#if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
339 if (ref_msg.GetDebugMessageRouting())
341 plLog::Warning(
"plWorld::SendMessageRecursive: The receiver plGameObject for message of type '{0}' does not exist.", ref_msg.
GetId());
351 PostMessage(hReceiverObject, msg, queueType, delay,
false);
358 PostMessage(hReceiverObject, msg, queueType, delay,
true);
363 CheckForWriteAccess();
372#if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
373 if (ref_msg.GetDebugMessageRouting())
375 plLog::Warning(
"plWorld::SendMessage: The receiver plComponent for message of type '{0}' does not exist.", ref_msg.
GetId());
383 m_Data.m_bSimulateWorld = bEnable;
388 return m_Data.m_bSimulateWorld;
393 return m_pUpdateTask;
398 return m_Data.m_uiUpdateCounter;
403 CheckForWriteAccess();
405 return m_Data.m_pSpatialSystem.
Borrow();
410 CheckForReadAccess();
412 return m_Data.m_pSpatialSystem.
Borrow();
417 m_Data.m_pCoordinateSystemProvider->GetCoordinateSystem(vGlobalPosition, out_coordinateSystem);
422 return *(m_Data.m_pCoordinateSystemProvider.
Borrow());
427 return *(m_Data.m_pCoordinateSystemProvider.
Borrow());
432 return m_Data.m_Clock;
437 return m_Data.m_Clock;
442 return m_Data.m_Random;
447 return &m_Data.m_Allocator;
452 return &m_Data.m_BlockAllocator;
457 return &m_Data.m_StackAllocator;
462 return m_Data.m_ReadMarker;
467 return m_Data.m_WriteMarker;
472 CheckForWriteAccess();
474 m_Data.m_pUserData = pUserData;
479 CheckForReadAccess();
481 return m_Data.m_pUserData;
484constexpr plUInt64 plWorld::GetMaxNumGameObjects()
486 return plGameObjectId::MAX_INSTANCES - 2;
489constexpr plUInt64 plWorld::GetMaxNumHierarchyLevels()
491 return 1 << (
sizeof(plGameObject::m_uiHierarchyLevel) * 8);
494constexpr plUInt64 plWorld::GetMaxNumComponentsPerType()
496 return plComponentId::MAX_INSTANCES - 1;
499constexpr plUInt64 plWorld::GetMaxNumWorldModules()
501 return PL_MAX_WORLD_MODULE_TYPES;
504constexpr plUInt64 plWorld::GetMaxNumComponentTypes()
506 return PL_MAX_COMPONENT_TYPES;
509constexpr plUInt64 plWorld::GetMaxNumWorlds()
511 return PL_MAX_WORLDS;
523 return s_Worlds[uiIndex];
529 return s_Worlds[hObject.GetInternalID().m_WorldIndex];
535 return s_Worlds[hComponent.GetInternalID().m_WorldIndex];
538PL_ALWAYS_INLINE
void plWorld::CheckForReadAccess()
const
540 PL_ASSERT_DEV(m_Data.m_iReadCounter > 0,
"Trying to read from World '{0}', but it is not marked for reading.",
GetName());
543PL_ALWAYS_INLINE
void plWorld::CheckForWriteAccess()
const
549PL_ALWAYS_INLINE
plGameObject* plWorld::GetObjectUnchecked(plUInt32 uiIndex)
const
554PL_ALWAYS_INLINE
bool plWorld::ReportErrorWhenStaticObjectMoves()
const
556 return m_Data.m_bReportErrorWhenStaticObjectMoves;
559PL_ALWAYS_INLINE
float plWorld::GetInvDeltaSeconds()
const
564 return 1.0f / fDelta;
Base class for all memory allocators.
Definition Allocator.h:23
void PushBack(const T &value)
Pushes value at the end of the array.
Definition ArrayBase_inl.h:333
void EnsureCount(plUInt32 uiCount)
Ensures the container has at least uiCount elements. Ie. calls SetCount() if the container has fewer ...
Definition ArrayBase_inl.h:144
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition ArrayBase_inl.h:172
A clock that can be speed up, slowed down, paused, etc. Useful for updating game logic,...
Definition Clock.h:15
plTime GetTimeDiff() const
Returns the time difference between the last two calls to Update().
Definition Clock_inl.h:52
Base class of all component types.
Definition Component.h:25
PL_ALWAYS_INLINE bool SendMessage(plMessage &ref_msg)
Sends a message to this component.
Definition Component.h:117
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
This class represents an object inside the world.
Definition GameObject.h:32
bool SendMessage(plMessage &ref_msg)
Sends a message to all components of this object.
Definition GameObject_inl.h:545
bool SendMessageRecursive(plMessage &ref_msg)
Sends a message to all components of this object and then recursively to all children.
Definition GameObject_inl.h:555
bool TryGetValue(const CompatibleKeyType &key, ValueType &out_value) const
Returns whether an entry with the given key was found and if found writes out the corresponding value...
IndexType GetCount() const
Returns the number of active entries in the table.
Definition IdTable_inl.h:168
bool TryGetValue(const IdType id, ValueType &out_value) const
Returns if an entry with the given id was found and if found writes out the corresponding value to ou...
Definition IdTable_inl.h:267
const ValueType & GetValueUnchecked(const IndexType index) const
Returns the value at the given index. Does bounds checks in debug builds but does not check for stale...
Definition IdTable_inl.h:314
bool Contains(const IdType id) const
Returns if the table contains an entry corresponding to the given id.
Definition IdTable_inl.h:328
Definition WorldData.h:50
Definition WorldData.h:75
Definition WorldData.h:273
Definition WorldData.h:286
static void Warning(plLogInterface *pInterface, const plFormatString &string)
A potential problem or a performance warning. Might be possible to ignore it.
Definition Log.cpp:391
Base class for all message types. Each message type has it's own id which is used to dispatch message...
Definition Message.h:22
PL_ALWAYS_INLINE plMessageId GetId() const
Returns the id for this message type.
Definition Message.h:48
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
PL_ALWAYS_INLINE plStringView GetTypeName() const
Returns the name of this type.
Definition RTTI.h:48
PL_ALWAYS_INLINE bool IsDerivedFrom(const plRTTI *pBaseType) const
Returns true if this type is derived from the given type (or of the same type).
Definition RTTI.h:60
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
T * Borrow() const
Borrows the managed object. The shared ptr stays unmodified.
Definition SharedPtr_inl.h:168
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
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
plUInt64 GetHash() const
Returns the hash of the stored string.
Definition HashedString_inl.h:204
static plThreadID GetCurrentThreadID()
Returns an identifier for the currently running thread.
Definition ThreadUtils_Posix.h:42
T * Borrow() const
Borrows the managed object. The unique ptr stays unmodified.
Definition UniquePtr_inl.h:102
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
plGameObjectHandle CreateObject(const plGameObjectDesc &desc)
Create a new game object from the given description and returns a handle to it.
Definition World_inl.h:12
plUInt32 GetIndex() const
Returns the index of this world.
Definition World_inl.h:7
plClock & GetClock()
Returns the clock that is used for all updates in this game world.
Definition World_inl.h:430
void DeleteComponentManager()
Deletes the component manager of the given type and all its components.
Definition World_inl.h:181
ModuleType * GetModule()
Returns the instance to the given module type or derived types.
const plEvent< const plGameObject * > & GetObjectDeletionEvent() const
Returns the event that is triggered before an object is deleted. This can be used for external system...
Definition World_inl.h:18
const ModuleType * GetModuleReadOnly() const
Returns the instance to the given module type or derived types.
void Traverse(VisitorFunc visitorFunc, TraversalMethod method=DepthFirst)
Traverses the game object tree starting at the top level objects and then recursively all children....
Definition World_inl.h:98
plComponentManagerBase * GetOrCreateManagerForComponentType(const plRTTI *pComponentRtti)
Returns the component manager that handles the given rtti component type.
Definition World_inl.h:173
bool TryGetObjectWithGlobalKey(const plTempHashedString &sGlobalKey, plGameObject *&out_pObject)
Returns whether an object with the given global key exists and if so writes out the corresponding poi...
Definition World_inl.h:53
static plWorld * GetWorld(plUInt32 uiIndex)
Returns the world with the given index.
Definition World_inl.h:521
plUInt32 GetUpdateCounter() const
Returns the number of update calls. Can be used to determine whether an operation has already been do...
Definition World_inl.h:396
ManagerType * GetOrCreateComponentManager()
Creates an instance of the given component manager type or returns a pointer to an already existing i...
Definition World_inl.h:151
plInternal::WorldData::WriteMarker & GetWriteMarker()
Mark the world for writing by using PL_LOCK(world.GetWriteMarker()). Only one thread can write at a t...
Definition World_inl.h:465
void GetCoordinateSystem(const plVec3 &vGlobalPosition, plCoordinateSystem &out_coordinateSystem) const
Returns the coordinate system for the given position. By default this always returns a coordinate sys...
Definition World_inl.h:415
void DeleteModule()
Deletes the module of the given type or derived types.
bool IsValidObject(const plGameObjectHandle &hObject) const
Returns whether the given handle corresponds to a valid object.
Definition World_inl.h:23
plRandom & GetRandomNumberGenerator()
Accesses the default random number generator. If more control is desired, individual components shoul...
Definition World_inl.h:440
void SetWorldSimulationEnabled(bool bEnable)
If enabled, the full simulation should be executed, otherwise only the rendering related updates shou...
Definition World_inl.h:381
plSpatialSystem * GetSpatialSystem()
Returns the spatial system that is associated with this world.
Definition World_inl.h:401
plInternal::WorldData::ObjectIterator GetObjects()
Returns an iterator over all objects in this world in no specific order.
Definition World_inl.h:86
plInternal::WorldLargeBlockAllocator * GetBlockAllocator()
Returns the block allocator used by this world.
Definition World_inl.h:450
plInternal::WorldData::ReadMarker & GetReadMarker() const
Mark the world for reading by using PL_LOCK(world.GetReadMarker()). Multiple threads can read simulta...
Definition World_inl.h:460
ModuleType * GetOrCreateModule()
Creates an instance of the given module type or derived type or returns a pointer to an already exist...
plAllocator * GetAllocator()
Returns the allocator used by this world.
Definition World_inl.h:445
static plUInt32 GetWorldCount()
Returns the number of active worlds.
Definition World_inl.h:515
plCoordinateSystemProvider & GetCoordinateSystemProvider()
Returns the coordinate system provider that is associated with this world.
Definition World_inl.h:420
plStringView GetName() const
Returns the name of this world.
Definition World_inl.h:2
void SendMessageRecursive(const plGameObjectHandle &hReceiverObject, plMessage &ref_msg)
Sends a message to all components of the receiverObject and all its children.
Definition World_inl.h:327
bool GetWorldSimulationEnabled() const
If enabled, the full simulation should be executed, otherwise only the rendering related updates shou...
Definition World_inl.h:386
void PostMessage(const plGameObjectHandle &hReceiverObject, const plMessage &msg, plTime delay, plObjectMsgQueueType::Enum queueType=plObjectMsgQueueType::NextFrame) const
Queues the message for the given phase. The message is send to the receiverObject after the given del...
Definition World_inl.h:347
bool IsValidComponent(const plComponentHandle &hComponent) const
Checks whether the given handle references a valid component.
Definition World_inl.h:247
plDoubleBufferedLinearAllocator * GetStackAllocator()
Returns the stack allocator used by this world.
Definition World_inl.h:455
plComponentManagerBase * GetManagerForComponentType(const plRTTI *pComponentRtti)
Returns the component manager that handles the given rtti component type.
Definition World_inl.h:233
void SendMessage(const plGameObjectHandle &hReceiverObject, plMessage &ref_msg)
Sends a message to all components of the receiverObject.
Definition World_inl.h:307
void SetUserData(void *pUserData)
Associates the given user data with the world. The user is responsible for the life time of user data...
Definition World_inl.h:470
ManagerType * GetComponentManager()
Returns the instance to the given component manager type.
bool TryGetComponent(const plComponentHandle &hComponent, ComponentType *&out_pComponent)
Returns whether a component with the given handle exists and if so writes out the corresponding point...
Definition World_inl.h:264
void PostMessageRecursive(const plGameObjectHandle &hReceiverObject, const plMessage &msg, plTime delay, plObjectMsgQueueType::Enum queueType=plObjectMsgQueueType::NextFrame) const
Queues the message for the given phase. The message is send to the receiverObject and all its childre...
Definition World_inl.h:354
void * GetUserData() const
Returns the associated user data.
Definition World_inl.h:477
bool TryGetObject(const plGameObjectHandle &hObject, plGameObject *&out_pObject)
Returns whether an object with the given handle exists and if so writes out the corresponding pointer...
Definition World_inl.h:32
const plSharedPtr< plTask > & GetUpdateTask()
Returns a task implementation that calls Update on this world.
Definition World_inl.h:391
plUInt32 GetObjectCount() const
Returns the total number of objects in this world.
Definition World_inl.h:79
Definition WorldModule.h:10
A handle to a component.
Definition Declarations.h:138
Definition CoordinateSystem.h:9
Describes the initial state of a game object.
Definition GameObjectDesc.h:11
A handle to a game object.
Definition Declarations.h:76
Internal game object id used by plGameObjectHandle.
Definition Declarations.h:43
Enum
Definition Declarations.h:274
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
constexpr double GetSeconds() const
Returns the seconds value.
Definition Time_inl.h:30