Level Discoverystart here Intermediatethe architecture Expertin depth
Intermediate Β· Batch 1 Β· Meadow
Chapter 1 Β· intermediate level

The architecture of a game

We revisit Batch 1 with a developer's eye: the loop and the timestep, scenes as a state machine, and the big idea that shapes the whole project β€” separating simulation from rendering.

1The loop, and the trap of time

Every real-time game is a loop: read the inputs β†’ update the state β†’ draw, again and again. The subtlety lives in β€œupdate”.

If you move an object by a fixed number of pixels each frame, its speed depends on the frame rate: twice as fast on a 120 Hz screen as on a 60 Hz one. The fix is the timestep (dt): we multiply each movement by the time that actually elapsed, in seconds.

the timestep makes the game independent of the machine
update(time, delta) {
  const dt = delta / 1000;       // ms β†’ s
  x += speed * dt;               // the same real speed everywhere
}
Two clocks. The client draws as fast as the screen allows (often 60 fps). But the match advances at a fixed pace on the server β€” more on that in chapter 3. Keeping these two clocks distinct is an architectural decision, not a detail.

2Scenes: a state machine

Phaser organises the game into scenes. Seen from higher up, it's a finite state machine: at any moment one scene is active, and explicit transitions lead from one to another.

Boot Lobby Tv (screen) Controller match…
The device type picks the branch: a big screen hosts (TV), a phone joins (Controller).

Each scene has a clear life cycle: init (receive data), create (build the display), update (every frame), and a SHUTDOWN where we clean up (remove listeners, DOM elements). Forgetting that cleanup is a guaranteed memory leak whenever you move from one scene to another.

3The big idea: simulation β‰  rendering

The project's most important decision: the core of the game does not know about Phaser. All the logic β€” trajectories, collisions, wind, score β€” lives in src/sim/, in plain JavaScript, with no graphics dependency whatsoever.

Why bother? Because the same simulation will run on the server (the sole judge of the match) while staying testable and replayable. Decoupling the β€œwhat” from the β€œhow” is what makes the multi-screen mode of Batch 3 possible, without rewriting the physics.

4Docker, seen as a reproducible boundary

The Dockerfile is multi-stage for an architectural reason: to separate build time from run time. The first stage installs all the tooling and compiles; the second keeps only the bare minimum needed to run.

Dockerfile β€” two stages, two responsibilities
# stage 1: build (needs the whole toolchain)
FROM node:24-alpine AS build
RUN npm ci && npm run build

# stage 2: run (slim, prod deps only)
FROM node:24-alpine
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist

The result: a smaller image, with no compilers or development dependencies in production. The same logic drives the server bundle (zip / tar.gz) added in v1.1.0: ship only dist/, server/ and the shared simulation.

Remember. The architecture of Batch 1 fits in one sentence: a time-driven loop, scenes as a state machine, and a simulation decoupled from rendering β€” the foundation everything else is built on.
← Home Chapter 2 Β· Data, rendering & synthesis β†’