Character control in the game has a lot of states, if relying on traditional global attributes and then judging by SWich and if, poor scalability and repetitive code.

The advantages of writing a state management library to address state changes are

  • Clean code
  • reusable
  • Easy to manage

What is a finite state machine?

Finite-state machine (FSM), also known as finite-state automata, or state machine for short, is a mathematical model that expresses Finite states and the transfer and action between these states. [1]

In addition, a basic state: enter state, exit state, receive input, transfer state and other actions. But just as parkour character state management, just need to transfer the state is enough. If you are interested, you can expand.

In the code

The code has a lot of comments, so it’s faster to read the code with the comments

/* * * Title: * state machine/state management library * * Description: * Multiple state switches, more than the traditional global parameter +if1. Simple state control: by adding state names, callback methods. Call the change state method to call functions and parameters back and forth. * 2. State machine control, by adding several transition modes, and then through the transition behavior to find the transition state and execute method. * 1.0 * * / namespace XYFramework {using System. Collections. Generic; public class XYState {# region parameter/ / / < summary > / / / / / / callback < summary > / / / < param name ="param"></param> public delegate void XYCallbackFunc(params object[] param); // </summary> // </summary> public string State {get; privateset; } /// <summary> /// dictionary for storing state callback parameters /// </summary> privatereadonlyDictionary<string, XYSimpleStateModel> _simpleStateDict = new Dictionary<string, XYSimpleStateModel>(); // </summary> // store state machine transitions StateModel with dictionary /// </summary> privatereadonly Dictionary<string, XYTranslationStateModel> _translateStateDict = new Dictionary<string, XYTranslationStateModel>();

        #endregion


        #region Model// </summary> class XYTranslationStateModel {private string _name; public XYTranslationStateModel(string name) { _name = name; } /// <summary> /// a dictionary for recording state names and converting state objects /// </summary> publicreadonlyDictionary<string, XYTranslateModel> TranslationDict = new Dictionary<string, XYTranslateModel>(); } // </summary> // </summary> public class XYSimpleStateModel {public string State; public XYCallbackFunc CallbackFunc; public XYSimpleStateModel(string state, XYCallbackFunc callBackFunc) { State = state; CallbackFunc = callBackFunc; }} /// </summary> // </summary> public class XYTranslateModel {public string FromState; public string Name; public string ToState; public XYCallbackFunc OnTranslationCallback; Public XYTranslateModel(string fromState, String name,string toState,XYCallbackFunc onTranslationCallback) { FromState = fromState; Name = name; ToState = toState; OnTranslationCallback = onTranslationCallback; }}#endregion


        Region state control method/ / / < summary > add a new state / / / / / / < summary > / / / < param name ="name"</param> public void AddState(string name) {_translateStateDict[name] = new XYTranslationStateModel(name); } / / / < summary > / / / set the initial state of / / / < summary > / / / < param name ="name"></param> public void StartState(string name) { State = name; } / / / < summary > / / / add a kind of state, and the callback method / / / < summary > / / / < param name ="state"<param > // <param name="callbackFunc"<param > // <param name="param"</param> public void AddSimpleState(string state, XYCallbackFunc callbackFunc) { _simpleStateDict[state] = new XYSimpleStateModel(state, callbackFunc); } / / / < summary > / / / add a state transition to monitor / / / < summary > / / / < param name ="fromState"</param> // <param name="name"</param> // <param name="toState"</param> // <param name="callbackFunc"</param> public void AddTranslateState(string fromState, String name,string toState, XYCallbackFunc callbackFunc) { _translateStateDict[fromState].TranslationDict[name] = new XYTranslateModel(fromState, name, toState, callbackFunc); } / / / < summary > / / / / / / change state < summary > / / / < param name ="state"<param > // <param name="param"> Parameter </param> public void ChangeState(string state,params object[] param) {if (_simpleStateDict.ContainsKey(state) == false)
            {
                XYLog.LogError("This state is not found.");
                return; } XYSimpleStateModel simple = _simpleStateDict[state]; simple.CallbackFunc(param); State = state; } / / / < summary > / / / by passing in the event name, automatic conversion state / / / < summary > / / / < param name ="name"</param> // <param name="param"> </param> public void TranslateState(string name,params object[] param) {if(State ! = null) { XYLog.LogError("Unassigned initial state");
                return;
            }
            if (_translateStateDict[State].TranslationDict.ContainsKey(name))
            {
                XYLog.LogError("This transition state was not found.");
                return;
            }
            XYTranslateModel tempTranslation = _translateStateDict[State].TranslationDict[name];
            tempTranslation.OnTranslationCallback(param);
            State = tempTranslation.ToState;
           
        }
        #endregion/// </summary> // </summary> public voidClear()
        {
            _translateStateDict.Clear();
        }

    }//class_end

}

Copy the code

The effect

The middle is also matched with the use of a Log library, interested can take a look

Warehouse address: https://github.com/Lafree317/XYFrameWork