Working with State Machines
Games are massive pieces of code that can get quite complex. To lower the complexity of code, we can try to separate different pieces so that they only perform one action very well. That is exactly what we are going to do with a State Machine. Let’s first start with a better problem statement.
The problem
Agents, such as the player or enemies, often have to operate in very different scenarios. In a platformer game, such as Super Mario Bros for example, the character needs to be able to walk, run, jump, dive, wall slide, fly, and so on. This is a lot of different kinds of code. If we try to fit this into one big class for the player, we’ll end up with a jumble of code that is very hard to understand, debug, or extend.
Ultimately, we want our game’s code to be easily understood and maintained. That’s why we will learn about the State Machine in the next section.
The solution
A great way to combat this complexity is...