Files
CourierOld/Game/CourierOld/Assets/Code/StateMachine/AbstractStateMachine.cs
T

75 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Code.StateMachine
{
public abstract class AbstractStateMachine<TStateEnum, TStateMachineContext>
: MonoBehaviour
where TStateMachineContext : new()
{
public TStateEnum CurrentState { get; protected set; }
public BaseState<TStateMachineContext> CurrentBaseState { get; protected set; }
private BaseState<TStateMachineContext> _previousBaseState;
protected Dictionary<TStateEnum, BaseState<TStateMachineContext>> States = new();
public TStateEnum AllowedStates { get; set; }
public TStateMachineContext StateMachineContext { get; protected set; } = new();
protected virtual void Update()
{
CurrentBaseState.Tick();
}
protected void LateUpdate()
{
CurrentBaseState.LateTick();
}
public abstract void Initialize();
// Костыли 2 шт. для флагового енама в шаблонах
protected abstract bool CheckTransitionAllowed(TStateEnum nextState);
protected abstract void ResetAllowedTransitions();
// TODO Если в EnterState вызывается ExitState, то будет гарантированная поломка
public virtual AbstractStateMachine<TStateEnum, TStateMachineContext> SwitchState(TStateEnum nextState)
{
if (!CheckTransitionAllowed(nextState))
{
return null;
}
ResetAllowedTransitions();
_previousBaseState = CurrentBaseState;
CurrentBaseState = States[nextState];
if (_previousBaseState != null)
{
_previousBaseState.OnExited += CurrentBaseState.EnterState;
_previousBaseState.OnExited += FinishBaseStateTransition;
_previousBaseState.ExitState();
}
else
{
CurrentBaseState.EnterState();
}
CurrentState = nextState;
return this;
}
protected virtual void FinishBaseStateTransition()
{
_previousBaseState.OnExited -= CurrentBaseState.EnterState;
_previousBaseState.OnExited -= FinishBaseStateTransition;
_previousBaseState = null;
}
}
}