Plasma Engine  2.0
Loading...
Searching...
No Matches
StateMachine.h
1#pragma once
2
3#include <GameEngine/StateMachine/Implementation/StateMachineInstanceData.h>
4
5#include <Foundation/Reflection/Reflection.h>
6#include <Foundation/Strings/HashedString.h>
7#include <Foundation/Types/SharedPtr.h>
8
9class plComponent;
10class plWorld;
11class plBlackboard;
13
19class PL_GAMEENGINE_DLL plStateMachineState : public plReflectedClass
20{
21 PL_ADD_DYNAMIC_REFLECTION(plStateMachineState, plReflectedClass);
22
23public:
25
26 void SetName(plStringView sName);
27 plStringView GetName() const { return m_sName; }
28 const plHashedString& GetNameHashed() const { return m_sName; }
29
30 virtual void OnEnter(plStateMachineInstance& ref_instance, void* pInstanceData, const plStateMachineState* pFromState) const = 0;
31 virtual void OnExit(plStateMachineInstance& ref_instance, void* pInstanceData, const plStateMachineState* pToState) const;
32 virtual void Update(plStateMachineInstance& ref_instance, void* pInstanceData, plTime deltaTime) const;
33
34 virtual plResult Serialize(plStreamWriter& inout_stream) const;
35 virtual plResult Deserialize(plStreamReader& inout_stream);
36
40 virtual bool GetInstanceDataDesc(plInstanceDataDesc& out_desc);
41
42private:
43 // These are dummy functions for the scripting reflection
44 void Reflection_OnEnter(plStateMachineInstance* pStateMachineInstance, const plStateMachineState* pFromState);
45 void Reflection_OnExit(plStateMachineInstance* pStateMachineInstance, const plStateMachineState* pToState);
46 void Reflection_Update(plStateMachineInstance* pStateMachineInstance, plTime deltaTime);
47
48 plHashedString m_sName;
49};
50
51class PL_GAMEENGINE_DLL plStateMachineState_Empty final : public plStateMachineState
52{
53 PL_ADD_DYNAMIC_REFLECTION(plStateMachineState_Empty, plStateMachineState);
54
55public:
58
59 virtual void OnEnter(plStateMachineInstance& ref_instance, void* pInstanceData, const plStateMachineState* pFromState) const override {}
60};
61
63{
64 enum Enum
65 {
66 OnEnter,
67 OnExit,
68 Update,
69
70 Count
71 };
72};
73
80class PL_GAMEENGINE_DLL plStateMachineTransition : public plReflectedClass
81{
82 PL_ADD_DYNAMIC_REFLECTION(plStateMachineTransition, plReflectedClass);
83
84 virtual bool IsConditionMet(plStateMachineInstance& ref_instance, void* pInstanceData) const = 0;
85
86 virtual plResult Serialize(plStreamWriter& inout_stream) const;
87 virtual plResult Deserialize(plStreamReader& inout_stream);
88
92 virtual bool GetInstanceDataDesc(plInstanceDataDesc& out_desc);
93};
94
98class PL_GAMEENGINE_DLL plStateMachineDescription : public plRefCounted
99{
100 PL_DISALLOW_COPY_AND_ASSIGN(plStateMachineDescription);
101
102public:
105
107 plUInt32 AddState(plUniquePtr<plStateMachineState>&& pState);
108
110 void AddTransition(plUInt32 uiFromStateIndex, plUInt32 uiToStateIndex, plUniquePtr<plStateMachineTransition>&& pTransistion);
111
112 plResult Serialize(plStreamWriter& inout_stream) const;
113 plResult Deserialize(plStreamReader& inout_stream);
114
115private:
116 friend class plStateMachineInstance;
117
118 struct TransitionContext
119 {
121 plUInt32 m_uiToStateIndex = 0;
122 plUInt32 m_uiInstanceDataOffset = plInvalidIndex;
123 };
124
126 TransitionArray m_FromAnyTransitions;
127
128 struct StateContext
129 {
131 TransitionArray m_Transitions;
132 plUInt32 m_uiInstanceDataOffset = plInvalidIndex;
133 };
134
136 plHashTable<plHashedString, plUInt32> m_StateNameToIndexTable;
137
138 plInstanceDataAllocator m_InstanceDataAllocator;
139};
140
143class PL_GAMEENGINE_DLL plStateMachineInstance
144{
145 PL_DISALLOW_COPY_AND_ASSIGN(plStateMachineInstance);
146
147public:
150
151 plResult SetState(plStateMachineState* pState);
152 plResult SetState(plUInt32 uiStateIndex);
153 plResult SetState(const plHashedString& sStateName);
154 plResult SetStateOrFallback(const plHashedString& sStateName, plUInt32 uiFallbackStateIndex = 0);
155 plStateMachineState* GetCurrentState() { return m_pCurrentState; }
156
157 void Update(plTime deltaTime);
158
159 plReflectedClass& GetOwner() { return m_Owner; }
160 plWorld* GetOwnerWorld();
161
162 void SetBlackboard(const plSharedPtr<plBlackboard>& pBlackboard);
163 const plSharedPtr<plBlackboard>& GetBlackboard() const { return m_pBlackboard; }
164
166 plTime GetTimeInCurrentState() const { return m_TimeInCurrentState; }
167
169 void FireTransitionEvent(plStringView sEvent);
170
171 plStringView GetCurrentTransitionEvent() const { return m_sCurrentTransitionEvent; }
172
173private:
174 PL_ALLOW_PRIVATE_PROPERTIES(plStateMachineInstance);
175
176 bool Reflection_SetState(const plHashedString& sStateName);
177 plComponent* Reflection_GetOwnerComponent() const;
178 plBlackboard* Reflection_GetBlackboard() const { return m_pBlackboard.Borrow(); }
179
180 void SetStateInternal(plUInt32 uiStateIndex);
181 void EnterCurrentState(const plStateMachineState* pFromState);
182 void ExitCurrentState(const plStateMachineState* pToState);
183 plUInt32 FindNewStateToTransitionTo();
184
185 PL_ALWAYS_INLINE void* GetInstanceData(plUInt32 uiOffset)
186 {
187 return plInstanceDataAllocator::GetInstanceData(m_InstanceData.GetByteBlobPtr(), uiOffset);
188 }
189
190 PL_ALWAYS_INLINE void* GetCurrentStateInstanceData()
191 {
192 if (m_pDescription != nullptr && m_uiCurrentStateIndex < m_pDescription->m_States.GetCount())
193 {
194 return GetInstanceData(m_pDescription->m_States[m_uiCurrentStateIndex].m_uiInstanceDataOffset);
195 }
196 return nullptr;
197 }
198
199 plReflectedClass& m_Owner;
201 plSharedPtr<plBlackboard> m_pBlackboard;
202
203 plStateMachineState* m_pCurrentState = nullptr;
204 plUInt32 m_uiCurrentStateIndex = plInvalidIndex;
205 plTime m_TimeInCurrentState;
206 plStringView m_sCurrentTransitionEvent;
207
208 const plStateMachineDescription::TransitionArray* m_pCurrentTransitions = nullptr;
209
210 plBlob m_InstanceData;
211};
212
213PL_DECLARE_REFLECTABLE_TYPE(PL_GAMEENGINE_DLL, plStateMachineInstance);
A blackboard is a key/value store that provides OnChange events to be informed when a value changes.
Definition Blackboard.h:66
plBlob allows to store simple binary data larger than 4GB. This storage class is used by plImage to a...
Definition Blob.h:319
Base class of all component types.
Definition Component.h:25
Definition DynamicArray.h:81
Definition HashTable.h:333
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
Helper class to manager instance data allocation, construction and destruction.
Definition InstanceDataAllocator.h:35
static PL_ALWAYS_INLINE void * GetInstanceData(const plByteBlobPtr &blobPtr, plUInt32 uiOffset)
Retrieves a void pointer to the instance data within the given blob at the given offset,...
Definition InstanceDataAllocator.h:59
Base class for reference counted objects.
Definition RefCounted.h:52
All classes that should be dynamically reflectable, need to be derived from this base class.
Definition DynamicRTTI.h:86
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
The state machine description defines the structure of a state machine like e.g. what states it has a...
Definition StateMachine.h:99
The state machine instance represents the actual state machine. Typically it is created from a descri...
Definition StateMachine.h:144
plTime GetTimeInCurrentState() const
Returns how long the state machine is in its current state.
Definition StateMachine.h:166
Definition StateMachine.h:52
Base class for a state in a state machine.
Definition StateMachine.h:20
Base class for a transition in a state machine. The target state of a transition is automatically set...
Definition StateMachine.h:81
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
Structure to describe an instance data type.
Definition InstanceDataAllocator.h:17
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