Plasma Engine  2.0
Loading...
Searching...
No Matches
ComponentManager.h
1#pragma once
2
3#include <Foundation/Containers/HybridArray.h>
4#include <Foundation/Containers/IdTable.h>
5#include <Foundation/Logging/Log.h>
6#include <Foundation/Memory/BlockStorage.h>
7#include <Foundation/Reflection/Reflection.h>
8#include <Foundation/Types/Delegate.h>
9
10#include <Core/World/Component.h>
11#include <Core/World/Declarations.h>
12#include <Core/World/WorldModule.h>
13
20class PL_CORE_DLL plComponentManagerBase : public plWorldModule
21{
22 PL_ADD_DYNAMIC_REFLECTION(plComponentManagerBase, plWorldModule);
23
24protected:
27
28public:
30 bool IsValidComponent(const plComponentHandle& hComponent) const;
31
33 bool TryGetComponent(const plComponentHandle& hComponent, plComponent*& out_pComponent);
34
36 bool TryGetComponent(const plComponentHandle& hComponent, const plComponent*& out_pComponent) const;
37
39 plUInt32 GetComponentCount() const;
40
42 plComponentHandle CreateComponent(plGameObject* pOwnerObject);
43
45 template <typename ComponentType>
46 plComponentHandle CreateComponent(plGameObject* pOwnerObject, ComponentType*& out_pComponent);
47
49 void DeleteComponent(const plComponentHandle& hComponent);
50
52 void DeleteComponent(plComponent* pComponent);
53
56 virtual void CollectAllComponents(plDynamicArray<plComponentHandle>& out_allComponents, bool bOnlyActive) = 0;
57
60 virtual void CollectAllComponents(plDynamicArray<plComponent*>& out_allComponents, bool bOnlyActive) = 0;
61
62protected:
64 // internal methods
65 friend class plWorld;
66 friend class plInternal::WorldData;
67
68 virtual void Deinitialize() override;
69
70protected:
71 friend class plWorldReader;
72
73 plComponentHandle CreateComponentNoInit(plGameObject* pOwnerObject, plComponent*& out_pComponent);
74 void InitializeComponent(plComponent* pComponent);
75 void DeinitializeComponent(plComponent* pComponent);
76 void PatchIdTable(plComponent* pComponent);
77
78 virtual plComponent* CreateComponentStorage() = 0;
79 virtual void DeleteComponentStorage(plComponent* pComponent, plComponent*& out_pMovedComponent) = 0;
80
82
84};
85
86template <typename T, plBlockStorageType::Enum StorageType>
88{
89public:
90 using ComponentType = T;
92
95 virtual ~plComponentManager();
96
98 bool TryGetComponent(const plComponentHandle& hComponent, ComponentType*& out_pComponent);
99
101 bool TryGetComponent(const plComponentHandle& hComponent, const ComponentType*& out_pComponent) const;
102
105
108
110 static plWorldModuleTypeId TypeId();
111
112 virtual void CollectAllComponents(plDynamicArray<plComponentHandle>& out_allComponents, bool bOnlyActive) override;
113 virtual void CollectAllComponents(plDynamicArray<plComponent*>& out_allComponents, bool bOnlyActive) override;
114
115protected:
116 friend ComponentType;
117 friend class plComponentManagerFactory;
118
119 virtual plComponent* CreateComponentStorage() override;
120 virtual void DeleteComponentStorage(plComponent* pComponent, plComponent*& out_pMovedComponent) override;
121
122 void RegisterUpdateFunction(UpdateFunctionDesc& desc);
123
125};
126
127
129
131{
132 enum Enum
133 {
134 Always,
135 WhenSimulating
136 };
137};
138
140template <typename ComponentType, plComponentUpdateType::Enum UpdateType, plBlockStorageType::Enum StorageType = plBlockStorageType::FreeList>
141class plComponentManagerSimple final : public plComponentManager<ComponentType, StorageType>
142{
143public:
145
146 virtual void Initialize() override;
147
149 void SimpleUpdate(const plWorldModule::UpdateContext& context);
150
151private:
152 static void SimpleUpdateName(plStringBuilder& out_sName);
153};
154
156
157#define PL_ADD_COMPONENT_FUNCTIONALITY(componentType, baseType, managerType) \
158public: \
159 using ComponentManagerType = managerType; \
160 virtual plWorldModuleTypeId GetTypeId() const override { return s_TypeId; } \
161 static PL_ALWAYS_INLINE plWorldModuleTypeId TypeId() { return s_TypeId; } \
162 virtual plComponentMode::Enum GetMode() const override; \
163 static plComponentHandle CreateComponent(plGameObject* pOwnerObject, componentType*& pComponent); \
164 static void DeleteComponent(componentType* pComponent); \
165 void DeleteComponent(); \
166 \
167private: \
168 friend managerType; \
169 static plWorldModuleTypeId s_TypeId
170
171#define PL_ADD_ABSTRACT_COMPONENT_FUNCTIONALITY(componentType, baseType) \
172public: \
173 virtual plWorldModuleTypeId GetTypeId() const override { return plWorldModuleTypeId(-1); } \
174 static PL_ALWAYS_INLINE plWorldModuleTypeId TypeId() { return plWorldModuleTypeId(-1); }
175
177#define PL_DECLARE_COMPONENT_TYPE(componentType, baseType, managerType) \
178 PL_ADD_DYNAMIC_REFLECTION(componentType, baseType); \
179 PL_ADD_COMPONENT_FUNCTIONALITY(componentType, baseType, managerType);
180
182#define PL_DECLARE_ABSTRACT_COMPONENT_TYPE(componentType, baseType) \
183 PL_ADD_DYNAMIC_REFLECTION(componentType, baseType); \
184 PL_ADD_ABSTRACT_COMPONENT_FUNCTIONALITY(componentType, baseType);
185
186
190#define PL_BEGIN_COMPONENT_TYPE(componentType, version, mode) \
191 plWorldModuleTypeId componentType::s_TypeId = \
192 plWorldModuleFactory::GetInstance()->RegisterWorldModule<typename componentType::ComponentManagerType, componentType>(); \
193 plComponentMode::Enum componentType::GetMode() const { return mode; } \
194 plComponentHandle componentType::CreateComponent(plGameObject* pOwnerObject, componentType*& out_pComponent) \
195 { \
196 return pOwnerObject->GetWorld()->GetOrCreateComponentManager<ComponentManagerType>()->CreateComponent(pOwnerObject, out_pComponent); \
197 } \
198 void componentType::DeleteComponent(componentType* pComponent) { pComponent->GetOwningManager()->DeleteComponent(pComponent->GetHandle()); } \
199 void componentType::DeleteComponent() { GetOwningManager()->DeleteComponent(GetHandle()); } \
200 PL_BEGIN_DYNAMIC_REFLECTED_TYPE(componentType, version, plRTTINoAllocator)
201
205#define PL_BEGIN_ABSTRACT_COMPONENT_TYPE(componentType, version) PL_BEGIN_ABSTRACT_DYNAMIC_REFLECTED_TYPE(componentType, version)
206
208#define PL_END_COMPONENT_TYPE PL_END_DYNAMIC_REFLECTED_TYPE
209#define PL_END_ABSTRACT_COMPONENT_TYPE PL_END_ABSTRACT_DYNAMIC_REFLECTED_TYPE
210
211#include <Core/World/Implementation/ComponentManager_inl.h>
Definition BlockStorage.h:20
Definition BlockStorage.h:45
Definition BlockStorage.h:17
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
virtual void CollectAllComponents(plDynamicArray< plComponent * > &out_allComponents, bool bOnlyActive)=0
Adds all components that this manager handles to the given array (array is not cleared)....
plComponentHandle CreateComponent(plGameObject *pOwnerObject, ComponentType *&out_pComponent)
Create a new component instance and returns a handle to it.
virtual void CollectAllComponents(plDynamicArray< plComponentHandle > &out_allComponents, bool bOnlyActive)=0
Adds all components that this manager handles to the given array (array is not cleared)....
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
Definition DynamicArray.h:81
This class represents an object inside the world.
Definition GameObject.h:32
Definition IdTable.h:171
Definition WorldData.h:18
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
Definition WorldModule.h:10
virtual void Deinitialize()
This method is called before the destructor. A derived type can override this method to do deinitiali...
Definition WorldModule.h:101
Reads a world description from a stream. Allows to instantiate that world multiple times in different...
Definition WorldReader.h:47
A handle to a component.
Definition Declarations.h:138
Definition ComponentManager.h:131
Definition WorldModule.h:33
Description of an update function that can be registered at the world.
Definition WorldModule.h:43