The Player
To demonstrate how the Observer pattern can be used, we will look at a common situation in games. We will have a Player with some health that will need to be shared. The health of the Player can often be used for many things in a game. The value of health might be displayed as part of the HUD. It can also be displayed as a colored health bar in either the HUD or directly at the top or at the bottom of the Player. Additionally, the game may switch to a game over screen when the Player health is at or below zero.
These display elements, as well as the stage switching mechanisms, are dependent directly on the Player's health. Since it is very unlikely that these variables are all in the same scope, it would take some work if we tried ...
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