Plasma Engine  2.0
Loading...
Searching...
No Matches
Component.h
1#pragma once
2
4
5#include <Core/World/Declarations.h>
6#include <Foundation/Reflection/Reflection.h>
7
8class plMessage;
9class plWorldWriter;
10class plWorldReader;
11
12// TODO: windows.h workaround
13#ifdef SendMessage
14# undef SendMessage
15#endif
16
24class PL_CORE_DLL plComponent : public plReflectedClass
25{
26 PL_ADD_DYNAMIC_REFLECTION(plComponent, plReflectedClass);
27
28protected:
31 virtual ~plComponent();
32
33public:
43 void SetActiveFlag(bool bEnabled);
44
48 bool GetActiveFlag() const;
49
57 bool IsActive() const;
58
62 bool IsActiveAndInitialized() const;
63
67 bool IsActiveAndSimulating() const;
68
70 plComponentManagerBase* GetOwningManager();
71
73 const plComponentManagerBase* GetOwningManager() const;
74
76 plGameObject* GetOwner();
77
79 const plGameObject* GetOwner() const;
80
82 plWorld* GetWorld();
83
85 const plWorld* GetWorld() const;
86
87
89 plComponentHandle GetHandle() const;
90
92 plUInt32 GetUniqueID() const;
93
95 void SetUniqueID(plUInt32 uiUniqueID);
96
97
99 virtual void SerializeComponent(plWorldWriter& inout_stream) const;
100
105 virtual void DeserializeComponent(plWorldReader& inout_stream);
106
107
109 void EnsureInitialized();
110
113 void EnsureSimulationStarted();
114
115
117 PL_ALWAYS_INLINE bool SendMessage(plMessage& ref_msg) { return SendMessageInternal(ref_msg, false); }
118 PL_ALWAYS_INLINE bool SendMessage(plMessage& ref_msg) const { return SendMessageInternal(ref_msg, false); }
119
121 void PostMessage(const plMessage& msg, plTime delay = plTime::MakeZero(), plObjectMsgQueueType::Enum queueType = plObjectMsgQueueType::NextFrame) const;
122
124 virtual bool HandlesMessage(const plMessage& msg) const;
125
127 void SetUserFlag(plUInt8 uiFlagIndex, bool bSet);
128
130 bool GetUserFlag(plUInt8 uiFlagIndex) const;
131
133 void SetCreatedByPrefab() { m_ComponentFlags.Add(plObjectFlags::CreatedByPrefab); }
134
136 bool WasCreatedByPrefab() const { return m_ComponentFlags.IsSet(plObjectFlags::CreatedByPrefab); }
137
138protected:
139 friend class plWorld;
140 friend class plGameObject;
141 friend class plComponentManagerBase;
142
144 bool IsDynamic() const;
145
146 virtual plWorldModuleTypeId GetTypeId() const = 0;
147 virtual plComponentMode::Enum GetMode() const = 0;
148
158 virtual void Initialize();
159
168 virtual void Deinitialize();
169
177 virtual void OnActivated();
178
185 virtual void OnDeactivated();
186
205 virtual void OnSimulationStarted();
206
208 void EnableUnhandledMessageHandler(bool enable);
209
213 virtual bool OnUnhandledMessage(plMessage& msg, bool bWasPostedMsg);
214
218 virtual bool OnUnhandledMessage(plMessage& msg, bool bWasPostedMsg) const;
219
220protected:
222 const plRTTI* m_pMessageDispatchType = nullptr;
223
224 bool IsInitialized() const;
225 bool IsInitializing() const;
226 bool IsSimulationStarted() const;
227
228private:
229 // updates the component's active state depending on the owner object's active state
230 void UpdateActiveState(bool bOwnerActive);
231
232 plGameObject* Reflection_GetOwner() const;
233 plWorld* Reflection_GetWorld() const;
234 void Reflection_Update(plTime deltaTime);
235
236 bool SendMessageInternal(plMessage& msg, bool bWasPostedMsg);
237 bool SendMessageInternal(plMessage& msg, bool bWasPostedMsg) const;
238
239 plComponentId m_InternalId;
241 plUInt32 m_uiUniqueID = plInvalidIndex;
242
243 plComponentManagerBase* m_pManager = nullptr;
244 plGameObject* m_pOwner = nullptr;
245
246 static plWorldModuleTypeId s_TypeId;
247};
248
250{
251 enum Enum
252 {
253 Initialize,
254 Deinitialize,
255 OnActivated,
256 OnDeactivated,
257 OnSimulationStarted,
258 Update,
259
260 Count
261 };
262};
263
264#include <Core/World/Implementation/Component_inl.h>
Base class of all component types.
Definition Component.h:25
bool WasCreatedByPrefab() const
Checks whether the plObjectFlags::CreatedByPrefab flag is set on this component.
Definition Component.h:136
PL_ALWAYS_INLINE bool SendMessage(plMessage &ref_msg)
Sends a message to this component.
Definition Component.h:117
void SetCreatedByPrefab()
Adds plObjectFlags::CreatedByPrefab to the component. See the flag for details.
Definition Component.h:133
plComponent()
Keep the constructor private or protected in derived classes, so it cannot be called manually.
Base class for all component managers. Do not derive directly from this class, but derive from plComp...
Definition ComponentManager.h:21
This class represents an object inside the world.
Definition GameObject.h:32
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
All classes that should be dynamically reflectable, need to be derived from this base class.
Definition DynamicRTTI.h:86
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
Reads a world description from a stream. Allows to instantiate that world multiple times in different...
Definition WorldReader.h:47
Stores an entire plWorld in a stream.
Definition WorldWriter.h:13
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
Definition Component.h:250
A handle to a component.
Definition Declarations.h:138
Internal component id used by plComponentHandle.
Definition Declarations.h:103
@ CreatedByPrefab
Such flagged objects and components are ignored during scene export (see plWorldWriter) and will be r...
Definition Declarations.h:191
@ ActiveFlag
The object/component has the 'active flag' set.
Definition Declarations.h:178
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
PL_ALWAYS_INLINE static constexpr plTime MakeZero()
Creates an instance of plTime that was initialized with zero.
Definition Time.h:42