Wikipedia : A finite state machine (English: finite-state machine, abbreviation: FSM), also known as a finite state automaton, or a state machine for short, is a mathematical model that represents a finite number of states and behaviors such as transitions and actions between these states.
This article refers to the StateMachine in quick-cocos to implement the Unity version of the finite state machine.
Implementation ideas:
- Create a state machine object
public class StateMachine : MonoBehaviour {
...
}
- Initialize state machine information, including events and callback functions.
// 事件
public class SMEvent
{
readonly string name;
readonly List<string> froms;
readonly string to;
public string Name { get { return name; } }
public List<string> Froms { get { return froms; } }
public string To { get { return to; } }
public SMEvent(string _name, List<string> _froms, string _to)
{
name = _name;
froms = _froms;
to = _to;
}
}
// 回调函数
Dictionary<string, Func<SMEvent, bool>> _callbacks;
- Switching states is achieved by executing events.
public int DoEvent(string name){ ... }
- Conditional judgment of the execution event
public bool IsReady(){...} :返回状态机是否就绪
public string GetState(){...} :返回当前状态
public bool IsState(string state){...} :判断当前状态是否是参数state状态
public bool CanDoEvent(string eventName){...}:当前状态如果能完成eventName对应的event的状态转换,则返回true
public bool CanNotDoEvent(string eventName){...} :当前状态如果不能完成eventName对应的event的状态转换,则返回true
public bool IsFinishedState(){...}:当前状态如果是最终状态,则返回true
- execute callback function
// 在事件EVENT开始前被激活
private bool BeforeThisEvent(SMEvent smEvent) {...}
// 在离开旧状态STATE时被激活
private bool LeaveThisEvent(SMEvent smEvent) {...}
// 在进入新状态STATE时被激活
private bool EnterThisEvent(SMEvent smEvent) {...}
// 在事件EVENT结束后被激活
private bool AfterThisEvent(SMEvent smEvent) {...}
// 在任何事件开始前被激活
private bool BeforeAnyEvent(SMEvent smEvent) {...}
// 在任何事件结束后被激活
private bool AfterAnyEvent(SMEvent smEvent) {...}
// 在进入任何状态时被激活
private bool EnterAnyState(SMEvent smEvent) {...}
// 在离开任何状态时被激活
private bool LeaveAnyState(SMEvent smEvent) {...}
// 当状态发生改变的时候被激活
private bool ChangeAnyState(SMEvent smEvent) {...}
Example of use:
// 创建一个状态机实例
StateMachine fsm = gameObject.AddComponent<StateMachine>();
// 添加事件和状态
List<SMEvent> events = new List<SMEvent>();
events.Add(new SMEvent("move", new List<string> { "idle", "jump"}, "walk"));
events.Add(new SMEvent("attack", new List<string> { "idle", "walk" }, "jump"));
events.Add(new SMEvent("normal", new List<string> { "walk", "jump"}, "idle"));
events.Add(new SMEvent("lie", new List<string> { "move", "idle"}, "falldown"));
// 添加回调函数
Func<SMEvent, bool> onenteridle = x => Walk(x);
Func<SMEvent, bool> onenterwalk = x => Jump(x);
Func<SMEvent, bool> onenterjump = x => Idle(x);
Func<SMEvent, bool> onenterlie = x => FallDown(x);
Dictionary<string, Func<SMEvent, bool>> callbacks = new Dictionary<string, Func<SMEvent, bool>>();
callbacks.Add("onenternormal", onenteridle);
callbacks.Add("onentermove", onenterwalk);
callbacks.Add("onenterattack", onenterjump);
callbacks.Add("onenterlie", onenterlie);
// 初始化状态机
string initial = "idle";
fsm.SetupState(events, callbacks, initial, "", false);;
// 回调
private bool Walk(SMEvent smEvent) {
Debug.LogError("Walk");
return true;
}
private bool Jump(SMEvent smEvent)
{
Debug.LogError("Jump");
return true;
}
private bool Idle(SMEvent smEvent)
{
Debug.LogError("Idle");
return true;
}
private bool FallDown(SMEvent smEvent)
{
Debug.LogError("FallDown");
return true;
}
Instance address:
https://github.com/MagicDavid20/UnityFSM/tree/master/fsm
other:
- State and event names can be implemented through configuration tables or enumerations
- The specific implementation of Walk, Jump, etc. can be realized through the state mode, moving the pointer address.
If there are any mistakes, please point them out.
email:dxmdxm1992#gmail.com