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 of all, we need an FSM plan. For this demo, we are interested only in how to connect the FSM to the existing game, 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 simple:
- The AI tank starts in the Patrol state, and wanders around the patrolling points we defined before.
- Then, if the players get in range, the tank switches into the Attack state.
- In the Attack state, the tank turns toward the player and start shooting at it.
- Finally, if we are in the Attack state and the players leaves the AI's range, the tank will go back to the Patrol state.
For the implementation, do...