Implementing basic enemy behavior using an FSM
In Chapter 2, we briefly introduced the State Pattern, so we’ll now look at how to implement this design pattern for keeping the state of our enemy characters. Expressly, using an FSM, we can declare the fixed set of states (that is, finite) our enemy can be in at any given time – and the FSM is only going to do precisely these things.
The first implementation of our FSM will not adhere to SOLID principles very well, but it will hopefully be a simple enough introduction that will make sense practically. We can also use it as an example to point out any flaws with the approach and later refactor it to something better.
I should note that maybe we won’t refactor it later… sometimes a simple approach is all that is required, and refactoring for the sake of refactoring is just wasted effort where that time could be better spent on tightening up the core game mechanic instead, for example.
Additional reading...