From bd5eb432b7ed8ae281ffb92e1241bfe4c420d0ad Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 30 Apr 2025 19:20:45 +0300 Subject: [PATCH] feat: serialized state machine & states --- Engine.Systems/StateMachine/IState.cs | 6 +++--- Engine.Systems/StateMachine/State.cs | 18 ++++++++++++++++-- .../StateMachine/StateBehaviourBase.cs | 16 +++++++++++++++- Engine.Systems/StateMachine/StateMachine.cs | 2 ++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Engine.Systems/StateMachine/IState.cs b/Engine.Systems/StateMachine/IState.cs index b3bf3bf..e3bb49e 100644 --- a/Engine.Systems/StateMachine/IState.cs +++ b/Engine.Systems/StateMachine/IState.cs @@ -1,14 +1,14 @@ +using Syntriax.Engine.Core; + namespace Syntriax.Engine.Systems.StateMachine; -public interface IState +public interface IState : IEntity, INameable { event StateUpdateEventHandler? OnStateUpdate; event StateTransitionedFromEventHandler? OnStateTransitionedFrom; event StateTransitionedToEventHandler? OnStateTransitionedTo; event StateTransitionReadyEventHandler? OnStateTransitionReady; - string Name { get; } - IState? GetNextState(); void Update(); diff --git a/Engine.Systems/StateMachine/State.cs b/Engine.Systems/StateMachine/State.cs index 6f0583a..2afdff0 100644 --- a/Engine.Systems/StateMachine/State.cs +++ b/Engine.Systems/StateMachine/State.cs @@ -2,19 +2,33 @@ using Syntriax.Engine.Core; namespace Syntriax.Engine.Systems.StateMachine; -public class State : IState +public class State : BaseEntity, IState { public event IState.StateUpdateEventHandler? OnStateUpdate = null; public event IState.StateTransitionedFromEventHandler? OnStateTransitionedFrom = null; public event IState.StateTransitionedToEventHandler? OnStateTransitionedTo = null; public event IState.StateTransitionReadyEventHandler? OnStateTransitionReady = null; + public event INameable.NameChangedEventHandler? OnNameChanged = null; private readonly List transitions = []; private readonly Dictionary possibleTransitions = []; + private string _name = "Default State Name"; - public string Name { get; set; } = "Default State Name"; public IReadOnlyList Transitions => transitions; public IReadOnlyDictionary PossibleTransitions => possibleTransitions; + public string Name + { + get => _name; + set + { + if (_name.CompareTo(value) == 0) + return; + + string previousName = _name; + _name = value; + OnNameChanged?.Invoke(this, previousName); + } + } public void RemoveTransition(string name) { diff --git a/Engine.Systems/StateMachine/StateBehaviourBase.cs b/Engine.Systems/StateMachine/StateBehaviourBase.cs index a33ebbe..e550a87 100644 --- a/Engine.Systems/StateMachine/StateBehaviourBase.cs +++ b/Engine.Systems/StateMachine/StateBehaviourBase.cs @@ -7,10 +7,24 @@ public abstract class StateBehaviourBase : Behaviour, IState public event IState.StateUpdateEventHandler? OnStateUpdate = null; public event IState.StateTransitionedFromEventHandler? OnStateTransitionedFrom = null; public event IState.StateTransitionedToEventHandler? OnStateTransitionedTo = null; + public event INameable.NameChangedEventHandler? OnNameChanged = null; public abstract event IState.StateTransitionReadyEventHandler? OnStateTransitionReady; - public abstract string Name { get; } + private string _name = string.Empty; + public string Name + { + get => _name; + set + { + if (_name.CompareTo(value) == 0) + return; + + string previousName = _name; + _name = value; + OnNameChanged?.Invoke(this, previousName); + } + } protected virtual void OnUpdateState() { } public void Update() diff --git a/Engine.Systems/StateMachine/StateMachine.cs b/Engine.Systems/StateMachine/StateMachine.cs index 02fb3a7..8c262e5 100644 --- a/Engine.Systems/StateMachine/StateMachine.cs +++ b/Engine.Systems/StateMachine/StateMachine.cs @@ -1,4 +1,5 @@ using Syntriax.Engine.Core; +using Syntriax.Engine.Core.Serialization; namespace Syntriax.Engine.Systems.StateMachine; @@ -7,6 +8,7 @@ public class StateMachine : Behaviour public event StateChangedEventHandler? OnStateChanged = null; private IState _state = new State(); + [Serialize] public IState State { get => _state;