Bash.tv → GitHub → itch.io

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 GitHub

Game 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

Update order (one step)

  1. Read input → 2. update entities / systems → 3. move + resolve collisions → 4. apply results (damage, pickups, score) →
  2. 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

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.