Back to Drift City
SOURCE / PINNED RELEASE

Made of little things.

Drift City

Release
098731aa0f0f…
Author-recorded commit
2822ea0a4f5b…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-a4c0039c855

Archive hash verified: b3323e408235502b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/game/signals.ts
import * as THREE from 'three';

/**
 * Traffic lights at the crossings of the City's two main streets (x = 0 and z = 0).
 *
 * Every light runs the same pattern, in step: one direction gets green for 7 s, then
 * amber for 2 s while the other is still red, then the other direction gets its turn.
 * The poles and housings are baked into the map with the rest of the street furniture;
 * the lenses are six shared meshes (each colour, for each direction) recoloured as the
 * lights change, so a whole city of lights costs six draw calls.
 */

/** Which way the traffic a light faces is travelling: along z ('ns') or along x ('ew'). */
export type Axis = 'ns' | 'ew';
export type Light = 'green' | 'amber' | 'red';

export const GREEN = 7;
export const AMBER = 2;
const CYCLE = 2 * (GREEN + AMBER);

const LIGHTS: Light[] = ['red', 'amber', 'green'];
/** Lit and unlit colours of each lens. */
const LENS: Record<Light, [number, number]> = {
  red: [0xff2a1f, 0x3d1110],
  amber: [0xffb21a, 0x3d2c10],
  green: [0x35ff7a, 0x0f3a20],
};

export class Signals {
  readonly root = new THREE.Group();
  private t = 0;
  private readonly junctions: { x: number; z: number }[] = [];
  private readonly lenses: Record<Axis, Record<Light, THREE.BufferGeometry[]>> = {
    ns: { red: [], amber: [], green: [] },
    ew: { red: [], amber: [], green: [] },
  };
  private readonly mats: Record<Axis, Record<Light, THREE.MeshBasicMaterial>> = {
    ns: { red: lens('red'), amber: lens('amber'), green: lens('green') },
    ew: { red: lens('red'), amber: lens('amber'), green: lens('green') },
  };

  /** A signalled crossing at (x, z). */
  addJunction(x: number, z: number): void {
    this.junctions.push({ x, z });
  }

  /**
   * The three lenses of a signal head whose face is centred at (x, y, z) and looks along
   * (fx, fz), an axis direction, towards the traffic it controls.
   */
  addHead(x: number, y: number, z: number, fx: number, fz: number, axis: Axis): void {
    LIGHTS.forEach((light, i) => {
      const geo = new THREE.BoxGeometry(fx ? 0.04 : 0.38, 0.38, fx ? 0.38 : 0.04);
      geo.translate(x + fx * 0.02, y + (1 - i) * 0.48, z + fz * 0.02);
      this.lenses[axis][light].push(geo);
    });
  }

  /** Build the lens meshes once every head is in. */
  finish(): void {
    for (const axis of ['ns', 'ew'] as const)
      for (const light of LIGHTS) {
        const parts = this.lenses[axis][light];
        if (!parts.length) continue;
        const mesh = new THREE.Mesh(merge(parts), this.mats[axis][light]);
        this.root.add(mesh);
        for (const p of parts) p.dispose();
      }
    this.update(0);
  }

  update(dt: number): void {
    this.t = (this.t + dt) % CYCLE;
    for (const axis of ['ns', 'ew'] as const) {
      const on = this.state(axis);
      for (const light of LIGHTS) this.mats[axis][light].color.setHex(LENS[light][light === on ? 0 : 1]);
    }
  }

  /** What the lights show to traffic travelling along an axis. */
  state(axis: Axis): Light {
    const t = axis === 'ns' ? this.t : (this.t + GREEN + AMBER) % CYCLE;
    return t < GREEN ? 'green' : t < GREEN + AMBER ? 'amber' : 'red';
  }

  /** Is there a signalled crossing at (x, z)? */
  at(x: number, z: number): boolean {
    return this.junctions.some((j) => Math.hypot(j.x - x, j.z - z) < 9);
  }
}

function lens(light: Light): THREE.MeshBasicMaterial {
  return new THREE.MeshBasicMaterial({ color: LENS[light][1] });
}

/** Merge non-indexed copies of boxes into one geometry. */
function merge(parts: THREE.BufferGeometry[]): THREE.BufferGeometry {
  const pos: number[] = [];
  for (const p of parts) {
    const a = p.toNonIndexed().getAttribute('position');
    for (let i = 0; i < a.count; i++) pos.push(a.getX(i), a.getY(i), a.getZ(i));
  }
  const geo = new THREE.BufferGeometry();
  geo.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3));
  return geo;
}