Structuring a browser game as plain ES modules (no bundler)
javascriptes-modulesarchitecturegame-staterefactoring
Joyride (a top-down driving game on justinahampton.com) started as a 1,100-line single HTML file and was split into native ES modules with no bundler, TypeScript or framework, because the site is static and the game has no dependencies. Justin wanted "a professional architecture as we will be adding more features". The split was done as a pure move (no behavior change), verified by a harness, then reviewed by architect, pedant and adversarial QA agents; the pedant caught two regressions before shipping.
Layering
Keep the import graph acyclic and layered bottom to top, for example:
config / util / state -> dom -> fx / hud -> city / economy / input -> peds -> combat -> physics -> traffic / player -> shop / pause -> render / game -> menu -> controls -> main.
A lower layer never imports a higher one. When it needs to cause something higher up, it sets state and lets the higher layer act (the player's death is a flag set by combat that the game loop handles, not a call up).
Shared state convention
- One shared object
Sexported fromstate.js. ES module bindings are read-only to importers, and the game reassigns arrays (S.cars = S.cars.filter(...)), so mutate through the object. - Alias inside functions only (
const p = S.p); never hold an alias to anSarray across a call that may reassign it. The player object is replaced on reset and respawn. - Storage through guarded
readStore/writeStorehelpers (localStorage can throw). - Data tables in config (vehicle types, prices, map landmarks as
LOTSentries, theme palettes) so adding a landmark or theme is one entry. - Ask a module, do not assume geometry: once the city became a road graph, nothing else may assume a road on every grid multiple; everything goes through
validDir/degree/sides/hasH/hasV.
Pause ownership
One module owns pausing (S.pausedBy = shop | settings | menu); only the owner resumes. This fixed a bug where Space over the settings panel resumed the game underneath.
Deploy and caching
Serve the module directory with Cache-Control: no-cache so a deploy never mixes stale modules with a new page (Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch)).
Review backlog worth knowing (the usual smells)
- A lower layer importing a higher one (physics calling combat's
damage): push impacts to a list that the game loop drains. - The same state mutated from several modules (wanted level in economy, game and shop): give its owner
clear/decayfunctions. - Inline object construction duplicated across modules: add factories before the next entity type.
- A feature that touches five files (a new weapon): an
ITEMStable once the third one lands. dom.jsquerying elements at import time blocks testing modules without the markup: addinitDom().- Mixed-type identifiers (
whoas string or object with a magic field): compare objects directly.
See also
- Test harnesses for browser canvas games and simulations
- Arcade vehicle handling, handbrake drift and vehicle architecture