using System.Collections.Generic; using UnityEngine; using UnityEngine.Windows.Speech;
public enum StateType { idle, run, jump }
public class FSM : MonoBehaviour { //当前状态 private IState currentIState; //字典存储状态 public Dictionary<StateType, IState> states; public FSM() { //设置默认状态 this.states = new Dictionary<StateType, IState>(); }
//添加状态 public void AddState(StateType state, IState istate) { if (states.ContainsKey(state)) { Debug.Log("请勿重复添加"); } states.Add(state, istate); } //转换状态 public void TransformState(StateType state) { if (states.ContainsKey(state)) { Debug.Log("已经转换到此状态了!"); } if (currentIState != null) { currentIState.Exit(); } currentIState = states[state]; currentIState.Enter(); }