Chapter 12 Using States and State Machines

Apple Game Frameworks and Technologies — by Tammy Coron (112 / 193)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Next Steps | TOC | Create the Player States 👉

In the previous chapter, you started using the Entity Component System (ECS) architecture. As you were learning about this popular game design pattern, you built four separate components for the Val’s Revenge game.

In this chapter, you’ll pick up where you left off, adding two more components: one to handle collectible items, and one to manage the physics bodies. You’ll also begin to add more gameplay- and game state–related logic, such as what happens when players find treasure and food, or what happens when they pick up keys that Val can use to unlock doors.

Game State and the State Pattern

INFORMATION

Finding the definition of game state isn’t that easy. Not because it’s a made-up term — that’s not it at all. The problem is that game state is actually part of a larger design pattern known as the state pattern, which is closely related to the concept of a finite-state machine (FSM). Using the state pattern, you’re able to track the state of an object — in other words, the way something is with respect to its attributes. For example, whether or not the game is in progress.

Traditionally, game developers would manage game state and state-related logic using a series of if statements. For example…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.