Back to winniegolf
SOURCE / PINNED RELEASE

Made of little things.

winniegolf

Release
017b791c0f4b…
Author-recorded commit
612845c125fd…
License
LICENSE
Author’s source reference
nostr://npub18weq5jzw27gkhwcuf6gu57alcptjdvh78nlulgpkz8s7pnlxfnnspc00s9/wss%3A%2F%2Fgit.napplet.soy%2F/n-2d501957e21

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

src/game/render.ts
import { palette } from './palette.js';
import {
  BALL_R,
  HOLE_R,
  predictPath,
  spinnerAngle,
} from './physics.js';
import type { Ball, Level, Particle, Vec } from './types.js';

export interface ViewTransform {
  scale: number;
  tx: number;
  ty: number;
  rotate: boolean;
}

/**
 * Fit the course into the frame, below `topInset` so the broadcast bar never
 * covers play. When turning it 90 degrees uses more of the frame (portrait),
 * the course is rotated; input maps back through the same transform so aiming
 * never changes.
 */
export function computeTransform(
  cw: number,
  ch: number,
  w: number,
  h: number,
  topInset: number,
): ViewTransform {
  const availH = Math.max(40, ch - topInset);
  const flat = Math.min(cw / w, availH / h);
  const turned = Math.min(cw / h, availH / w);
  const rotate = turned > flat;
  const scale = rotate ? turned : flat;
  const footW = rotate ? h : w;
  const footH = rotate ? w : h;
  return {
    scale,
    tx: (cw - footW * scale) / 2,
    ty: topInset + (availH - footH * scale) / 2,
    rotate,
  };
}

/** Screen point in CSS pixels -> course coordinates. */
export function screenToCourse(
  sx: number,
  sy: number,
  view: ViewTransform,
  h: number,
): Vec {
  const u = (sx - view.tx) / view.scale;
  const v = (sy - view.ty) / view.scale;
  return view.rotate ? { x: v, y: h - u } : { x: u, y: v };
}

function applyTransform(
  ctx: CanvasRenderingContext2D,
  view: ViewTransform,
  h: number,
  dpr: number,
): void {
  const s = dpr * view.scale;
  if (view.rotate) {
    ctx.setTransform(s, 0, 0, s, dpr * (view.tx + view.scale * h), dpr * view.ty);
    ctx.rotate(Math.PI / 2);
  } else {
    ctx.setTransform(s, 0, 0, s, dpr * view.tx, dpr * view.ty);
  }
}

function roundRectPath(
  ctx: CanvasRenderingContext2D,
  x: number,
  y: number,
  w: number,
  h: number,
  r: number,
): void {
  const radius = Math.max(0, Math.min(r, Math.min(w, h) / 2));
  ctx.beginPath();
  ctx.moveTo(x + radius, y);
  ctx.lineTo(x + w - radius, y);
  ctx.quadraticCurveTo(x + w, y, x + w, y + radius);
  ctx.lineTo(x + w, y + h - radius);
  ctx.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);
  ctx.lineTo(x + radius, y + h);
  ctx.quadraticCurveTo(x, y + h, x, y + h - radius);
  ctx.lineTo(x, y + radius);
  ctx.quadraticCurveTo(x, y, x + radius, y);
  ctx.closePath();
}

function fillFairway(ctx: CanvasRenderingContext2D, w: number, h: number): void {
  ctx.fillStyle = palette.turfA;
  ctx.fillRect(0, 0, w, h);
  ctx.fillStyle = palette.turfB;
  const band = 34;
  for (let y = 0; y < h; y += band * 2) ctx.fillRect(0, y, w, band);
}

function drawWater(
  ctx: CanvasRenderingContext2D,
  level: Level,
  time: number,
): void {
  ctx.fillStyle = palette.water;
  ctx.fillRect(0, 0, level.w, level.h);
  ctx.fillStyle = palette.waterDeep;
  ctx.globalAlpha = 0.35;
  const wave = Math.sin(time * 1.4) * 6;
  for (let x = -20; x < level.w + 20; x += 64) {
    ctx.fillRect(x + wave, 0, 18, level.h);
  }
  ctx.globalAlpha = 1;
}

