Creating decision-making AI with FSM
In Chapter 2, Finite State Machines, we saw how to implement a simple FSM. In this section, we are using the same technique, but will apply it to the more complex scenario of this demo.
First, we need an FSM plan. We are interested only in connecting the FSM to the existing game for this demo, so we will keep it simple. The FSM for our tank is composed of just two states – patrolling and shooting.
The plan is nice and straightforward:
- The AI tank starts in the
Patrol
state and wanders around the previously defined patrolling points. - Then, if the players get in range, the tank switches to the
Attack
state. - In the
Attack
state, the tank turns toward the player and starts shooting at it. - Finally, if we are in the
Attack
state and the players leave the AI's range, the tank will return to thePatrol
state.
For the implementation...