Level Discoverystart here Intermediatethe architecture Expertin depth
Intermediate · Batch 2 · Desert
Chapter 2 · intermediate level

Data, rendering & synthesis

How a single game wears four very different outfits: biomes described as data, a renderer-agnostic visuals module, textures and sound generated at runtime, depth through layers, and quality that adapts to the device.

1Data-driven biomes

A biome isn't code: it's data. One plain object describes the sky, the terrain palette, the celestial body, the parallax ridges and the ambient particle effect. The renderer reads the sheet and paints the rest.

src/config/biomes.js — one biome's sheet
{
  id: 'desert',
  sky: [0xf6b66b, 0xfde9c8],        // sky gradient
  terrain: { fill: 0xd9a441, edge: 0xf0cd7a, dark: 0x9c6f25 },
  mountains: [0xd8a566, 0xc28a4a], // two parallax ridges
  ambient: 'sand', ambientColor: 0xe9cf9a, // background particles
  roughness: 1.25,                  // drives the terrain ruggedness
}

Four biomes — Meadow 🌳, Desert 🏜️, Tundra ❄️, Volcano 🌋 — share one engine. To add a fifth, you add an object, not a code path.

Why data, not code. Settings that live as data are easy to tweak, test and serialize. Notice roughness rides along in the biome: the same number drives the terrain generator and the wind feel, keeping each place coherent.

2One renderer-agnostic module for two surfaces

The TV draws with Phaser; the phone controller draws a small preview on a plain canvas. To keep the cannon, the charge tint and the windsock identical on both, the shared maths lives in src/render/visuals.js — pure functions that return numbers, never touching either renderer.

src/render/visuals.js — colour maths, framework-free
// barrel: cool steel at low power, glowing red at full charge
export function barrelColor(power) {
  const t = powerRatio(power);
  const base = { r: 0xd7, g: 0xdd, b: 0xe8 };
  const hot  = { r: 0xff, g: 0x3b, b: 0x2f };
  // …g, b the same
}

This module knows neither Phaser nor the DOM: it returns numbers (colours, geometry points). Each view draws them with its own technology — one shared source of truth for the look.

For the curious. The module exposes helpers like shade(), craterRimColor() and computeWindsock(). None imports Phaser. That's the rule: anything shared between renderers must stay renderer-agnostic — it computes geometry and colours, and each surface draws them its own way.

3Textures generated at runtime

The project ships no image files. The particle sprites — spark, smoke, flash, cloud — are drawn once at boot into off-screen textures, then tinted and blended at runtime. Asset-free, yet fully styleable.

src/systems/textures.js — baking sprites at boot
let g = make();
g.fillStyle(0xffffff, 1);
g.fillCircle(5, 5, 5);
g.generateTexture('spark', 10, 10); // reused, tinted, for every biome

No audio files either. Every effect is built at runtime from oscillators and filtered noise through the Web Audio API, in src/systems/Sfx.js. A cannon shot, for instance, is a short low-passed noise crack plus a descending sine thump.

src/systems/Sfx.js — the cannon “boom”
const osc = ctx.createOscillator();
osc.type = 'sine';
osc.frequency.setValueAtTime(160, now);
osc.frequency.exponentialRampToValueAtTime(50, now + 0.25); // it descends
The browser gate. Browsers start the audio context suspended; it can only resume after a user gesture. So Sfx.unlock() runs on the first tap/click — without it, the first sounds would silently never play.
Remember. An “asset-free” bundle is tiny, caches trivially, and suffers no blocking load. The cost shifts to CPU at runtime — a trade-off we accept (see the next section).

4Depth, parallax & quality tiers

The fake 3D depth is just two Phaser properties per layer: depth (draw order) and scrollFactor (how much it follows the camera). Far ridges scroll slowly, the foreground scrolls fully — and the inter-round camera pan reveals continuous parallax.

src/objects/Background.js — layered scenery
this.ridge(far,  GAME_HEIGHT * 0.62, 70, DEPTH.mountainFar,  0.25);
this.ridge(near, GAME_HEIGHT * 0.72, 50, DEPTH.mountainNear, 0.45);

Smart TVs and weak browsers can choke on the full particle/parallax load. So the host measures its own frame rate for a short window at startup and records a quality tier. The renderer then scales effects to match.

src/systems/benchmark.js — pick a tier from real fps
const fps = 1000 / avg;
const tier = fps < LITE_FPS_THRESHOLD ? 'lite' : 'full';
scene.registry.set('quality', tier); // the renderer reads this
Remember. Batch 2's architecture is “describe, don't hard-code”: biomes as data, visuals as shared pure functions, textures and sound synthesized on the fly — and an honest measurement deciding how much to draw.
← Chapter 1 · The architecture Chapter 3 · Real-time client/server →