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/engine.ts
import { LEVELS } from './levels.js';
import {
  emptyEvents,
  MAX_SHOT_SPEED,
  MIN_SHOT_SPEED,
  simulate,
} from './physics.js';
import { palette } from './palette.js';
import type { Ball, Level, Particle, Vec } from './types.js';

export type Phase = 'ready' | 'rolling' | 'sunk';

export interface UpdateOptions {
  effects: boolean;
  reduced: boolean;
}

const MAX_PARTICLES = 240;

export class Game {
  levelIndex = 0;
  holeStrokes: number[] = LEVELS.map(() => 0);
  currentStrokes = 0;
  ball: Ball = { x: 0, y: 0, vx: 0, vy: 0 };
  phase: Phase = 'ready';
  courseTime = 0;
  aimAngle = 0;
  aimPower = 0.45;
  aiming = false;
  particles: Particle[] = [];
  trail: Vec[] = [];
  shake = 0;
  message = '';
  messageTimer = 0;
  hintTimer = 0;

  private lastSafe: Vec = { x: 0, y: 0 };
  private trailClear = 0;

  constructor() {
    this.loadLevel(0);
  }

  get level(): Level {
    return LEVELS[this.levelIndex];
  }

  get isFinalHole(): boolean {
    return this.levelIndex === LEVELS.length - 1;
  }

  get totalStrokes(): number {
    let sum = 0;
    for (let i = 0; i < LEVELS.length; i += 1) {
      if (this.holeStrokes[i] > 0) sum += this.holeStrokes[i];
      else if (i === this.levelIndex) sum += this.currentStrokes;
    }
    return sum;
  }

  /** Par for the holes that have been started, so the running total is honest. */
  get startedPar(): number {
    let par = 0;
    for (let i = 0; i < LEVELS.length; i += 1) {
      if (this.holeStrokes[i] > 0 || i === this.levelIndex) par += LEVELS[i].par;
    }
    return par;
  }

  get vsPar(): number {
    return this.totalStrokes - this.startedPar;
  }

  loadLevel(index: number): void {
    this.levelIndex = Math.max(0, Math.min(LEVELS.length - 1, index));
    const level = this.level;
    this.ball = { x: level.tee.x, y: level.tee.y, vx: 0, vy: 0 };
    this.currentStrokes = 0;
    this.phase = 'ready';
    this.aiming = false;
    this.aimAngle = Math.atan2(level.cup.y - level.tee.y, level.cup.x - level.tee.x);
    this.aimPower = 0.45;
    this.particles = [];
    this.trail = [];
    this.lastSafe = { x: level.tee.x, y: level.tee.y };
    this.shake = 0;
    this.message = '';
    this.messageTimer = 0;
    this.hintTimer = 4.5;
  }

  restartHole(): void {
    this.loadLevel(this.levelIndex);
  }

  newRound(): void {
    this.holeStrokes = LEVELS.map(() => 0);
    this.loadLevel(0);
  }

  /** Move on after a hole. Returns false when the round is already over. */
  nextLevel(): boolean {
    if (this.isFinalHole) return false;
    this.loadLevel(this.levelIndex + 1);
    return true;
  }

  setAim(angle: number, power: number): void {
    this.aimAngle = angle;
    this.aimPower = Math.max(0, Math.min(1, power));
  }

  /** Commit the current aim. Returns false when the tap was too soft to count. */
  putt(): boolean {
    if (this.phase !== 'ready') return false;
    if (this.aimPower < 0.06) {
      this.aiming = false;
      return false;
    }
    const speed =
      MIN_SHOT_SPEED + this.aimPower * (MAX_SHOT_SPEED - MIN_SHOT_SPEED);
    this.lastSafe = { x: this.ball.x, y: this.ball.y };
    this.ball.vx = Math.cos(this.aimAngle) * speed;
    this.ball.vy = Math.sin(this.aimAngle) * speed;
    this.currentStrokes += 1;
    this.phase = 'rolling';
    this.aiming = false;
    this.trail = [];
    this.trailClear = 0;
    this.hintTimer = 0;
    return true;
  }

