Organize a browser game's code so it stays easy to change. The game loop and update order, scenes, entities (classes or ECS), systems, one shared game object instead of globals, data-driven config, an event bus, seeded randomness, object pools, spatial hashing for collisions, time scaling (pause, slow motion, hit-stop), and a folder layout. Use when starting a game's structure, when a game file...
Download skill (.zip) Files on GitHubGame architecture
Goal: every new feature has an obvious home, and changing one part doesn’t break another. Pick the simplest structure that fits the game’s size. Don’t build an engine for a 200-line game.
| Game size | Structure |
|---|---|
| Jam / toy (< 500 lines) | One game object, a few scene objects, entity classes. Two or three files is fine |
| Small game (most Bash games) | The game-scaffolding layout: core/, scenes/, entities/, config.js |
| Many entity types sharing behaviors, or 500+ entities | Add systems over component data (light ECS, below) |
| Using an engine (Phaser / three.js) | Follow the engine’s scene and object model. Keep game rules in your own modules, not in render callbacks |
The layers (dependencies point down only)
scenes/ title, play, pause, gameover, level-select ← orchestrate; own the entities
entities/ player, enemy, pickup, projectile ← state + behavior of one thing
systems/ physics, collisions, spawning, scoring ← logic over many things (optional)
core/ loop, input, audio, save, screen, events, rng ← engine services, no game rules
config.js / data/levels.json ← numbers and content, no code
core/never imports fromscenes/orentities/.- Entities don’t reach into other entities’ internals. They go through the scene, a system, or events.
- One
gameobject holds services (config, input, audio, save, scenes, events, rng) and is passed in. No globals except a debug handle (window.__game).
Update order (one step)
- Read input → 2. update entities / systems → 3. move + resolve collisions → 4. apply results (damage, pickups, score) →
- remove dead entities, spawn new ones (never mid-loop: collect, then apply) → 6. render (read-only).
Rendering must never change game state. Rendering twice should show the same thing.
Patterns (code in references/patterns.md)
| Need | Pattern |
|---|---|
| Decouple “coin collected” from score, sound, achievements | Event bus: events.emit("coin", {value}) |
| Replays, daily seeds, fair multiplayer | Seeded RNG (mulberry32), never Math.random in game logic |
| Hundreds of bullets / particles | Object pool + swap-remove arrays |
| Many collision checks | Spatial hash grid: only test neighbors |
| Slow motion, hit-stop, pause | Time scale: update(dt * game.timeScale) |
| Lots of shared behaviors (burning, frozen, homing) | Components + systems (light ECS) |
| Levels, waves, items | Data files (data/*.json or JS objects) read by generic code |
| AI behaviors | State machine per entity (idle → chase → attack) |
Data-driven by default
Put tunables (speeds, health, costs, spawn tables, level layouts) in config.js or data/. Code reads data. That makes
balancing a one-line change, lets a design doc point to exact values, and makes refactors safe.
Anti-patterns to fix when you see them
- Game logic inside
draw()/ render callbacks - Removing items from an array while looping forward over it
setTimeoutfor gameplay timing (it doesn’t pause). Use timers advanced bydt- Entities storing references to scenes that get replaced (leaks, stale state)
- Event listeners added on every restart and never removed
- One 2,000-line file mixing input, rules, drawing and saving (see game-refactoring)
- Frame-count timers (
if (frame % 60)) instead of seconds
When asked to design the structure
Answer with: the folder tree, what each module owns (one line each), the update order, how two example features flow through it (for example “player picks up a key → door opens”), and what’s deliberately left out. Keep it to one screen.