using System.Collections.Generic; namespace Bro.Common.FSM { // 状态处理代理定义 public delegate void StatHandler(); public class SimpleFSM { // 状态机描述 public string Desp { get;set; } // 当前状态 private int curStat { get;set;} // 状态动作表 private Dictionary statTbl = new Dictionary(); public SimpleFSM() { } /// /// 注册状态处理 /// /// /// /// public bool Regist(int stat, StatHandler handle) { if ( null == handle || statTbl.ContainsKey(stat) ) { return false; } statTbl.Add(stat, handle); return true; } /// /// 注册状态处理表 /// /// /// public bool Regist(Dictionary statTbl) { this.statTbl = statTbl; return true; } /// /// 设置当前状态 /// /// public void SetStat(int stat) { this.curStat = stat; } /// /// 获取当前状态 /// /// public int GetStat() { return curStat; } /// /// 状态基运行 /// public void Run() { for (; ; ) { foreach (var item in statTbl) { StatHandler handle = item.Value; if (null == handle) { continue; } if (curStat == item.Key) { handle(); break; } } } } } }