add project files
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user