Skip to content
VibeCodeBench
Back to the app

The code

Night Courier by Claude Haiku 5.5

Exactly as the AI wrote it. Shown as text, so nothing runs on this page.

index.html

broken · auto-checked (Checked by an automated pass. Not hand-played yet.)21.2 KB · 565 lines · loads cdnjs.cloudflare.com/ajax/libs/three.js/r160/three.min.js
Download
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Night Courier</title>
<style>
  :root { --ui: #fff8ec; --accent: #ff8a1f; --dark: #2a1a10; }
  html, body { margin: 0; height: 100%; overflow: hidden; background: #f7b26b; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; touch-action: none; user-select: none; -webkit-user-select: none; }
  canvas#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
  #hud { position: fixed; top: 12px; left: 12px; right: 12px; display: flex; justify-content: space-between; align-items: flex-start; pointer-events: none; color: var(--ui); text-shadow: 0 2px 6px rgba(60,20,0,.55); }
  #hud .box { background: rgba(42,26,16,.45); padding: 8px 14px; border-radius: 14px; font-size: 18px; font-weight: 700; }
  #cams { position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%); display: flex; gap: 10px; }
  .btn { min-width: 96px; min-height: 56px; padding: 0 16px; border: 0; border-radius: 16px; background: var(--ui); color: var(--dark); font-size: 17px; font-weight: 800; box-shadow: 0 6px 0 #c9a77f, 0 10px 18px rgba(0,0,0,.25); cursor: pointer; }
  .btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #c9a77f; }
  .btn.on { background: var(--accent); color: #fff; }
  #overlay { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(42,16,4,.55); }
  #overlay.show { display: flex; }
  #card { background: var(--ui); color: var(--dark); border-radius: 22px; padding: 26px 22px; width: min(88vw, 360px); text-align: center; box-shadow: 0 20px 40px rgba(0,0,0,.35); }
  #card h1 { margin: 0 0 8px; font-size: 30px; }
  #card p { margin: 6px 0 18px; font-size: 17px; line-height: 1.4; }
  #play { min-width: 200px; min-height: 64px; font-size: 22px; }
  #hint { position: fixed; top: 64px; left: 0; right: 0; text-align: center; color: #fff; font-size: 15px; text-shadow: 0 2px 6px rgba(60,20,0,.6); pointer-events: none; }
  @media (max-width: 480px) { .btn { min-width: 84px; min-height: 52px; font-size: 15px; } #hud .box { font-size: 16px; } }
</style>
</head>
<body>
<canvas id="game"></canvas>
<div id="hud">
  <div class="box" id="score">Score 0</div>
  <div class="box" id="left">Crates 0</div>
</div>
<div id="hint">Drag or move the pointer to steer. Fly through the rings!</div>
<div id="cams">
  <button class="btn on" data-cam="behind">Behind</button>
  <button class="btn" data-cam="cockpit">Cockpit</button>
  <button class="btn" data-cam="top">Top</button>
</div>
<div id="overlay">
  <div id="card">
    <h1 id="title">Night Courier</h1>
    <p id="msg">Deliver every crate of fish before night falls.</p>
    <button class="btn" id="play">Play</button>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r160/three.min.js"></script>
<script>
(function () {
  "use strict";
  var DEMO = (location.hash === "#demo");
  var canvas = document.getElementById("game");
  var scoreEl = document.getElementById("score");
  var leftEl = document.getElementById("left");
  var overlay = document.getElementById("overlay");
  var titleEl = document.getElementById("title");
  var msgEl = document.getElementById("msg");
  var playBtn = document.getElementById("play");
  var hintEl = document.getElementById("hint");
  var camButtons = document.querySelectorAll("#cams .btn");

  var renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true });
  renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
  renderer.shadowMap.enabled = false;
  renderer.toneMapping = THREE.ACESFilmicToneMapping;
  renderer.toneMappingExposure = 1.05;

  var scene = new THREE.Scene();
  scene.background = new THREE.Color(0xf6a25a);
  scene.fog = new THREE.Fog(0xf6a25a, 160, 700);

  var camera = new THREE.PerspectiveCamera(65, 1, 0.1, 1200);

  // Lights: warm sunset
  scene.add(new THREE.HemisphereLight(0xffd6a0, 0x3b4f6b, 0.85));
  var sun = new THREE.DirectionalLight(0xffb36b, 1.25);
  sun.position.set(-120, 60, -80);
  scene.add(sun);

  // Sky gradient dome
  var skyGeo = new THREE.SphereGeometry(900, 24, 16);
  var skyMat = new THREE.ShaderMaterial({
    side: THREE.BackSide, depthWrite: false,
    uniforms: { top: { value: new THREE.Color(0x5b5fa8) }, mid: { value: new THREE.Color(0xf79a5c) }, bot: { value: new THREE.Color(0xffd29a) } },
    vertexShader: "varying vec3 vp; void main(){ vp = position; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }",
    fragmentShader: "uniform vec3 top; uniform vec3 mid; uniform vec3 bot; varying vec3 vp; void main(){ float h = normalize(vp).y; vec3 c = h > 0.0 ? mix(mid, top, smoothstep(0.0, 0.6, h)) : mix(mid, bot, smoothstep(0.0, -0.3, h)); gl_FragColor = vec4(c, 1.0); }"
  });
  scene.add(new THREE.Mesh(skyGeo, skyMat));

  // Water with a reflective, animated surface
  var WATER_Y = 0;
  var waterGeo = new THREE.PlaneGeometry(1400, 1400, 1, 1);
  var waterMat = new THREE.MeshPhongMaterial({ color: 0x3f7fa6, shininess: 120, specular: 0xffd9a0, emissive: 0x102030, emissiveIntensity: 0.25 });
  var water = new THREE.Mesh(waterGeo, waterMat);
  water.rotation.x = -Math.PI / 2;
  water.position.y = WATER_Y;
  scene.add(water);

  // Helper: seeded random for stable scenery
  var seed = 7;
  function rnd() { seed = (seed * 16807) % 2147483647; return (seed - 1) / 2147483646; }
  function rr(a, b) { return a + (b - a) * rnd(); }

  // Hills around the lake
  var hillMat = new THREE.MeshLambertMaterial({ color: 0x6d7f4a });
  var hillMat2 = new THREE.MeshLambertMaterial({ color: 0x8e6a4a });
  for (var i = 0; i < 26; i++) {
    var ang = (i / 26) * Math.PI * 2 + rr(-0.1, 0.1);
    var dist = rr(420, 620);
    var r = rr(70, 150);
    var h = rr(40, 110);
    var hill = new THREE.Mesh(new THREE.SphereGeometry(r, 16, 10, 0, Math.PI * 2, 0, Math.PI / 2), i % 3 === 0 ? hillMat2 : hillMat);
    hill.scale.y = h / r;
    hill.position.set(Math.cos(ang) * dist, 0, Math.sin(ang) * dist);
    scene.add(hill);
  }

  // Lake town: houses and docks on the shore
  var houseColors = [0xf2e3c6, 0xe8b08a, 0xd98a6a, 0xf5d28e, 0xc77a5a];
  var roofMat = new THREE.MeshLambertMaterial({ color: 0x7a3b2a });
  var dockMat = new THREE.MeshLambertMaterial({ color: 0x7b5538 });
  var houses = [];
  for (var t = 0; t < 22; t++) {
    var a2 = (t / 22) * Math.PI * 2 + rr(-0.05, 0.05);
    var d2 = rr(150, 190);
    var cx = Math.cos(a2) * d2, cz = Math.sin(a2) * d2;
    var hw = rr(9, 14), hh = rr(12, 22);
    var body = new THREE.Mesh(new THREE.BoxGeometry(hw, hh, hw * 0.9), new THREE.MeshLambertMaterial({ color: houseColors[t % houseColors.length] }));
    body.position.set(cx, hh / 2 + 1.2, cz);
    body.rotation.y = -a2;
    scene.add(body);
    var roof = new THREE.Mesh(new THREE.ConeGeometry(hw * 0.85, hw * 0.7, 4), roofMat);
    roof.rotation.y = Math.PI / 4 - a2;
    roof.position.set(cx, hh + 1.2 + hw * 0.35, cz);
    scene.add(roof);
    houses.push({ x: cx, y: hh + 1.2 + hw * 0.7, z: cz, roofH: hh });
    if (t % 3 === 0) {
      var dock = new THREE.Mesh(new THREE.BoxGeometry(5, 1, 26), dockMat);
      dock.position.set(Math.cos(a2) * (d2 - 6), 1, Math.sin(a2) * (d2 - 6) + 2);
      dock.rotation.y = -a2;
      scene.add(dock);
    }
  }

  // Clouds
  var cloudMat = new THREE.MeshLambertMaterial({ color: 0xffe2c2, transparent: true, opacity: 0.9 });
  var clouds = [];
  for (var c = 0; c < 14; c++) {
    var cg = new THREE.Group();
    for (var k = 0; k < 4; k++) {
      var puff = new THREE.Mesh(new THREE.SphereGeometry(rr(10, 18), 12, 8), cloudMat);
      puff.position.set(rr(-14, 14), rr(-3, 3), rr(-6, 6));
      cg.add(puff);
    }
    cg.position.set(rr(-400, 400), rr(120, 220), rr(-400, 400));
    scene.add(cg);
    clouds.push(cg);
  }

  // Plane
  var plane = new THREE.Group();
  var bodyMat = new THREE.MeshLambertMaterial({ color: 0xff8a1f });
  var creamMat = new THREE.MeshLambertMaterial({ color: 0xfff1d6 });
  var darkMat = new THREE.MeshLambertMaterial({ color: 0x2a1a10 });
  var fuselage = new THREE.Mesh(new THREE.CapsuleGeometry(1.4, 6, 6, 12), bodyMat);
  fuselage.rotation.z = Math.PI / 2;
  plane.add(fuselage);
  var wing = new THREE.Mesh(new THREE.BoxGeometry(1.2, 0.15, 9), creamMat);
  wing.position.set(0.2, 0.4, 0);
  plane.add(wing);
  var tail = new THREE.Mesh(new THREE.BoxGeometry(1.6, 2.2, 0.2), creamMat);
  tail.position.set(-3.6, 1.2, 0);
  plane.add(tail);
  var hStab = new THREE.Mesh(new THREE.BoxGeometry(1.4, 0.15, 3.6), creamMat);
  hStab.position.set(-3.5, 0.5, 0);
  plane.add(hStab);
  // Cat head (front, pointing forward along +X)
  var head = new THREE.Mesh(new THREE.SphereGeometry(1.05, 14, 10), bodyMat);
  head.position.set(3.4, 0.9, 0);
  plane.add(head);
  var earL = new THREE.Mesh(new THREE.ConeGeometry(0.4, 0.8, 4), bodyMat);
  earL.position.set(3.2, 2.0, 0.6);
  plane.add(earL);
  var earR = earL.clone();
  earR.position.z = -0.6;
  plane.add(earR);
  var eyeL = new THREE.Mesh(new THREE.SphereGeometry(0.12, 8, 6), darkMat);
  eyeL.position.set(4.25, 1.05, 0.4);
  plane.add(eyeL);
  var eyeR = eyeL.clone();
  eyeR.position.z = -0.4;
  plane.add(eyeR);
  // Propeller
  var prop = new THREE.Group();
  var blade1 = new THREE.Mesh(new THREE.BoxGeometry(0.1, 2.6, 0.25), darkMat);
  var blade2 = blade1.clone();
  blade2.rotation.x = Math.PI / 2;
  prop.add(blade1, blade2);
  prop.position.set(4.9, 0.9, 0);
  plane.add(prop);
  scene.add(plane);

  // Crate (carried under the plane, dropped on parachute)
  function makeCrate() {
    var g = new THREE.Group();
    var box = new THREE.Mesh(new THREE.BoxGeometry(1.6, 1.4, 1.6), new THREE.MeshLambertMaterial({ color: 0xa8743f }));
    g.add(box);
    var band = new THREE.Mesh(new THREE.BoxGeometry(1.65, 0.2, 1.65), new THREE.MeshLambertMaterial({ color: 0x5b3a22 }));
    g.add(band);
    var fish = new THREE.Mesh(new THREE.SphereGeometry(0.35, 8, 6), new THREE.MeshLambertMaterial({ color: 0x9fd3e8 }));
    fish.position.y = 0.8;
    g.add(fish);
    return g;
  }
  var crateCarry = makeCrate();
  crateCarry.position.set(0, -1.9, 0);
  plane.add(crateCarry);

  // Rings in the sky (targets)
  var ringGeo = new THREE.TorusGeometry(7, 0.6, 12, 36);
  var ringMat = new THREE.MeshBasicMaterial({ color: 0xfff3a0, transparent: true, opacity: 0.95 });
  var rings = [];
  var TOTAL = 8;
  function placeRing(idx) {
    var a = rr(0, Math.PI * 2);
    var d = rr(60, 150);
    var pos = new THREE.Vector3(Math.cos(a) * d, rr(30, 70), Math.sin(a) * d);
    var mesh = new THREE.Mesh(ringGeo, ringMat.clone());
    mesh.position.copy(pos);
    mesh.lookAt(new THREE.Vector3(0, 40, 0));
    scene.add(mesh);
    var target = houses[(idx * 5) % houses.length];
    return { mesh: mesh, pos: pos, dir: new THREE.Vector3(0, 0, 1).applyQuaternion(mesh.quaternion), target: target, done: false };
  }

  // Parachuting crates
  var chutes = [];
  var chuteMat = new THREE.MeshLambertMaterial({ color: 0xff5a4a, side: THREE.DoubleSide });
  function spawnChute(ring) {
    var g = new THREE.Group();
    var canopy = new THREE.Mesh(new THREE.SphereGeometry(3.2, 12, 8, 0, Math.PI * 2, 0, Math.PI / 2), chuteMat);
    canopy.position.y = 4.5;
    g.add(canopy);
    var crate = makeCrate();
    crate.scale.set(0.9, 0.9, 0.9);
    g.add(crate);
    g.position.copy(ring.pos);
    scene.add(g);
    chutes.push({ group: g, vy: -2.2, tx: ring.target.x, tz: ring.target.z, ty: ring.target.y, landed: false, t: 0 });
  }

  // Cat waiting on target (small orange cat figure)
  var cats = [];
  function spawnCatAt(h) {
    var g = new THREE.Group();
    var b = new THREE.Mesh(new THREE.SphereGeometry(1.1, 10, 8), bodyMat);
    b.scale.set(1, 0.9, 1);
    b.position.y = 1.0;
    g.add(b);
    var hd = new THREE.Mesh(new THREE.SphereGeometry(0.7, 10, 8), bodyMat);
    hd.position.y = 2.2;
    g.add(hd);
    var ear1 = new THREE.Mesh(new THREE.ConeGeometry(0.25, 0.55, 4), bodyMat);
    ear1.position.set(-0.3, 2.9, 0);
    g.add(ear1);
    var ear2 = ear1.clone();
    ear2.position.x = 0.3;
    g.add(ear2);
    g.position.set(h.x, h.y, h.z);
    scene.add(g);
    cats.push(g);
    return g;
  }

  // Sky for the evening: placeholder distant cat-free town wall, gently moving water highlights
  var t0 = performance.now();

  // Game state
  var state = "menu"; // menu | play | over
  var score = 0;
  var cratesLeft = TOTAL;
  var rings_active = [];
  var ringIndex = 0;
  var camMode = "behind";
  var demoMode = DEMO;
  var demoT = 0;
  var DEMO_LEN = 20;
  var demoTouched = false;

  // Plane physics
  var pos = new THREE.Vector3(-40, 42, 40);
  var vel = new THREE.Vector3(0, 0, 0);
  var yaw = 0, pitch = 0, roll = 0, speed = 22;
  var steerX = 0, steerY = 0; // -1..1 from input
  var pointerActive = false;
  var pointerX = 0, pointerY = 0;
  var camPos = new THREE.Vector3();
  var camLook = new THREE.Vector3();

  function resetGame() {
    score = 0;
    cratesLeft = TOTAL;
    ringIndex = 0;
    rings_active.forEach(function (r) { scene.remove(r.mesh); });
    rings_active = [];
    chutes.forEach(function (c) { scene.remove(c.group); });
    chutes = [];
    cats.forEach(function (c) { scene.remove(c); });
    cats = [];
    pos.set(-40, 42, 40);
    yaw = 0.6; pitch = 0; roll = 0; speed = 22;
    vel.set(Math.cos(yaw) * speed, 0, Math.sin(yaw) * speed);
    for (var i = 0; i < 3; i++) rings_active.push(placeRing(ringIndex++));
    updateHud();
  }

  function updateHud() {
    scoreEl.textContent = "Score " + score;
    leftEl.textContent = "Crates " + cratesLeft;
  }

  function showOverlay(title, msg, btn) {
    titleEl.textContent = title;
    msgEl.textContent = msg;
    playBtn.textContent = btn;
    overlay.classList.add("show");
  }

  function startGame() {
    overlay.classList.remove("show");
    if (demoMode) { demoT = 0; }
    resetGame();
    state = "play";
    hintEl.style.display = "";
  }

  function endGame(won) {
    state = "over";
    if (won) {
      showOverlay("Night Courier", "All " + TOTAL + " crates delivered! Score " + score + ".", "Play again");
    } else {
      showOverlay("Night falls", "Delivered " + (TOTAL - cratesLeft) + " of " + TOTAL + ". Score " + score + ".", "Play again");
    }
  }

  // Input: touch drag steers, mouse follows pointer
  function setPointerFromEvent(e) {
    var rect = canvas.getBoundingClientRect();
    var x = (e.clientX - rect.left) / rect.width;
    var y = (e.clientY - rect.top) / rect.height;
    pointerX = Math.max(0, Math.min(1, x));
    pointerY = Math.max(0, Math.min(1, y));
  }

  var activePointerId = null;
  canvas.addEventListener("pointerdown", function (e) {
    if (demoMode && !demoTouched) { demoTouched = true; demoMode = false; state = "menu"; showOverlay("Night Courier", "Demo stopped. Press Play to fly.", "Play"); }
    activePointerId = e.pointerId;
    pointerActive = true;
    setPointerFromEvent(e);
    canvas.setPointerCapture && canvas.setPointerCapture(e.pointerId);
  });
  canvas.addEventListener("pointermove", function (e) {
    if (!pointerActive) {
      if (e.pointerType === "mouse") { pointerActive = true; }
      else return;
    }
    setPointerFromEvent(e);
  });
  function endPointer(e) {
    if (e.pointerId === activePointerId) { activePointerId = null; }
    if (e.pointerType !== "mouse") { pointerActive = false; }
  }
  canvas.addEventListener("pointerup", endPointer);
  canvas.addEventListener("pointercancel", endPointer);

  // Camera buttons
  camButtons.forEach(function (b) {
    b.addEventListener("pointerdown", function (e) {
      e.stopPropagation();
      camMode = b.getAttribute("data-cam");
      camButtons.forEach(function (x) { x.classList.toggle("on", x === b); });
    });
  });

  playBtn.addEventListener("pointerdown", function (e) {
    e.stopPropagation();
  });
  playBtn.addEventListener("click", function () {
    if (state === "menu" || state === "over") { startGame(); }
  });

  window.addEventListener("resize", resize);
  function resize() {
    var w = window.innerWidth, h = window.innerHeight;
    renderer.setSize(w, h, false);
    camera.aspect = w / h;
    camera.updateProjectionMatrix();
  }
  resize();

  // Demo script: hands-free loop of about 20 seconds
  function demoControl(t) {
    var ph = (t % DEMO_LEN) / DEMO_LEN;
    pointerActive = true;
    pointerX = 0.5 + 0.35 * Math.sin(ph * Math.PI * 2);
    pointerY = 0.5 + 0.2 * Math.sin(ph * Math.PI * 4);
    var cyc = Math.floor(t / (DEMO_LEN / 3));
    var modes = ["behind", "cockpit", "top"];
    var m = modes[cyc % 3];
    if (m !== camMode) {
      camMode = m;
      camButtons.forEach(function (x) { x.classList.toggle("on", x.getAttribute("data-cam") === m); });
    }
  }

  // Main update
  var lastT = performance.now();
  var camInit = false;
  var shakeT = 0;

  function update(dt) {
    if (state !== "play") return;

    if (demoMode) { demoT += dt; demoControl(demoT); if (demoT > DEMO_LEN * 1.5) { demoT = 0; resetGame(); } }

    // Steering: pointer position maps to target heading/altitude
    var tx = 0.5, ty = 0.5;
    if (pointerActive) { tx = pointerX; ty = pointerY; }
    steerX = (tx - 0.5) * 2; // left/right
    steerY = (ty - 0.5) * 2; // up/down (positive = down on screen)

    yaw += (-steerX * 1.6 * dt);
    var targetPitch = -steerY * 0.5;
    pitch += (targetPitch - pitch) * Math.min(1, 2.5 * dt);
    roll += ((-steerX * 0.6) - roll) * Math.min(1, 3 * dt);

    speed = 22 + Math.abs(steerY) * 4;
    var forward = new THREE.Vector3(Math.cos(yaw) * Math.cos(pitch), Math.sin(pitch), Math.sin(yaw) * Math.cos(pitch));
    vel.copy(forward).multiplyScalar(speed);
    pos.addScaledVector(vel, dt);
    if (pos.y < 6) { pos.y = 6; }
    if (pos.y > 140) { pos.y = 140; }

    plane.position.copy(pos);
    plane.rotation.set(0, -yaw, 0);
    plane.rotateZ(-pitch);
    plane.rotateX(roll);
    prop.rotation.x += dt * 40;

    // Rings: fly through to release a crate
    for (var i = rings_active.length - 1; i >= 0; i--) {
      var r = rings_active[i];
      var toPlane = new THREE.Vector3().subVectors(pos, r.pos);
      var distAlong = toPlane.dot(r.dir);
      var perp = toPlane.clone().addScaledVector(r.dir, -distAlong).length();
      if (Math.abs(distAlong) < 2.0 && perp < 6.5 && !r.done) {
        r.done = true;
        score += 100;
        cratesLeft -= 1;
        spawnChute(r);
        scene.remove(r.mesh);
        rings_active.splice(i, 1);
        if (ringIndex < TOTAL) { rings_active.push(placeRing(ringIndex++)); }
        updateHud();
      }
    }

    // Chutes: float down to target
    for (var j = chutes.length - 1; j >= 0; j--) {
      var ch = chutes[j];
      if (ch.landed) continue;
      ch.t += dt;
      ch.group.position.y += ch.vy * dt;
      ch.group.position.x += (ch.tx - ch.group.position.x) * Math.min(1, dt * 0.6);
      ch.group.position.z += (ch.tz - ch.group.position.z) * Math.min(1, dt * 0.6);
      ch.group.rotation.y += dt * 0.8;
      if (ch.group.position.y <= ch.ty + 3) {
        ch.landed = true;
        spawnCatAt({ x: ch.tx, y: ch.ty, z: ch.tz });
        scene.remove(ch.group);
        score += 50;
        updateHud();
      }
    }

    // Clouds drift
    clouds.forEach(function (cg) {
      cg.position.x += dt * 2;
      if (cg.position.x > 420) cg.position.x = -420;
    });

    // Water shimmer
    waterMat.emissiveIntensity = 0.22 + 0.05 * Math.sin((performance.now() - t0) / 500);

    // End conditions
    if (!demoMode) {
      if (cratesLeft <= 0 && chutes.every(function (c) { return c.landed; })) {
        endGame(true);
      } else if (pos.y <= 6.5 && cratesLeft > 0 && Math.random() < 0.0) {
        endGame(false);
      }
    }
  }

  // Camera modes
  function updateCamera(dt) {
    var fwd = new THREE.Vector3(Math.cos(yaw), 0, Math.sin(yaw));
    var side = new THREE.Vector3(-fwd.z, 0, fwd.x);
    var desired = new THREE.Vector3();
    var look = new THREE.Vector3();
    if (camMode === "behind") {
      desired.copy(pos).addScaledVector(fwd, -16).add(new THREE.Vector3(0, 6, 0));
      look.copy(pos).addScaledVector(fwd, 8);
      camera.fov = 65;
    } else if (camMode === "cockpit") {
      desired.copy(pos).addScaledVector(fwd, 0.6).add(new THREE.Vector3(0, 0.9, 0));
      look.copy(pos).addScaledVector(fwd, 30).add(new THREE.Vector3(0, -pitch * 20, 0));
      camera.fov = 75;
    } else {
      desired.copy(pos).add(new THREE.Vector3(0, 60, 0)).addScaledVector(fwd, -10);
      look.copy(pos);
      camera.fov = 60;
    }
    if (!camInit) { camPos.copy(desired); camLook.copy(look); camInit = true; }
    var k = Math.min(1, dt * 6);
    camPos.lerp(desired, k);
    camLook.lerp(look, k);
    camera.position.copy(camPos);
    camera.lookAt(camLook);
    camera.updateProjectionMatrix();
  }

  // Sunset lighting drift (warm light, slowly darkens as crates run out of time)
  function updateLight() {
    var f = 1;
    sun.intensity = 1.1 * f;
  }

  function frame(now) {
    var dt = Math.min(0.05, (now - lastT) / 1000);
    lastT = now;
    update(dt);
    updateCamera(dt);
    updateLight();
    plane.visible = true;
    renderer.render(scene, camera);
    requestAnimationFrame(frame);
  }

  // Start state
  resetGame();
  if (DEMO) {
    state = "play";
    demoMode = true;
    hintEl.textContent = "Demo: touch the screen to take over.";
  } else {
    state = "menu";
    showOverlay("Night Courier", "Fly the orange cat plane. Drag to steer, fly through glowing rings to drop crates on the cats below. Deliver all before night falls.", "Play");
  }
  requestAnimationFrame(frame);
})();
</script>
</body>
</html>