Description of game development in the state machine

Why do we need a state machine

The role of the state to implement more of the action all written in one part can lead to high maintenance costs and expand low
such as: walking, jumping, shooting, conversion to avoid, and some can be converted, some can not achieve complex logic
(full screen all if - else)

State mode switch to achieve

//包含着所有的状态
enum class State{StateA, StateB, StateC, ...} activeState;
...
//通过switch语句切换状态,根据具体情况实现细节
switch (activeState)
{
    case State.StateA:
        ...
        break;
    case State.StateB:
        ...
        break;
    .......
}
...

Prototype state machine, with an enumeration represents the current state of the switch statement by filling sound to switch between the states, but still have high maintenance costs to expand the shortcomings of low (While it is true than with the if - else heap good)

Finite State Machine (FSN) finite state machine

Basic state machine, the state machine in general are other variants of such a state machine
for an understanding of the state machine, is preferably draw a map (FIG structure, a block status, links between the state of the arrow )

Finite state machine is emphasized that the switching between the state and the encapsulation of different states, the method generally is achieved according to the needs to adjust the
reference to the realization of "artificial intelligence game" of the
first base classes need a base class State Translation

//状态基类,所有状态都继承这个类
class State
{
public:
    virtual ~State(){}
    virtual OnStateEnter(){}    //进入此状态执行一次
    virtual OnUpdate(){}        //每一帧执行一次
    virtual OnStateExit(){}     //跳出状态时调用一次
    list<Translation> translations; //状态迁移列表
};

//状态迁移
class Translation
{
public:
    virtual ~Translation(){}
    virtual bool isValid() = 0;         //用于判定迁移,可切换返回true
    virtual State* getNextState() = 0;  //进入下一个状态
    virtual void onTransition(){}       //迁移时调用
};

State is the base class will inherit each state, Translations keep him in other states Translation directed, through all Translation by each frame isValid () determines whether the jump, if OnStateExit can jump itself is executed ( state), and set the current state getNextState () obtained in

And then there's the state machine class, used to manage all states:

//状态机类
class FiniteStateMachine
{
public:
    void Update();          //每一帧运行一次
    State* initialState;    //初始状态
    State* activeState;     //正在运行的状态
    
protected:
    list<State> states;     //所有状态的实例
};

void FiniteStateMachine::Update()
{
    //遍历活动状态的所有迁移,若有可用的,则切换状态
    list<Translation>::iterator itr = activeState->translations.begin();
    for (int i = 0; i < activeState->translations.size(); ++i, ++itr)
    {
        if (itr->isValid())
        {
            activeState->OnStateExit();     //退出调用
            activeState = itr->getNextState();  //切换活动状态
            itr->onTransition();                //切换调用
            activeState->OnStateEnter();    //进入调用
            return;                 //直接返回
        }
    }
    //如果没有状态切换,运行一次update
    activeState->OnUpdate();
}

Specific details of the next state will inherit specific, concrete realization

Hierarchical Finite State Machine (HFSM) hierarchical state machine

Further encapsulated hierarchical state machine corresponds to a finite state machine, the state of a plurality of packages into a large state, and then a "history of the state" operation when recording sub-state cut out of this state, the state can be switched between the large (structure shown)

Figure StateA and StateB will be added to a higher level of state StateD, simply record when exiting the state, you can switch between CD state, increasing the reusability state C (omitting the AC, the link between BC)
major First, a higher level of packaging ideas, and second, to switch into higher-level executives to think in order to improve reuse
implementing hierarchical state machines mainly in two ways:
First, the parent state is a state machine, the state may be entered, add
two adding a parent-child state stack storage state, the sub-state of each state is a state of the stack when the stack into the state, the state at the end of the stack and sends a message, the state can not process new top of the stack on the stack again until state until the top of the stack to handle the message.

Concurrent state machine

Concurrent state machines can be understood as an object has two state machines operating simultaneously, two independent state machines (but may communicate by modifying the state of the object), for example, we can leg movements (standing, squatting, run) (gun, empty-handed, targeting) isolating and hand motion.

Pushdown state machine

Push down the core state machine is that he has a state stack (but hierarchical state machine implementation and the stack is completely different, hierarchical state machine stack is to record the parent state, the stack is to record on a state).
The main solution is to stack status state machine can not get on a state execution.
When implementing, plus a stack in the state machine, upon entering the state will push the state, the state of the state when exiting the stack, the stack is state run operation.
For example, when I was doing homework hungry, I want to eat something. Hungry eat something that most states can enter the state, and after the end of "eating" the state I want to restore the original state.

At run time, the state machine has "homework" on the stack
and then "I'm hungry," will "eat" stack
"eat" end, will "eat" the stack
continue to run the stack of "homework" (virtually impossible)

If wrong, please point out

Guess you like

Origin www.cnblogs.com/yasoudream/p/11802866.html