Plasma Engine  2.0
Loading...
Searching...
No Matches
ComponentManager_inl.h
1
2PL_FORCE_INLINE bool plComponentManagerBase::IsValidComponent(const plComponentHandle& hComponent) const
3{
4 return m_Components.Contains(hComponent);
5}
6
7PL_FORCE_INLINE bool plComponentManagerBase::TryGetComponent(const plComponentHandle& hComponent, plComponent*& out_pComponent)
8{
9 return m_Components.TryGetValue(hComponent, out_pComponent);
10}
11
12PL_FORCE_INLINE bool plComponentManagerBase::TryGetComponent(const plComponentHandle& hComponent, const plComponent*& out_pComponent) const
13{
14 plComponent* pComponent = nullptr;
15 bool res = m_Components.TryGetValue(hComponent, pComponent);
16 out_pComponent = pComponent;
17 return res;
18}
19
20PL_ALWAYS_INLINE plUInt32 plComponentManagerBase::GetComponentCount() const
21{
22 return static_cast<plUInt32>(m_Components.GetCount());
23}
24
25template <typename ComponentType>
26PL_ALWAYS_INLINE plComponentHandle plComponentManagerBase::CreateComponent(plGameObject* pOwnerObject, ComponentType*& out_pComponent)
27{
28 plComponent* pComponent = nullptr;
29 plComponentHandle hComponent = CreateComponentNoInit(pOwnerObject, pComponent);
30
31 if (pComponent != nullptr)
32 {
33 InitializeComponent(pComponent);
34 }
35
36 out_pComponent = plStaticCast<ComponentType*>(pComponent);
37 return hComponent;
38}
39
41
42template <typename T, plBlockStorageType::Enum StorageType>
45 , m_ComponentStorage(GetBlockAllocator(), GetAllocator())
46{
47 static_assert(PL_IS_DERIVED_FROM_STATIC(plComponent, ComponentType), "Not a valid component type");
48}
49
50template <typename T, plBlockStorageType::Enum StorageType>
52
53template <typename T, plBlockStorageType::Enum StorageType>
54PL_FORCE_INLINE bool plComponentManager<T, StorageType>::TryGetComponent(const plComponentHandle& hComponent, ComponentType*& out_pComponent)
55{
56 PL_ASSERT_DEV(ComponentType::TypeId() == hComponent.GetInternalID().m_TypeId,
57 "The given component handle is not of the expected type. Expected type id {0}, got type id {1}", ComponentType::TypeId(),
58 hComponent.GetInternalID().m_TypeId);
59 PL_ASSERT_DEV(hComponent.GetInternalID().m_WorldIndex == GetWorldIndex(),
60 "Component does not belong to this world. Expected world id {0} got id {1}", GetWorldIndex(), hComponent.GetInternalID().m_WorldIndex);
61
62 plComponent* pComponent = nullptr;
63 bool bResult = plComponentManagerBase::TryGetComponent(hComponent, pComponent);
64 out_pComponent = static_cast<ComponentType*>(pComponent);
65 return bResult;
66}
67
68template <typename T, plBlockStorageType::Enum StorageType>
70 const plComponentHandle& hComponent, const ComponentType*& out_pComponent) const
71{
72 PL_ASSERT_DEV(ComponentType::TypeId() == hComponent.GetInternalID().m_TypeId,
73 "The given component handle is not of the expected type. Expected type id {0}, got type id {1}", ComponentType::TypeId(),
74 hComponent.GetInternalID().m_TypeId);
75 PL_ASSERT_DEV(hComponent.GetInternalID().m_WorldIndex == GetWorldIndex(),
76 "Component does not belong to this world. Expected world id {0} got id {1}", GetWorldIndex(), hComponent.GetInternalID().m_WorldIndex);
77
78 const plComponent* pComponent = nullptr;
79 bool bResult = plComponentManagerBase::TryGetComponent(hComponent, pComponent);
80 out_pComponent = static_cast<const ComponentType*>(pComponent);
81 return bResult;
82}
83
84template <typename T, plBlockStorageType::Enum StorageType>
86{
87 return m_ComponentStorage.GetIterator(uiStartIndex);
88}
89
90template <typename T, plBlockStorageType::Enum StorageType>
92plComponentManager<T, StorageType>::GetComponents(plUInt32 uiStartIndex /*= 0*/) const
93{
94 return m_ComponentStorage.GetIterator(uiStartIndex);
95}
96
97// static
98template <typename T, plBlockStorageType::Enum StorageType>
99PL_ALWAYS_INLINE plWorldModuleTypeId plComponentManager<T, StorageType>::TypeId()
100{
101 return T::TypeId();
102}
103
104template <typename T, plBlockStorageType::Enum StorageType>
106{
107 out_allComponents.Reserve(out_allComponents.GetCount() + m_ComponentStorage.GetCount());
108
109 for (auto it = GetComponents(); it.IsValid(); it.Next())
111 if (!bOnlyActive || it->IsActive())
113 out_allComponents.PushBack(it->GetHandle());
114 }
115 }
116}
117
118template <typename T, plBlockStorageType::Enum StorageType>
120{
121 out_allComponents.Reserve(out_allComponents.GetCount() + m_ComponentStorage.GetCount());
122
123 for (auto it = GetComponents(); it.IsValid(); it.Next())
124 {
125 if (!bOnlyActive || it->IsActive())
126 {
127 out_allComponents.PushBack(it);
128 }
129 }
130}
131
132template <typename T, plBlockStorageType::Enum StorageType>
134{
135 return m_ComponentStorage.Create();
136}
137
138template <typename T, plBlockStorageType::Enum StorageType>
139PL_FORCE_INLINE void plComponentManager<T, StorageType>::DeleteComponentStorage(plComponent* pComponent, plComponent*& out_pMovedComponent)
140{
141 T* pMovedComponent = nullptr;
142 m_ComponentStorage.Delete(static_cast<T*>(pComponent), pMovedComponent);
143 out_pMovedComponent = pMovedComponent;
144}
145
146template <typename T, plBlockStorageType::Enum StorageType>
147PL_FORCE_INLINE void plComponentManager<T, StorageType>::RegisterUpdateFunction(UpdateFunctionDesc& desc)
148{
149 // round up to multiple of data block capacity so tasks only have to deal with complete data blocks
150 if (desc.m_uiGranularity != 0)
151 desc.m_uiGranularity = static_cast<plUInt16>(
153
155}
156
158
159template <typename ComponentType, plComponentUpdateType::Enum UpdateType, plBlockStorageType::Enum StorageType>
161 : plComponentManager<ComponentType, StorageType>(pWorld)
162{
163}
164
165template <typename ComponentType, plComponentUpdateType::Enum UpdateType, plBlockStorageType::Enum StorageType>
167{
169
170 plStringBuilder functionName;
171 SimpleUpdateName(functionName);
172
173 auto desc = plWorldModule::UpdateFunctionDesc(plWorldModule::UpdateFunction(&OwnType::SimpleUpdate, this), functionName);
174 desc.m_bOnlyUpdateWhenSimulating = (UpdateType == plComponentUpdateType::WhenSimulating);
175
176 this->RegisterUpdateFunction(desc);
177}
178
179template <typename ComponentType, plComponentUpdateType::Enum UpdateType, plBlockStorageType::Enum StorageType>
181{
182 for (auto it = this->m_ComponentStorage.GetIterator(context.m_uiFirstComponentIndex, context.m_uiComponentCount); it.IsValid(); ++it)
183 {
184 ComponentType* pComponent = it;
185 if (pComponent->IsActiveAndInitialized())
186 {
187 pComponent->Update();
188 }
189 }
190}
191
192// static
193template <typename ComponentType, plComponentUpdateType::Enum UpdateType, plBlockStorageType::Enum StorageType>
195{
196 plStringView sName(PL_SOURCE_FUNCTION);
197 const char* szEnd = sName.FindSubString(",");
198
199 if (szEnd != nullptr && sName.StartsWith("plComponentManagerSimple<class "))
200 {
201 plStringView sChoppedName(sName.GetStartPointer() + plStringUtils::GetStringElementCount("plComponentManagerSimple<class "), szEnd);
202
203 PL_ASSERT_DEV(!sChoppedName.IsEmpty(), "Chopped name is empty: '{0}'", sName);
204
205 out_sName = sChoppedName;
206 out_sName.Append("::SimpleUpdate");
207 }
208 else
209 {
210 out_sName = sName;
211 }
212}
void PushBack(const T &value)
Pushes value at the end of the array.
Definition ArrayBase_inl.h:333
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition ArrayBase_inl.h:172
Definition BlockStorage.h:20
Definition BlockStorage.h:45
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
bool IsValidComponent(const plComponentHandle &hComponent) const
Checks whether the given handle references a valid component.
Definition ComponentManager_inl.h:2
plUInt32 GetComponentCount() const
Returns the number of components managed by this manager.
Definition ComponentManager_inl.h:20
plComponentHandle CreateComponent(plGameObject *pOwnerObject)
Create a new component instance and returns a handle to it.
Definition ComponentManager.cpp:18
bool TryGetComponent(const plComponentHandle &hComponent, plComponent *&out_pComponent)
Returns if a component with the given handle exists and if so writes out the corresponding pointer to...
Definition ComponentManager_inl.h:7
Definition ComponentManager.h:88
plComponentManager(plWorld *pWorld)
Although the constructor is public always use plWorld::CreateComponentManager to create an instance.
Definition ComponentManager_inl.h:43
plBlockStorage< ComponentType, plInternal::DEFAULT_BLOCK_SIZE, StorageType >::Iterator GetComponents(plUInt32 uiStartIndex=0)
Returns an iterator over all components.
Definition ComponentManager_inl.h:85
bool TryGetComponent(const plComponentHandle &hComponent, ComponentType *&out_pComponent)
Returns if a component with the given handle exists and if so writes out the corresponding pointer to...
Definition ComponentManager_inl.h:54
static plWorldModuleTypeId TypeId()
Returns the type id corresponding to the component type managed by this manager.
Definition ComponentManager_inl.h:99
virtual void CollectAllComponents(plDynamicArray< plComponentHandle > &out_allComponents, bool bOnlyActive) override
Adds all components that this manager handles to the given array (array is not cleared)....
Definition ComponentManager_inl.h:105
Simple component manager implementation that calls an update method on all components every frame.
Definition ComponentManager.h:142
virtual void Initialize() override
This method is called after the constructor. A derived type can override this method to do initializa...
Definition ComponentManager_inl.h:166
void SimpleUpdate(const plWorldModule::UpdateContext &context)
A simple update function that iterates over all components and calls Update() on every component.
Definition ComponentManager_inl.h:180
void Reserve(plUInt32 uiCapacity)
Expands the array so it can at least store the given capacity.
Definition DynamicArray_inl.h:179
Definition DynamicArray.h:81
This class represents an object inside the world.
Definition GameObject.h:32
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
bool Contains(const IdType id) const
Returns if the table contains an entry corresponding to the given id.
Definition IdTable_inl.h:328
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
void Append(plUInt32 uiChar)
Appends a single Utf32 character.
Definition StringBuilder_inl.h:94
static constexpr plUInt32 GetStringElementCount(const T *pString)
Returns the number of elements of type T that the string contains, until it hits an element that is z...
Definition StringUtils_inl.h:45
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
void RegisterUpdateFunction(const UpdateFunctionDesc &desc)
Registers the given update function at the world.
Definition WorldModule.cpp:24
double RoundUp(double f, double fMultiple)
Returns a multiple of fMultiple that is larger than f.
Definition MathDouble_inl.h:47
A handle to a component.
Definition Declarations.h:138
This struct represents a block of type T, typically 4kb.
Definition LargeBlockAllocator.h:14
Definition WorldModule.h:33
Description of an update function that can be registered at the world.
Definition WorldModule.h:43