有限状态机 Finite State Machine(FSM)
生活随笔
收集整理的這篇文章主要介紹了
有限状态机 Finite State Machine(FSM)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
首先新建一個(gè)FSMstate和? FSMSystem 類 用來添加狀態(tài)和轉(zhuǎn)換條件
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /** A Finite State Machine System based on Chapter 3.1 of Game Programming Gems 1 by Eric Dybsand Written by Roberto Cezar Bianchini, July 2010 How to use:1. Place the labels for the transitions and the states of the Finite State Systemin the corresponding enums.2. Write new class(es) inheriting from FSMState and fill each one with pairs (transition-state).These pairs represent the state S2 the FSMSystem should be if while being on state S1, atransition T is fired and state S1 has a transition from it to S2. Remember this is a Deterministic FSM. You can't have one transition leading to two different states.Method Reason is used to determine which transition should be fired.You can write the code to fire transitions in another place, and leave this method empty if youfeel it's more appropriate to your project.Method Act has the code to perform the actions the NPC is supposed do if it's on this state.You can write the code for the actions in another place, and leave this method empty if youfeel it's more appropriate to your project.3. Create an instance of FSMSystem class and add the states to it.4. Call Reason and Act (or whichever methods you have for firing transitions and making the NPCsbehave in your game) from your Update or FixedUpdate methods.Asynchronous transitions from Unity Engine, like OnTriggerEnter, SendMessage, can also be used,just call the Method PerformTransition from your FSMSystem instance with the correct Transitionwhen the event occurs.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /// <summary> /// Place the labels for the Transitions in this enum. /// Don't change the first label, NullTransition as FSMSystem class uses it. ///當(dāng)前的行為 /// </summary> public enum Transition {NullTransition = 0, // Use this transition to represent a non-existing transition in your systemIdleTrans=1,//Idle動(dòng)畫RunTrasns=2, } /// <summary> /// Place the labels for the States in this enum. /// Don't change the first label, NullTransition as FSMSystem class uses it. /// 要執(zhí)行的動(dòng)作 /// </summary> public enum StateID {NullStateID = 0, // Use this ID to represent a non-existing State in your systemIdleID=1,//IdleIdRunID=2, } /// <summary> /// This class represents the States in the Finite State System. /// Each state has a Dictionary with pairs (transition-state) showing /// which state the FSM should be if a transition is fired while this state /// is the current state. /// Method Reason is used to determine which transition should be fired . /// Method Act has the code to perform the actions the NPC is supposed do if it's on this state. /// </summary> public abstract class FSMState {protected Dictionary<Transition, StateID> map = new Dictionary<Transition, StateID>();protected StateID stateID;public StateID ID { get { return stateID; } }public void AddTransition(Transition trans, StateID id){// Check if anyone of the args is invalidif (trans == Transition.NullTransition){Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real transition");return;}if (id == StateID.NullStateID){Debug.LogError("FSMState ERROR: NullStateID is not allowed for a real ID");return;}// Since this is a Deterministic FSM,// check if the current transition was already inside the mapif (map.ContainsKey(trans)){Debug.LogError("FSMState ERROR: State " + stateID.ToString() + " already has transition " + trans.ToString() +"Impossible to assign to another state");return;}map.Add(trans, id);}/// <summary>/// This method deletes a pair transition-state from this state's map./// If the transition was not inside the state's map, an ERROR message is printed./// </summary>public void DeleteTransition(Transition trans){// Check for NullTransitionif (trans == Transition.NullTransition){Debug.LogError("FSMState ERROR: NullTransition is not allowed");return;}// Check if the pair is inside the map before deletingif (map.ContainsKey(trans)){map.Remove(trans);return;}Debug.LogError("FSMState ERROR: Transition " + trans.ToString() + " passed to " + stateID.ToString() +" was not on the state's transition list");}/// <summary>/// This method returns the new state the FSM should be if/// this state receives a transition and/// </summary>public StateID GetOutputState(Transition trans){// Check if the map has this transitionif (map.ContainsKey(trans)){return map[trans];}return StateID.NullStateID;}/// <summary>/// This method is used to set up the State condition before entering it./// It is called automatically by the FSMSystem class before assigning it/// to the current state./// </summary>public virtual void DoBeforeEntering() { }/// <summary>/// This method is used to make anything necessary, as reseting variables/// before the FSMSystem changes to another one. It is called automatically/// by the FSMSystem before changing to a new state./// </summary>public virtual void DoBeforeLeaving() { }/// <summary>/// This method decides if the state should transition to another on its list/// NPC is a reference to the object that is controlled by this class/// 這個(gè)方法決定狀態(tài)是否應(yīng)該轉(zhuǎn)換到列表NPC中的另一個(gè)狀態(tài),NPC是對(duì)該類控制的對(duì)象的引用/// </summary>public abstract void Reason(GameObject player, GameObject npc);/// <summary>/// This method controls the behavior of the NPC in the game World./// Every action, movement or communication the NPC does should be placed here/// NPC is a reference to the object that is controlled by this class///該方法控制NPC在游戲世界中的行為。NPC所做的每一個(gè)動(dòng)作、動(dòng)作或通信都應(yīng)該放在這里NPC是對(duì)這個(gè)類控制的對(duì)象的引用/// </summary>public abstract void Act(GameObject player, GameObject npc); } // class FSMState/// <summary> /// FSMSystem class represents the Finite State Machine class. /// It has a List with the States the NPC has and methods to add, /// delete a state, and to change the current state the Machine is on. /// </summary> public class FSMSystem {private List<FSMState> states;// The only way one can change the state of the FSM is by performing a transition// Don't change the CurrentState directlyprivate StateID currentStateID;public StateID CurrentStateID { get { return currentStateID; } }private FSMState currentState;public FSMState CurrentState { get { return currentState; } }public FSMSystem(){states = new List<FSMState>();}/// <summary>/// This method places new states inside the FSM,/// or prints an ERROR message if the state was already inside the List./// First state added is also the initial state./// </summary>public void AddState(FSMState s){// Check for Null reference before deletingif (s == null){Debug.LogError("FSM ERROR: Null reference is not allowed");}// First State inserted is also the Initial state,// the state the machine is in when the simulation beginsif (states.Count == 0){states.Add(s);currentState = s;currentStateID = s.ID;return;}// Add the state to the List if it's not inside itforeach (FSMState state in states){if (state.ID == s.ID){Debug.LogError("FSM ERROR: Impossible to add state " + s.ID.ToString() +" because state has already been added");return;}}states.Add(s);}/// <summary>/// This method delete a state from the FSM List if it exists,/// or prints an ERROR message if the state was not on the List./// </summary>public void DeleteState(StateID id){// Check for NullState before deletingif (id == StateID.NullStateID){Debug.LogError("FSM ERROR: NullStateID is not allowed for a real state");return;}// Search the List and delete the state if it's inside itforeach (FSMState state in states){if (state.ID == id){states.Remove(state);return;}}Debug.LogError("FSM ERROR: Impossible to delete state " + id.ToString() +". It was not on the list of states");}/// <summary>/// This method tries to change the state the FSM is in based on/// the current state and the transition passed. If current state/// doesn't have a target state for the transition passed,/// an ERROR message is printed./// </summary>public void PerformTransition(Transition trans){// Check for NullTransition before changing the current stateif (trans == Transition.NullTransition){Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition");return;}// Check if the currentState has the transition passed as argumentStateID id = currentState.GetOutputState(trans);if (id == StateID.NullStateID){Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " +" for transition " + trans.ToString());return;}// Update the currentStateID and currentStatecurrentStateID = id;foreach (FSMState state in states){if (state.ID == currentStateID){// Do the post processing of the state before setting the new onecurrentState.DoBeforeLeaving();currentState = state;// Reset the state to its desired condition before it can reason or actcurrentState.DoBeforeEntering();break;}}} // PerformTransition() } //class FSMSystem然后 我們寫兩個(gè)狀態(tài)繼承FSMState類
第一個(gè)Idle類
using System.Collections; using System.Collections.Generic; using UnityEngine;public class IdleState : FSMState {public IdleState(){stateID = StateID.IdleID;}//執(zhí)行當(dāng)前狀態(tài)的邏輯public override void Act(GameObject player, GameObject npc){Debug.Log("Idle Act");}//判斷是不是要執(zhí)行下一個(gè)狀態(tài)public override void Reason(GameObject player, GameObject npc){Debug.Log("Idle Reason");}// Start is called before the first frame update}和RunState
using System.Collections; using System.Collections.Generic; using UnityEngine;public class RunState : FSMState {public RunState(){stateID =StateID.RunID;}public override void Act(GameObject player, GameObject npc){Debug.Log("Run Act");}public override void Reason(GameObject player, GameObject npc){Debug.Log("Run Reason");}}然后新建一個(gè)狀態(tài)管理類
FSMController 用來添加狀態(tài)的轉(zhuǎn)行條件
using System.Collections; using System.Collections.Generic; using UnityEngine;public class FSMController : MonoBehaviour {FSMSystem fsmSystem;public void FixedUpdate(){fsmSystem.CurrentState.Reason(this.gameObject, gameObject);fsmSystem.CurrentState.Act(this.gameObject, gameObject);}void Start(){fsmSystem = new FSMSystem();FSMState idleState = new IdleState();//當(dāng)前狀態(tài) 要轉(zhuǎn)變的狀態(tài)idleState.AddTransition(Transition.IdleTrans, StateID.RunID);FSMState runState = new RunState();runState.AddTransition(Transition.RunTrasns, StateID.IdleID);fsmSystem.AddState(idleState);fsmSystem.AddState(runState);}private bool isDown=true;void Update(){if (Input.GetMouseButtonDown(0)){if (isDown){fsmSystem.PerformTransition(Transition.IdleTrans);isDown = !isDown;}else{fsmSystem.PerformTransition(Transition.RunTrasns);isDown = !isDown;}}} }這邊博客是翻譯的維基百科的 寫的很好的狀態(tài)機(jī)
weki連接在這里
http://wiki.unity3d.com/index.php?title=Finite_State_Machine
總結(jié)
以上是生活随笔為你收集整理的有限状态机 Finite State Machine(FSM)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你知道为啥程序员下班后只关显示器从不关电
- 下一篇: 使用JSON和Jersey的Java R