  /** Advance one frame. Returns true on the frame the ball drops. */
  update(dt: number, options: UpdateOptions): boolean {
    const events = emptyEvents();
    const canShake = options.effects && !options.reduced;

    if (this.phase !== 'sunk') {
      this.courseTime = simulate(this.ball, this.level, dt, this.courseTime, events);
    }

    if (events.wet) {
      this.handleWater(canShake, options.effects);
    } else if (events.sunk) {
      this.handleSink(options.effects);
      this.advanceTimers(dt);
      return true;
    }

    // Physics zeroes the velocity once the ball gives up; hand aiming back.
    if (this.phase === 'rolling' && Math.hypot(this.ball.vx, this.ball.vy) < 1e-6) {
      this.phase = 'ready';
      const { x, y } = this.level.cup;
      this.aimAngle = Math.atan2(y - this.ball.y, x - this.ball.x);
      this.aiming = false;
    }

    if (events.bumper && events.at) {
      if (canShake) this.shake = Math.max(this.shake, 9);
      if (options.effects) this.spawnBurst(events.at.x, events.at.y, 8, palette.brass);
    } else if (events.rail && events.at) {
      if (canShake) this.shake = Math.max(this.shake, 5);
      if (options.effects) this.spawnBurst(events.at.x, events.at.y, 5, palette.dust);
    }

    this.advanceTimers(dt);
    this.updateTrail(dt);
    this.updateParticles(dt);
    return false;
  }

  private handleWater(canShake: boolean, effects: boolean): void {
    const at = { x: this.ball.x, y: this.ball.y };
    if (effects) this.spawnSplash(at.x, at.y);
    if (canShake) this.shake = Math.max(this.shake, 11);
    this.currentStrokes += 1;
    this.ball.x = this.lastSafe.x;
    this.ball.y = this.lastSafe.y;
    this.ball.vx = 0;
    this.ball.vy = 0;
    this.phase = 'ready';
    this.trail = [];
    this.message = 'Water — one stroke penalty';
    this.messageTimer = 2.4;
  }

  private handleSink(effects: boolean): void {
    this.phase = 'sunk';
    this.holeStrokes[this.levelIndex] = this.currentStrokes;
    this.trail = [];
    this.message = '';
    this.messageTimer = 0;
    if (effects) {
      this.spawnBurst(this.level.cup.x, this.level.cup.y, 26, palette.flag);
      this.spawnBurst(this.level.cup.x, this.level.cup.y, 14, palette.brass);
    }
  }

  private advanceTimers(dt: number): void {
    this.shake = Math.max(0, this.shake - dt * 34);
    if (this.messageTimer > 0) this.messageTimer -= dt;
    if (this.hintTimer > 0) this.hintTimer -= dt;
  }

  private updateTrail(dt: number): void {
    if (this.phase === 'rolling') {
      const last = this.trail[this.trail.length - 1];
      if (!last || Math.hypot(this.ball.x - last.x, this.ball.y - last.y) > 7) {
        this.trail.push({ x: this.ball.x, y: this.ball.y });
        if (this.trail.length > 16) this.trail.shift();
      }
      this.trailClear = 0.45;
    } else if (this.trail.length > 0) {
      this.trailClear -= dt;
      if (this.trailClear <= 0) this.trail = [];
    }
  }

  private updateParticles(dt: number): void {
    if (this.particles.length === 0) return;
    for (const p of this.particles) {
      p.x += p.vx * dt;
      p.y += p.vy * dt;
      const damping = Math.max(0, 1 - p.drag * dt);
      p.vx *= damping;
      p.vy *= damping;
      p.life -= dt;
    }
    this.particles = this.particles.filter((p) => p.life > 0);
  }

  private spawnBurst(x: number, y: number, count: number, color: string): void {
    for (let i = 0; i < count; i += 1) {
      if (this.particles.length >= MAX_PARTICLES) return;
      const angle = Math.random() * Math.PI * 2;
      const speed = 45 + Math.random() * 110;
      const life = 0.3 + Math.random() * 0.35;
      this.particles.push({
        x,
        y,
        vx: Math.cos(angle) * speed,
        vy: Math.sin(angle) * speed,
        life,
        maxLife: life,
        size: 2 + Math.random() * 3,
        color,
        drag: 4.5,
      });
    }
  }

  private spawnSplash(x: number, y: number): void {
    for (let i = 0; i < 22; i += 1) {
      if (this.particles.length >= MAX_PARTICLES) return;
      const angle = Math.random() * Math.PI * 2;
      const speed = 60 + Math.random() * 150;
      const life = 0.35 + Math.random() * 0.4;
      this.particles.push({
        x,
        y,
        vx: Math.cos(angle) * speed,
        vy: Math.sin(angle) * speed,
        life,
        maxLife: life,
        size: 2 + Math.random() * 3.5,
        color: Math.random() > 0.4 ? palette.splash : palette.waterSpark,
        drag: 3.4,
      });
    }
  }
}