Building the Game HUD
Building the Heads-Up Display and Derived State
In the last lessons, you set up a global store with useReducer + Context and drove screens from state. As a quick reminder, your UI now depends on a single state tree, and navigation is just dispatching actions. In this lesson, you will build the GameHUD (Heads-Up Display) to read from that state and compute “derived” values during render (like time text, queue colors, and progress widths).
Reducer Update: Toggle Pause
You already built most of the reducer in the previous unit; the new part here is a simple TOGGLE_PAUSE action that the HUD will dispatch. The rest of this file mirrors what you have, shown here as context.
Explanation:
TOGGLE_PAUSEflipstimers.isPausedwithout touching other fields. It is a pure update and keeps the reducer focused.- You will dispatch this from the HUD’s Pause/Resume button.
GameHUD: Reading State and Computing Derived Values
Now create the HUD that consumes the global state and derives UI values on the fly (no extra component state needed). You read from Context, compute display values, and conditionally style elements.
Explanation:
useGameContextgives you state and dispatch from the global store (no prop drilling).formatGameTimederives a human-readable clock fromtimers.gameHour/minuteduring render.getQueueColorderives a color from the queue percentage; the same function feeds the text color and the bar color.isRushHourtoggles visual emphasis when the hour hits 7.- The Pause/Resume button dispatches
TOGGLE_PAUSE. The label depends ontimers.isPaused. - The queue bar width is computed from
queueLength/maxQueue— no extra state means fewer re-renders and no sync bugs.
Note: The Pause/Resume button in the HUD currently only toggles the isPaused flag in state; it does not affect the actual game logic or timers yet. Implementing a real game loop that responds to pause/resume will be covered in a future course. For now, focus on wiring up the UI and state.
Summary and Next Steps
You connected the UI to state in a clean way:
- Added
TOGGLE_PAUSEto the reducer. - Built
GameHUDto compute derived values (time text, queue color, bar width) at render time.
You are ready to practice and make this HUD feel alive. Let’s jump in and bring more of the game state to the screen.