function drawShore(ctx: CanvasRenderingContext2D, level: Level): void {
  if (!level.land) return;
  ctx.fillStyle = palette.shore;
  for (const rect of level.land) {
    ctx.fillRect(rect.x - 7, rect.y - 7, rect.w + 14, rect.h + 14);
  }
}

function drawRails(ctx: CanvasRenderingContext2D, level: Level): void {
  for (const rect of level.walls) {
    ctx.fillStyle = palette.shadow;
    roundRectPath(ctx, rect.x, rect.y + 5, rect.w, rect.h, 7);
    ctx.fill();
    ctx.fillStyle = palette.rail;
    roundRectPath(ctx, rect.x, rect.y, rect.w, rect.h - 5, 7);
    ctx.fill();
    ctx.fillStyle = palette.railTop;
    roundRectPath(ctx, rect.x + 2, rect.y + 1, rect.w - 4, 4, 2);
    ctx.fill();
  }
}

function drawSand(ctx: CanvasRenderingContext2D, level: Level): void {
  for (const rect of level.sand) {
    roundRectPath(ctx, rect.x, rect.y, rect.w, rect.h, 18);
    ctx.fillStyle = palette.sand;
    ctx.fill();
    ctx.strokeStyle = palette.sandEdge;
    ctx.lineWidth = 3;
    ctx.stroke();
  }
}

function drawBumpers(ctx: CanvasRenderingContext2D, level: Level): void {
  for (const bumper of level.bumpers) {
    ctx.fillStyle = palette.shadow;
    ctx.beginPath();
    ctx.arc(bumper.x, bumper.y + 5, bumper.r + 1, 0, Math.PI * 2);
    ctx.fill();
    ctx.fillStyle = palette.brass;
    ctx.beginPath();
    ctx.arc(bumper.x, bumper.y, bumper.r, 0, Math.PI * 2);
    ctx.fill();
    ctx.strokeStyle = palette.brassDeep;
    ctx.lineWidth = 3;
    ctx.stroke();
    ctx.fillStyle = palette.ink;
    ctx.beginPath();
    ctx.arc(bumper.x, bumper.y, bumper.r * 0.4, 0, Math.PI * 2);
    ctx.fill();
  }
}

function drawSpinners(ctx: CanvasRenderingContext2D, level: Level, time: number): void {
  for (const spinner of level.spinners) {
    const angle = spinnerAngle(spinner, time);
    const dx = Math.cos(angle);
    const dy = Math.sin(angle);
    const ax = spinner.cx - dx * spinner.half;
    const ay = spinner.cy - dy * spinner.half;
    const bx = spinner.cx + dx * spinner.half;
    const by = spinner.cy + dy * spinner.half;

    ctx.lineCap = 'round';
    ctx.strokeStyle = palette.shadow;
    ctx.lineWidth = spinner.thick;
    ctx.beginPath();
    ctx.moveTo(ax + 4, ay + 6);
    ctx.lineTo(bx + 4, by + 6);
    ctx.stroke();

    ctx.strokeStyle = palette.rail;
    ctx.beginPath();
    ctx.moveTo(ax, ay);
    ctx.lineTo(bx, by);
    ctx.stroke();

    const segments = 6;
    const seg = (spinner.half * 2) / segments;
    ctx.lineCap = 'butt';
    ctx.strokeStyle = palette.flag;
    for (let i = 0; i < segments; i += 2) {
      const s0 = -spinner.half + i * seg;
      ctx.beginPath();
      ctx.moveTo(spinner.cx + dx * s0, spinner.cy + dy * s0);
      ctx.lineTo(spinner.cx + dx * (s0 + seg), spinner.cy + dy * (s0 + seg));
      ctx.stroke();
    }

    ctx.fillStyle = palette.ink;
    ctx.beginPath();
    ctx.arc(spinner.cx, spinner.cy, spinner.thick * 0.95, 0, Math.PI * 2);
    ctx.fill();
    ctx.fillStyle = palette.brass;
    ctx.beginPath();
    ctx.arc(spinner.cx, spinner.cy, spinner.thick * 0.4, 0, Math.PI * 2);
    ctx.fill();
  }
}

