49 lines
901 B
C#
49 lines
901 B
C#
using UnityEngine;
|
|
|
|
namespace OHM.UnityToolkit.FSM
|
|
{
|
|
public class FiniteStateMachine<StateType> where StateType : StateBase<StateType>
|
|
{
|
|
private float _currentStateStartTime;
|
|
|
|
public StateType CurrentState { get; private set; }
|
|
|
|
public void SetState(StateType state)
|
|
{
|
|
if (state == null)
|
|
{
|
|
return;
|
|
}
|
|
if (CurrentState != null)
|
|
{
|
|
CurrentState.Leave();
|
|
}
|
|
_currentStateStartTime = Time.time;
|
|
CurrentState = state;
|
|
state.Enter();
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (CurrentState == null)
|
|
{
|
|
return;
|
|
}
|
|
CurrentState.Update();
|
|
if (CurrentState == null || CurrentState.NextState == null)
|
|
{
|
|
return;
|
|
}
|
|
if (Time.time - _currentStateStartTime >= CurrentState.MaxDuration)
|
|
{
|
|
SetState(CurrentState.NextState as StateType);
|
|
}
|
|
}
|
|
|
|
public float GetCurrentStateDuration()
|
|
{
|
|
return Time.time - _currentStateStartTime;
|
|
}
|
|
}
|
|
}
|