记录一下FSM 备用
IState.cs
using UnityEngine;
using System.Collections;
public interface IState
{
/// <summary>
/// 获取这个状态机的状态
/// </summary>
/// <returns></returns>
uint GetStateID();
/// <summary>
/// 进入这个状态
/// </summary>
/// <param name="machine">状态机</param>
/// <param name="prevState">上一个状态</param>
/// <param name="param1">参数1</param>
/// <param name="param2">参数2</param>
void OnEnter(StateMachine machine,IState prevState,object param1,object param2);
/// <summary>
/// 离开当前这个状态
/// </summary>
/// <param name="nextState">下一个要进入的状态</param>
/// <param name="param1">参数1</param>
/// <param name="param2">参数2</param>
void OnLeave(IState nextState,object param1,object param2);
void OnUpdate();
void OnFixedUpdate();
void OnleteUpdate();
}
StateMachine.cs
新建一个C#脚本。StateMachine.cs
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
/// <summary>
/// 1.当前状态
/// 2.注册一个状态
/// 3.移除一个状态
/// 4.获取一个状态
/// 5.停止当前状态
/// 6.切换状态的回调
/// 7.切换状态回调委托
/// 8.切换状态
/// 9.判断当前状态是否是某个状态
/// </summary>
public class StateMachine
{
/// <summary>
/// 所有的状态集合
/// </summary>
private Dictionary<uint, IState> mStateDic = null;
/// <summary>
/// 当前状态
/// </summary>
private IState mCurrentState = null;
public IState CurrentState
{
get
{
return mCurrentState;
}
}
/// <summary>
/// 当前状态id
/// </summary>
public uint CurrentID
{
get
{
return mCurrentState == null ? 0 : mCurrentState.GetStateID();
}
}
public StateMachine()
{
mStateDic = new Dictionary<uint, IState>();
mCurrentState = null;
}
/// <summary>
/// 注册一个状态
/// </summary>
/// <param name="state">状态对象</param>
/// <returns>成功还是失败</returns>
public bool RegisterState(IState state)
{
if (state == null)
{
Debug.LogError("StateMachine.RegisterState state is Null!");
return false;
}
if (mStateDic.ContainsKey(state.GetStateID()))
{
Debug.LogError("StateMachine.RegisterState mStateDic hava this key key = " + state.GetStateID());
return false;
}
mStateDic.Add(state.GetStateID(), state);
return true;
}
/// <summary>
/// 移除一个状态
/// </summary>
/// <param name="stateId">状态id</param>
/// <returns>当状态不存在或者状态正在运行那么返回失败</returns>
public bool RemoveState(uint stateId)
{
if (!mStateDic.ContainsKey(stateId))
{
return false;
}
if (mCurrentState !=null && mCurrentState.GetStateID() == stateId)
{
return false;
}
mStateDic.Remove(stateId);
return true;
}
/// <summary>
/// 获取一个状态
/// </summary>
/// <param name="stateId">状态ID</param>
/// <returns></returns>
public IState GetState(uint stateId)
{
IState state = null;
mStateDic.TryGetValue(stateId, out state);
return state;
}
/// <summary>
/// 停止当前状态
/// </summary>
/// <param name="param1">参数1</param>
/// <param name="param2">参数2</param>
public void StopState(object param1,object param2)
{
if (mCurrentState == null)
{
return;
}
mCurrentState.OnLeave(null,param1,param2);
mCurrentState = null;
}
/// <summary>
/// 切换状态的回调
/// </summary>
public BetweenSwitchState BetweenSwitchStateCallBack = null;
/// <summary>
/// 切换状态回调委托
/// </summary>
/// <param name="from">当前状态</param>
/// <param name="to">要跳转的状态</param>
/// <param name="param1">参数1</param>
/// <param name="param2">参数2</param>
public delegate void BetweenSwitchState(IState from,IState to,object param1,object param2);
/// <summary>
/// 切换状态
/// </summary>
/// <param name="newStateId">要切换的状态id</param>
/// <param name="param1">参数1</param>
/// <param name="param2">参数2</param>
/// <returns>如果不存在这个状态或者当前状态等于要切换的那个状态 返回失败</returns>
public bool SwitchState(uint newStateId,object param1,object param2)
{
if (mCurrentState != null && mCurrentState.GetStateID() == newStateId)
{
return false;
}
IState newState = null;
mStateDic.TryGetValue(newStateId,out newState);
if (newState == null)
{
return false;
}
if (mCurrentState != null)
{
mCurrentState.OnLeave(newState,param1,param2);
}
IState oldState = mCurrentState;
mCurrentState = newState;
if (BetweenSwitchStateCallBack != null)
{
BetweenSwitchStateCallBack(oldState, mCurrentState, param1, param2);
}
newState.OnEnter(this,oldState,param1,param2);
return true;
}
/// <summary>
/// 判断当前状态是不是某个状态
/// </summary>
/// <param name="stateId">状态id</param>
/// <returns>是?不是</returns>
public bool IsInState(uint stateId)
{
return mCurrentState == null ? false : mCurrentState.GetStateID() == stateId;
}
public void OnUpdate()
{
if (mCurrentState != null)
{
mCurrentState.OnUpdate();
}
}
public void OnFixedUpdate()
{
if (mCurrentState != null)
{
mCurrentState.OnFixedUpdate();
}
}
public void OnLeteUpdate()
{
if (mCurrentState != null)
{
mCurrentState.OnleteUpdate();
}
}
}