function drawCup(ctx: CanvasRenderingContext2D, level: Level, time: number): void {
  const { x, y } = level.cup;
  ctx.fillStyle = palette.cupRim;
  ctx.beginPath();
  ctx.arc(x, y, HOLE_R + 3, 0, Math.PI * 2);
  ctx.fill();
  ctx.fillStyle = palette.cup;
  ctx.beginPath();
  ctx.arc(x, y, HOLE_R, 0, Math.PI * 2);
  ctx.fill();

  const poleTop = { x, y: y - 42 };
  const wave = Math.sin(time * 3) * 4;

  ctx.strokeStyle = 'rgba(8, 16, 11, 0.28)';
  ctx.lineWidth = 4;
  ctx.lineCap = 'round';
  ctx.beginPath();
  ctx.moveTo(x + 5, y + 4);
  ctx.lineTo(poleTop.x + 16, poleTop.y + 10);
  ctx.stroke();

  ctx.fillStyle = palette.flag;
  ctx.beginPath();
  ctx.moveTo(poleTop.x, poleTop.y);
  ctx.lineTo(poleTop.x + 26 + wave, poleTop.y + 7 + wave * 0.4);
  ctx.lineTo(poleTop.x, poleTop.y + 15);
  ctx.closePath();
  ctx.fill();

  ctx.strokeStyle = palette.pole;
  ctx.lineWidth = 3.5;
  ctx.beginPath();
  ctx.moveTo(x, y);
  ctx.lineTo(poleTop.x, poleTop.y);
  ctx.stroke();
}

function drawTrail(ctx: CanvasRenderingContext2D, trail: Vec[]): void {
  for (let i = 0; i < trail.length; i += 1) {
    const t = (i + 1) / (trail.length + 1);
    ctx.globalAlpha = t * 0.4;
    ctx.fillStyle = palette.ball;
    ctx.beginPath();
    ctx.arc(trail[i].x, trail[i].y, BALL_R * (0.3 + t * 0.45), 0, Math.PI * 2);
    ctx.fill();
  }
  ctx.globalAlpha = 1;
}

function drawBall(ctx: CanvasRenderingContext2D, ball: Ball): void {
  ctx.fillStyle = palette.shadow;
  ctx.beginPath();
  ctx.ellipse(ball.x + 3, ball.y + 5, BALL_R * 1.02, BALL_R * 0.84, 0, 0, Math.PI * 2);
  ctx.fill();

  const gradient = ctx.createRadialGradient(
    ball.x - 4,
    ball.y - 5,
    1,
    ball.x,
    ball.y,
    BALL_R,
  );
  gradient.addColorStop(0, palette.ball);
  gradient.addColorStop(0.65, palette.ball);
  gradient.addColorStop(1, palette.ballShade);
  ctx.fillStyle = gradient;
  ctx.beginPath();
  ctx.arc(ball.x, ball.y, BALL_R, 0, Math.PI * 2);
  ctx.fill();

  ctx.fillStyle = 'rgba(120, 132, 145, 0.35)';
  const dimples: Array<[number, number]> = [
    [-4, 2],
    [3, 4],
    [1, -4],
    [6, -2],
    [-2, -6],
  ];
  for (const [ox, oy] of dimples) {
    ctx.beginPath();
    ctx.arc(ball.x + ox, ball.y + oy, 1.7, 0, Math.PI * 2);
    ctx.fill();
  }
}

