feat: serialized state machine & states
This commit is contained in:
parent
d2ca85568f
commit
bd5eb432b7
@ -1,14 +1,14 @@
|
|||||||
|
using Syntriax.Engine.Core;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Systems.StateMachine;
|
namespace Syntriax.Engine.Systems.StateMachine;
|
||||||
|
|
||||||
public interface IState
|
public interface IState : IEntity, INameable
|
||||||
{
|
{
|
||||||
event StateUpdateEventHandler? OnStateUpdate;
|
event StateUpdateEventHandler? OnStateUpdate;
|
||||||
event StateTransitionedFromEventHandler? OnStateTransitionedFrom;
|
event StateTransitionedFromEventHandler? OnStateTransitionedFrom;
|
||||||
event StateTransitionedToEventHandler? OnStateTransitionedTo;
|
event StateTransitionedToEventHandler? OnStateTransitionedTo;
|
||||||
event StateTransitionReadyEventHandler? OnStateTransitionReady;
|
event StateTransitionReadyEventHandler? OnStateTransitionReady;
|
||||||
|
|
||||||
string Name { get; }
|
|
||||||
|
|
||||||
IState? GetNextState();
|
IState? GetNextState();
|
||||||
|
|
||||||
void Update();
|
void Update();
|
||||||
|
@ -2,19 +2,33 @@ using Syntriax.Engine.Core;
|
|||||||
|
|
||||||
namespace Syntriax.Engine.Systems.StateMachine;
|
namespace Syntriax.Engine.Systems.StateMachine;
|
||||||
|
|
||||||
public class State : IState
|
public class State : BaseEntity, IState
|
||||||
{
|
{
|
||||||
public event IState.StateUpdateEventHandler? OnStateUpdate = null;
|
public event IState.StateUpdateEventHandler? OnStateUpdate = null;
|
||||||
public event IState.StateTransitionedFromEventHandler? OnStateTransitionedFrom = null;
|
public event IState.StateTransitionedFromEventHandler? OnStateTransitionedFrom = null;
|
||||||
public event IState.StateTransitionedToEventHandler? OnStateTransitionedTo = null;
|
public event IState.StateTransitionedToEventHandler? OnStateTransitionedTo = null;
|
||||||
public event IState.StateTransitionReadyEventHandler? OnStateTransitionReady = null;
|
public event IState.StateTransitionReadyEventHandler? OnStateTransitionReady = null;
|
||||||
|
public event INameable.NameChangedEventHandler? OnNameChanged = null;
|
||||||
|
|
||||||
private readonly List<StateTransition> transitions = [];
|
private readonly List<StateTransition> transitions = [];
|
||||||
private readonly Dictionary<string, StateTransition> possibleTransitions = [];
|
private readonly Dictionary<string, StateTransition> possibleTransitions = [];
|
||||||
|
private string _name = "Default State Name";
|
||||||
|
|
||||||
public string Name { get; set; } = "Default State Name";
|
|
||||||
public IReadOnlyList<StateTransition> Transitions => transitions;
|
public IReadOnlyList<StateTransition> Transitions => transitions;
|
||||||
public IReadOnlyDictionary<string, StateTransition> PossibleTransitions => possibleTransitions;
|
public IReadOnlyDictionary<string, StateTransition> 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)
|
public void RemoveTransition(string name)
|
||||||
{
|
{
|
||||||
|
@ -7,10 +7,24 @@ public abstract class StateBehaviourBase : Behaviour, IState
|
|||||||
public event IState.StateUpdateEventHandler? OnStateUpdate = null;
|
public event IState.StateUpdateEventHandler? OnStateUpdate = null;
|
||||||
public event IState.StateTransitionedFromEventHandler? OnStateTransitionedFrom = null;
|
public event IState.StateTransitionedFromEventHandler? OnStateTransitionedFrom = null;
|
||||||
public event IState.StateTransitionedToEventHandler? OnStateTransitionedTo = null;
|
public event IState.StateTransitionedToEventHandler? OnStateTransitionedTo = null;
|
||||||
|
public event INameable.NameChangedEventHandler? OnNameChanged = null;
|
||||||
|
|
||||||
public abstract event IState.StateTransitionReadyEventHandler? OnStateTransitionReady;
|
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() { }
|
protected virtual void OnUpdateState() { }
|
||||||
public void Update()
|
public void Update()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Syntriax.Engine.Core;
|
using Syntriax.Engine.Core;
|
||||||
|
using Syntriax.Engine.Core.Serialization;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Systems.StateMachine;
|
namespace Syntriax.Engine.Systems.StateMachine;
|
||||||
|
|
||||||
@ -7,6 +8,7 @@ public class StateMachine : Behaviour
|
|||||||
public event StateChangedEventHandler? OnStateChanged = null;
|
public event StateChangedEventHandler? OnStateChanged = null;
|
||||||
|
|
||||||
private IState _state = new State();
|
private IState _state = new State();
|
||||||
|
[Serialize]
|
||||||
public IState State
|
public IState State
|
||||||
{
|
{
|
||||||
get => _state;
|
get => _state;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user