52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using Syntriax.Engine.Core;
|
|
|
|
namespace Syntriax.Engine.Systems.StateMachine;
|
|
|
|
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;
|
|
|
|
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()
|
|
{
|
|
OnUpdateState();
|
|
OnStateUpdate?.InvokeSafe(this);
|
|
}
|
|
|
|
protected virtual void OnTransitionedToState(IState from) { }
|
|
public void TransitionTo(IState from)
|
|
{
|
|
OnTransitionedToState(from);
|
|
OnStateTransitionedTo?.InvokeSafe(this, from);
|
|
}
|
|
|
|
protected virtual void OnTransitionedFromState(IState to) { }
|
|
public void TransitionFrom(IState to)
|
|
{
|
|
OnTransitionedFromState(to);
|
|
OnStateTransitionedFrom?.InvokeSafe(this, to);
|
|
}
|
|
|
|
public abstract IState? GetNextState();
|
|
}
|