function drawAim(
  ctx: CanvasRenderingContext2D,
  level: Level,
  ball: Ball,
  angle: number,
  power: number,
  time: number,
): void {
  const dir = { x: Math.cos(angle), y: Math.sin(angle) };
  const reach = 90 + power * 380;
  const points = predictPath({ x: ball.x, y: ball.y }, dir, level, reach, 2);

  ctx.save();
  ctx.setLineDash([10, 10]);
  ctx.lineDashOffset = -time * 34;
  ctx.lineCap = 'round';
  ctx.lineWidth = 4;
  ctx.strokeStyle = palette.aim;
  ctx.beginPath();
  ctx.moveTo(points[0].x, points[0].y);
  for (let i = 1; i < points.length; i += 1) ctx.lineTo(points[i].x, points[i].y);
  ctx.stroke();
  ctx.restore();

  const radius = BALL_R + 10;
  ctx.lineWidth = 5;
  ctx.strokeStyle = 'rgba(16, 26, 36, 0.5)';
  ctx.beginPath();
  ctx.arc(ball.x, ball.y, radius, -Math.PI / 2, Math.PI * 1.5);
  ctx.stroke();
  if (power > 0.02) {
    ctx.strokeStyle = power > 0.78 ? palette.flag : palette.brass;
    ctx.beginPath();
    ctx.arc(
      ball.x,
      ball.y,
      radius,
      -Math.PI / 2,
      -Math.PI / 2 + power * Math.PI * 2,
    );
    ctx.stroke();
  }
}

function drawParticles(ctx: CanvasRenderingContext2D, particles: Particle[]): void {
  for (const p of particles) {
    ctx.globalAlpha = Math.max(0, p.life / p.maxLife);
    ctx.fillStyle = p.color;
    ctx.beginPath();
    ctx.arc(p.x, p.y, p.size, 0, Math.PI * 2);
    ctx.fill();
  }
  ctx.globalAlpha = 1;
}

function drawRough(ctx: CanvasRenderingContext2D, width: number, height: number): void {
  ctx.setTransform(1, 0, 0, 1, 0, 0);
  ctx.fillStyle = palette.rough;
  ctx.fillRect(0, 0, width, height);
  ctx.strokeStyle = palette.roughAlt;
  ctx.lineWidth = 14;
  ctx.globalAlpha = 0.55;
  ctx.beginPath();
  for (let x = -height; x < width + height; x += 44) {
    ctx.moveTo(x, 0);
    ctx.lineTo(x + height, height);
  }
  ctx.stroke();
  ctx.globalAlpha = 1;
}

export interface SceneInput {
  level: Level;
  ball: Ball;
  time: number;
  view: ViewTransform;
  shakeX: number;
  shakeY: number;
  dpr: number;
  width: number;
  height: number;
  aiming: boolean;
  aimAngle: number;
  aimPower: number;
  showGuide: boolean;
  particles: Particle[];
  trail: Vec[];
}

export function drawScene(ctx: CanvasRenderingContext2D, input: SceneInput): void {
  const { level, view, dpr, width, height } = input;

  ctx.setTransform(1, 0, 0, 1, 0, 0);
  ctx.clearRect(0, 0, width, height);
  drawRough(ctx, width, height);

  applyTransform(ctx, view, level.h, dpr);
  // Shake is authored in screen pixels; convert into course units here.
  ctx.translate(input.shakeX / view.scale, input.shakeY / view.scale);

  ctx.save();
  ctx.beginPath();
  ctx.rect(0, 0, level.w, level.h);
  ctx.clip();

  if (level.land) {
    drawWater(ctx, level, input.time);
    drawShore(ctx, level);
    ctx.save();
    ctx.beginPath();
    for (const rect of level.land) ctx.rect(rect.x, rect.y, rect.w, rect.h);
    ctx.clip();
    fillFairway(ctx, level.w, level.h);
    ctx.restore();
  } else {
    fillFairway(ctx, level.w, level.h);
  }

  drawSand(ctx, level);
  ctx.restore();

  drawRails(ctx, level);
  drawBumpers(ctx, level);
  drawSpinners(ctx, level, input.time);
  drawCup(ctx, level, input.time);

  if (input.trail.length > 1) drawTrail(ctx, input.trail);
  if (input.aiming && input.showGuide) {
    drawAim(ctx, level, input.ball, input.aimAngle, input.aimPower, input.time);
  }
  drawBall(ctx, input.ball);
  drawParticles(ctx, input.particles);
}