Spotting use cases of the State pattern in games
Now that we have a gist of what the State pattern is, the problems it solves, and how it typically solves them, in this section, we will talk about some potential use cases that we can find in games where game developers usually apply the State pattern.
One common use case for the State pattern is when we are abstracting character movement. In these cases, we usually create one class for each movement state we want the context object to be in. Note that this doesn’t necessarily need to be player actions. For instance, it’s common to have a FallingState
class, especially if you want your character to have falling-specific behaviors, such as increased gravity to make the jump mechanic feel snappier. We could abstract this with an FSM that looks like the following:

Figure 8.1 – A set of possible states and their transitions as an FSM
Note that the State pattern is not exclusive to in...