Plasma Engine  2.0
Loading...
Searching...
No Matches
StateMachineComponent.h
1#pragma once
2
3#include <Core/Messages/EventMessage.h>
4#include <GameEngine/StateMachine/StateMachineResource.h>
5
7struct PL_GAMEENGINE_DLL plMsgStateMachineStateChanged : public plEventMessage
8{
9 PL_DECLARE_MESSAGE_TYPE(plMsgStateMachineStateChanged, plEventMessage);
10
11 plHashedString m_sOldStateName;
12 plHashedString m_sNewStateName;
13
14private:
15 const char* GetOldStateName() const { return m_sOldStateName; }
16 void SetOldStateName(const char* szName) { m_sOldStateName.Assign(szName); }
17
18 const char* GetNewStateName() const { return m_sNewStateName; }
19 void SetNewStateName(const char* szName) { m_sNewStateName.Assign(szName); }
20};
21
23
29{
30 PL_ADD_DYNAMIC_REFLECTION(plStateMachineState_SendMsg, plStateMachineState);
31
32public:
35
36 virtual void OnEnter(plStateMachineInstance& ref_instance, void* pInstanceData, const plStateMachineState* pFromState) const override;
37 virtual void OnExit(plStateMachineInstance& ref_instance, void* pInstanceData, const plStateMachineState* pToState) const override;
38
39 virtual plResult Serialize(plStreamWriter& inout_stream) const override;
40 virtual plResult Deserialize(plStreamReader& inout_stream) override;
41
42 plTime m_MessageDelay;
43
44 bool m_bSendMessageOnEnter = true;
45 bool m_bSendMessageOnExit = false;
46 bool m_bLogOnEnter = false;
47 bool m_bLogOnExit = false;
48};
49
51
69{
70 PL_ADD_DYNAMIC_REFLECTION(plStateMachineState_SwitchObject, plStateMachineState);
71
72public:
75
76 virtual void OnEnter(plStateMachineInstance& ref_instance, void* pInstanceData, const plStateMachineState* pFromState) const override;
77
78 virtual plResult Serialize(plStreamWriter& inout_stream) const override;
79 virtual plResult Deserialize(plStreamReader& inout_stream) override;
80
81 plString m_sGroupPath;
82 plString m_sObjectToEnable;
83 bool m_bDeactivateOthers = true;
84};
85
87
88class PL_GAMEENGINE_DLL plStateMachineComponentManager : public plComponentManager<class plStateMachineComponent, plBlockStorageType::Compact>
89{
90public:
93
94 virtual void Initialize() override;
95
96 void Update(const plWorldModule::UpdateContext& context);
97
98private:
99 void ResourceEventHandler(const plResourceEvent& e);
100
101 plHashSet<plComponentHandle> m_ComponentsToReload;
102};
103
105
107class PL_GAMEENGINE_DLL plStateMachineComponent : public plComponent
108{
110
112 // plComponent
113
114public:
115 virtual void SerializeComponent(plWorldWriter& inout_stream) const override;
116 virtual void DeserializeComponent(plWorldReader& inout_stream) override;
117
118protected:
119 virtual void OnActivated() override;
120 virtual void OnDeactivated() override;
121
123 // plStateMachineComponent
124
125public:
129
131
133 plStateMachineInstance* GetStateMachineInstance() { return m_pStateMachineInstance.Borrow(); }
134 const plStateMachineInstance* GetStateMachineInstance() const { return m_pStateMachineInstance.Borrow(); }
135
136 void SetResource(const plStateMachineResourceHandle& hResource);
137 const plStateMachineResourceHandle& GetResource() const { return m_hResource; }
138
139 void SetResourceFile(const char* szFile); // [ property ]
140 const char* GetResourceFile() const; // [ property ]
141
144 void SetInitialState(const char* szName); // [ property ]
145 const char* GetInitialState() const { return m_sInitialState; } // [ property ]
146
148 bool SetState(plStringView sName); // [ scriptable ]
149
151 plStringView GetCurrentState() const; // [ scriptable ]
152
154 void FireTransitionEvent(plStringView sEvent);
155
156 void SetBlackboardName(const char* szName); // [ property ]
157 const char* GetBlackboardName() const { return m_sBlackboardName; } // [ property ]
158
159private:
160 friend class plStateMachineState_SendMsg;
161 void SendStateChangedMsg(plMsgStateMachineStateChanged& msg, plTime delay);
162 void InstantiateStateMachine();
163 void Update();
164
166 plHashedString m_sInitialState;
167 plHashedString m_sBlackboardName;
168
169 plUniquePtr<plStateMachineInstance> m_pStateMachineInstance;
170
171 plEventMessageSender<plMsgStateMachineStateChanged> m_StateChangedSender; // [ event ]
172};
Base class of all component types.
Definition Component.h:25
Definition ComponentManager.h:88
A message sender that sends all messages to the next component derived from plEventMessageHandlerComp...
Definition EventMessage.h:39
Definition HashSet.h:191
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
void Assign(const char(&string)[N])
Assigning a new string from a string constant is a slow operation, but the hash computation can happe...
A component that holds an plStateMachineInstance using the plStateMachineDescription from the resourc...
Definition StateMachineComponent.h:108
plStateMachineInstance * GetStateMachineInstance()
Returns the plStateMachineInstance owned by this component.
Definition StateMachineComponent.h:133
Definition StateMachineComponent.h:89
The state machine instance represents the actual state machine. Typically it is created from a descri...
Definition StateMachine.h:144
A state machine state that sends a plMsgStateMachineStateChanged on state enter or exit to the owner ...
Definition StateMachineComponent.h:29
A state machine state that sets the enabled flag on a game object and disables all other objects in t...
Definition StateMachineComponent.h:69
Base class for a state in a state machine.
Definition StateMachine.h:20
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
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
Base class for all messages that are sent as 'events'.
Definition EventMessage.h:8
Message that is sent by plStateMachineState_SendMsg once the state is entered.
Definition StateMachineComponent.h:8
These events may be sent by a specific plResource or by the plResourceManager.
Definition Declarations.h:22
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
Definition WorldModule.h:33