public interface IState
{
string Name { get; }
string Tag { set; get; }
IStateMachine Parent { get; set; }
float Time { get; }
ListTransitons { get; }
void EnterCallback(IState prev);
void ExitCallback(IState next);
void UpdateCallback(float deltaTime);
void LateUpdatecallback(float deltaTime);
void FixedUpdateCallback(float deltaTime);
void AddTransiton(ITransition t);
}
public interface IStateMachine
{
IState CurrentState { get; }
IState DefaultState {set;get; }
void AddState(IState state);
void RemoveState(IState state);
IState GetStateWithTag(string tag);
}
public interface ITransition
{
IState From { get; set; }
IState To {get; set;}
string Name { get; set; }
bool TransitionCallback(); // 过渡时的回调,返回true过渡结束 返回false继续进行过渡
bool ShouldBengin(); }// 能否开始过渡
实例化FSM
public delegate void LOStateDelegate();
public delegate void LOStateDelegateState(IState state); //Enter,Exit的委托
public delegate void LOStateDelegateFloat(float f);//Update LateUpdate FixedUpdate的委托
public class LOState : IState
{
public event LOStateDelegateState OnEnter;
public event LOStateDelegateState OnExit;
public event LOStateDelegateFloat OnUpdate;
public event LOStateDelegateFloat OnLateUpdate;
public event LOStateDelegateFloat OnFixedUpdate;
private string _name;
private string _tag;
private IStateMachine _parent;
private float _timer;
private List_transitons;//状态过度
public string Name { get {return _name;} }
public string Tag { get { return _tag; } set { _tag = value;} }
public IStateMachine Parent { get { return _parent; } set { _parent = value; } }
public float Time { get { return _timer; } }
public ListTransitons { get { return _transitons; } }
public LOState(string name) { _name = name; _transitons = new List();}
public void AddTransiton(ITransition t)
{
if (t!=null&&!_transitons.Contains(t))
{
_transitons.Add(t);
}
}
public virtual void EnterCallback(IState prev)
{
_timer = 0f;
if (OnEnter!=null)
{
OnEnter(prev);
}
}
public virtual void ExitCallback(IState next)
{
_timer = 0f;
if (OnExit!=null)
{
OnExit(next);
}
}
public virtual void UpdateCallback(float deltaTime)
{
_timer += deltaTime;
if (OnUpdate!=null)
{
OnUpdate(deltaTime);
}
}
public virtual void LateUpdatecallback(float deltaTime)
{
if (OnLateUpdate != null)
{
OnLateUpdate(deltaTime);
}
}
public virtual void FixedUpdateCallback(float deltaTime)
{
if (OnFixedUpdate!=null)
{
OnFixedUpdate(deltaTime);
}
}
}
public class LOStateMachine : LOState, IStateMachine
{
private IState _currentState;
private IState _defaultState;
private List_state;//保存状态的容器
private bool _isTransition = false;//是否正在过度
private ITransition _t;
public IState CurrentState { get { return _currentState; } }
public IState DefaultState
{ get { return _defaultState; }
set {
if (_state.Contains(value))
{ AddState(value); _defaultState = value; } }
}
public LOStateMachine(string name,IState defaultState):base(name)
{
_state = new List();
_defaultState = defaultState;
}
public void AddState(IState state)
{
if (state!=null&&!_state.Contains(state))
{ _state.Add(state); state.Parent = this;
if (_defaultState==null)
{ _defaultState = state; } }
}
public void RemoveState(IState state)
{ //在状态机运行的过程中,不能删除当前状态
if (_currentState==state)
{ return; }
if (state!=null&&!_state.Contains(state))
{ _state.Remove(state);
state.Parent = null;
if (_defaultState == state)
{ _defaultState = (_state.Count >= 1) ? _state[0] : null; } }
}
public IState GetStateWithTag(string tag)
{ return null; }
//每帧判断是否需要过渡,如果需要过渡遍历当前状态的过渡条件列表是否达成,达成就实现过渡
public override void UpdateCallback(float deltaTime)
{
if (_isTransition) //是否正在过渡
{
if (_t.TransitionCallback()) //判断过渡是否结束
{
DoTransition(_t); //如果过度结束了 就执行过渡
_isTransition = false;
}
return;
}
base.UpdateCallback(deltaTime);
if (_currentState == null)
{ _currentState = _defaultState; }
List<ITransition> ts=_currentState.Transitions;
int count = _currentState.Transitons.Count;
for (int i = 0; i < CurrentState.Transitons.Count; i++)
{
ITransition t = ts[i];
if (t.ShouldBengin())
{
_isTransition = true;
_t = t;
return;
}
}
_currentState.UpdateCallback(deltaTime); //如果没有满足过渡条件 那就执行当前状态的回调Update
}
public override void LateUpdatecallback(float deltaTime)
{
base.LateUpdatecallback(deltaTime);
_currentState.LateUpdatecallback(deltaTime);
}
public override void FixedUpdateCallback(float deltaTime)
{
base.FixedUpdateCallback(deltaTime);
_currentState.FixedUpdateCallback(deltaTime);
}
private void DoTransition(ITransition t)
{
_currentState.ExitCallback(t.To);
_currentState = t.To;
_currentState.EnterCallback(t.From);
}
}
public delegate bool LOTransitionDelegate();
public class LOTransition : ITransition
{
public event LOTransitionDelegate OnTranesiton;
public event LOTransitionDelegate OnCheck;
private IState _from;
private IState _to;
private string _name;
public IState From
{ get { return _from; } set { _from = value; } } public IState To
{ get { return _to; } set { _to = value; } }
public string Name { get { return _name; } set { _name = value; } } public LOTransition(string name,IState fromState,IState toState)
{ _name = name; _from = fromState; _to = toState; }
////// 检测状态过度是否结束
///public bool TransitionCallback()
{
if (OnTranesiton != null)
{ return OnTranesiton(); } return true; }
public bool ShouldBengin()
{
if (OnCheck != null)
{ return OnCheck(); }
return false; }
}