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<int, StatHandler> statTbl = new Dictionary<int,StatHandler>();
|
|
public SimpleFSM()
|
{
|
|
}
|
|
/// <summary>
|
/// 注册状态处理
|
/// </summary>
|
/// <param name="stat"></param>
|
/// <param name="handle"></param>
|
/// <returns></returns>
|
public bool Regist(int stat, StatHandler handle)
|
{
|
if ( null == handle || statTbl.ContainsKey(stat) )
|
{
|
return false;
|
}
|
|
statTbl.Add(stat, handle);
|
|
return true;
|
}
|
|
/// <summary>
|
/// 注册状态处理表
|
/// </summary>
|
/// <param name="statTbl"></param>
|
/// <returns></returns>
|
public bool Regist(Dictionary<int, StatHandler> statTbl)
|
{
|
this.statTbl = statTbl;
|
|
return true;
|
}
|
|
/// <summary>
|
/// 设置当前状态
|
/// </summary>
|
/// <param name="stat"></param>
|
public void SetStat(int stat)
|
{
|
this.curStat = stat;
|
}
|
|
/// <summary>
|
/// 获取当前状态
|
/// </summary>
|
/// <returns></returns>
|
public int GetStat()
|
{
|
return curStat;
|
}
|
|
/// <summary>
|
/// 状态基运行
|
/// </summary>
|
public void Run()
|
{
|
for (; ; )
|
{
|
foreach (var item in statTbl)
|
{
|
StatHandler handle = item.Value;
|
if (null == handle)
|
{
|
continue;
|
}
|
|
if (curStat == item.Key)
|
{
|
handle();
|
break;
|
}
|
}
|
}
|
|
}
|
}
|
}
|