状态管理类
- 有限状态机用来管理状态,在某一时刻只能处于一个状态
public class Fsm
{
private StateID currentStateId;
public StateID CurrentStateId { get { return currentStateId; } }
private FsmState currentState;
public FsmState CurrentState { get { return currentState; } }
public Fsm()
{
fsmList = new List<FsmState>();
}
private List<FsmState> fsmList;
public void AddState(FsmState fsmState)
{
if(fsmList.Contains(fsmState))
{
Debug.LogError("已经有该状态了");
return;
}
if(fsmList.Count==0)
{
currentState = fsmState;
currentStateId = fsmState.MStateID;
}
fsmList.Add(fsmState);
}
public void RemoveState(FsmState fsmState)
{
if (!fsmList.Contains(fsmState))
{
Debug.LogError("没有该状态");
return;
}
fsmList.Remove(fsmState);
}
public void MakeTransition(Transition transition)
{
currentStateId=currentState.MakeTransition(transition);
foreach(var state in fsmList)
{
if(state.MStateID==currentStateId)
{
currentState.OnExit();
currentState = state;
currentState.OnExit();
}
}
}
}
fsm一般为具体的角色或者敌人持有,构建状态机,一个Fsm的对象一般由具体的角色持有,当切换状态时,调用fsm.MakeTransition()进行状态切换.
|