C # in the finite state machine

Wrote recently doing some monster AI, found that before the state machine maintenance is not very good, a little change yourself a bit


The so-called finite state machine that is the object of the game we decided to switch between the current state and the state, the state machine can only point to a result, the results point to the state's behavior, which is a function of execution

Before all of the state machine logic state wrote state class, but if you need to return to the newly added logic state in writing, or create a new class of state, a little lacking in readable lines of code

The main idea is to make changes to the state and class logic state is performed in a separated state logic package into separate class, written in the object file corresponding game linked script, this would improve the readability of maintenance at the same time Yasumasa

Specific wording as follows

public  class FsmCore // This class is a core state machine, the state is changing state and calls the corresponding function category 
{
     public StateBase curState; // current state is an abstract class of such 
    public StateBase PrevState; // a state 
    public StateDataBase curdata ; // data needed to perform the current state, this data is an abstract class
 // change state methods 
public  void ChangeStatemethod (StateBase state, data StateDataBase) // where two parameters are the new state data and execute a desired new state 
    {
         IF (curState! = null && curState.ID == state.ID) // determines whether the current state and the incoming state is the same state, if the state is not the same process, if the current state is empty or not starts with a state of the switching state 
        {
            return ; 
        } 
        IF (PrevState =! null ) // determines whether a record on the state, if the state is exited 
        { 
            prevState.Leave (data); // where data is not assigned, the data is still stored on the a state data 
        } 
       curdata = the data; // the new data assignment 
        curState = state; // change the current state 
        PrevState = curState; // a state change, explained here, on a state and the current state is in fact the same · state, the benefits of this writing is that the current can be assigned a meaningless less a state is empty 
        curState.Enter (the Data); // entry method calls the new state 
    }
     public  void Excute () // effect of this method is to perform the current state of the corresponding function (behavior)
    {
         IF (! CurState = null ) // current state is determined to be a non-empty 
        { 
            curState.Excute (curdata); 
        } 
    } 
} 
// base class state data class, this class stores all data to be used and a abstract execution method, when there is a new game object's behavior is not the same, you can create a class in this state to succeed him, rewrite Excute way through the final point of run-time polymorphism rewritten methods 
public  abstract  class StateDataBase 
{ 
    public Animator Anim;
     public NavMeshAgent NAV;
     public the GameObject obj;
     public the GameObject target;
     public FsmCore FSM;
     protectedStateDataBase (Animator Anim, NavMeshAgent NAV, the GameObject obj, the GameObject target, FsmCore FSM) 
    { 
        the this .anim = Anim;
         the this .nav = NAV;
         the this .OBJ = obj;
         the this .target = target;
         the this .fsm = FSM; 
    } 
    public  abstract  void Excute (); 
} 
/// base class for state classes 
all inherit from this class state, override these three methods, the logic of which varies depending on processing, can be directly invoked in the process herein Excute the method of Excute StateDataBase, because through subclass rewritten to this method, without the type judgment so here again game objects, can directly call Excute compared to the previous state machine is determined to be rather convenient in Excute ID is used to distinguish the state since the state is created each time a new object is switched, the object can not be used to directly compare
 public  abstract  class StateBase
{
    public abstract int ID { get; }
    public abstract void Enter(StateDataBase data);
    public abstract void Excute(StateDataBase data);
    public abstract void Leave(StateDataBase data);
 
}

 

Guess you like

Origin www.cnblogs.com/over-the-clouds/p/11402290.html