最近一直在实习,忙于公司一个c++ 项目 ,一直想写博客来着,没时间写今天熬夜打算先献上自己前几天自己封装的一个fsm状态机
话不多说,直接上正题,这篇博客主要是在学校的时候状态机一直使用的是player maker插件或者是自己使用if else (switch case)来实现自己的状态机
所谓的状态机不过是一种数据结构,可以遵循一定的顺序的状态循环而已 (PS:自己能力有限,有不对的地方欢迎大家指出)
public delegate void FsmEvent(); public class MyState<T> { private T myState; public string name; public MyState(string name,T state) { this.name = name; myState = state; } public Dictionary<T, MyFSMTranslation<T>> TranslationDict = new Dictionary<T, MyFSMTranslation<T>>(); public event FsmEvent DoBeforeEntering; public void DoBeforeEntery() { if (DoBeforeEntering != null) { DoBeforeEntering(); } else { Debug.Log("没有添加事件"); } } public event FsmEvent DoBeforeLeaving; public void DoBeforeLeave() { if (DoBeforeLeaving != null) { DoBeforeLeaving(); } else { Debug.Log("没有绑定事件"); } } public event FsmEvent Act; public void DoAct() { if (Act != null) { Act(); } else { Debug.Log("没有绑定事件"); } } }
解释一下自己的代码,FsmEvent是自定义的fsm的事件,MyState<T> 是泛型的状态类,包含了状态和三个事件,分别为第一次进入,一直执行,退出
1 public class MyFSMTranslation<T> 2 { 3 public MyState<T> fromState; 4 /// <summary> 5 /// 转换状态的名字 6 /// </summary> 7 public string name; 8 public MyState<T> toState; 9 10 /// <summary> 11 /// 状态跳转 12 /// </summary> 13 /// <param name="fromState"> 当前状态</param> 14 /// <param name="name"> 状态跳转的名字</param> 15 /// <param name="toState"> 下一个要进入的状态</param> 16 public MyFSMTranslation(MyState<T> fromState, MyState<T> toState, string name = null) 17 { 18 fromState = fromState; 19 toState = toState; 20 name = name; 21 } 22 }
这是一个状态跳转类注释很详细就不解释了
重点到了
1 public class FSMS<T> 2 { 3 4 private T myFsmType; 5 public T FsmType 6 { 7 get 8 { 9 return myFsmType; 10 } 11 12 } 13 14 public string StateName 15 { 16 get 17 { 18 return StateDict[myFsmType].name; 19 } 20 } 21 22 23 // 状态存贮 24 Dictionary<T, MyState<T>> StateDict = new Dictionary<T, MyState<T>>(); 25 26 27 /// <summary> 28 /// 添加状态 29 /// </summary> 30 /// <param name="state">State.</param> 31 public void AddState(T stateType,string name,MyState<T> State) 32 { 33 StateDict[stateType] = State; 34 } 35 36 37 /// <summary> 38 /// 添加跳转 39 /// </summary> 40 public void AddTranslation(T fromState, MyState<T> toState,string name) 41 { 42 StateDict[fromState].TranslationDict[fromState] = new MyFSMTranslation<T>(StateDict[fromState], toState,name); 43 } 44 45 public void AddTranslation(T fromState, MyState<T> toState) 46 { 47 StateDict[fromState].TranslationDict[fromState] = new MyFSMTranslation<T>(StateDict[fromState], toState); 48 } 49 50 51 52 public bool Translate(T nextType) 53 { 54 MyState<T> nextstate; 55 if (StateDict.ContainsKey(nextType)) 56 { 57 nextstate = StateDict[nextType]; 58 } 59 else 60 { 61 return false; 62 } 63 if (StateDict[myFsmType].TranslationDict.ContainsKey(nextType)) 64 { 65 66 //当期状态转换之后执行的函数 67 //当期状态转换 68 //下一个状态需要执行的函数 69 //改变类里面的状态 70 StateDict[myFsmType].DoBeforeLeave(); 71 myFsmType = nextType; 72 nextstate.DoBeforeEntery(); 73 74 } 75 else 76 { 77 return false; 78 } 79 80 return true; 81 } 82 83 /// <summary> 84 /// 执行当前状态的方法 85 /// </summary> 86 public void Carry() 87 { 88 MyState<T> nextstate = StateDict[myFsmType]; 89 nextstate.DoAct(); 90 } 91 92 93 /// <summary> 94 /// 启动状态机 95 /// </summary> 96 public void Start(T fsmtype) 97 { 98 myFsmType = fsmtype; 99 } 100 101 /// <summary> 102 /// 清空状态机 103 /// </summary> 104 public void Clear() 105 { 106 StateDict.Clear(); 107 } 108 109 }
上面都有注释,就不在解释了,大家感觉有不对的地方欢迎指出
时间: 2024-10-10 04:45:10