Player states
The player character has a wide variety of behaviors, such as jumping, running, and crouching. Coding such behaviors can become very complex and hard to manage. One solution is to use Boolean variables (is_jumping or is_running, for example), but this leads to possibly confusing states (what if is_crouching and is_jumping are both true?) and quickly leads to spaghetti code.
A better solution to this problem is to use a state machine to handle the player's current state and control the transitions to other states. Finite state machines were discussed in Chapter 3, Escape the Maze.
Here is a diagram of the player's states and the transitions between them:
As you can see, state machine diagrams can become quite complex, even ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access