In video games, entities continually transition from one state to another depending on player inputs or events. An enemy character might go from an idle state to an attack state depending on whether it spots the player moving across the map. A player character is constantly blending from one animation to another as it responds to the player inputs. In this chapter, we will review a pattern that permits us to define the individual states of an entity and its stateful behaviors.
To start, we will use the traditional State pattern to manage the individual finite states of our main character. In the context of our racing game project, the main character is a motorcycle, so it has a set of mechanical behaviors and animations. As we progress in our implementation of the State pattern, we will soon see its limitations, which we will overcome...