import type { InteractionState } from './states'; export interface PointerBounds { left: number; top: number; width: number; height: number; } export interface InteractionConfig { enabled?: boolean; landingDuration?: number; petDelay?: number; petLinger?: number; petSoundInterval?: number; pickupSound?: string; dropSound?: string; landingSound?: string; purrSound?: string; purrVolume?: number; /** Hard cap on throw speed, px/s. Prevents a fast flick launching the cat off-screen. */ maxThrowVelocity?: number; /** Cursor-velocity smoothing, 0–1. Higher = twitchier throws, lower = smoother. */ throwSmoothing?: number; } export const INTERACTION: InteractionConfig = { enabled: true, landingDuration: 0.5, petDelay: 0.25, petLinger: 0.3, petSoundInterval: 0.9, pickupSound: 'pickup', dropSound: 'drop', landingSound: 'landing', purrSound: 'purr', purrVolume: 0.35, maxThrowVelocity: 1200, throwSmoothing: 0.35 }; function clamp(v: number, lo: number, hi: number): number { return Math.min(Math.max(v, lo), hi); } function lerp(a: number, b: number, t: number): number { return a + (b - a) * t; } export class PointerController { cursorX = 0; cursorY = 0; state: InteractionState = 'free'; timeInState = 0; onGrab?: () => void; onRelease?: () => void; onLanding?: () => void; onPetStart?: () => void; onPetSound?: () => void; private cfg: Required< Pick< InteractionConfig, | 'landingDuration' | 'petDelay' | 'petLinger' | 'petSoundInterval' | 'maxThrowVelocity' | 'throwSmoothing' > > & { enabled: boolean }; private bounds: PointerBounds | null = null; private grabAnchor: { x: number; y: number } | null = null; private grabPointer = { x: 0, y: 0 }; private hoverTime = 0; private lingerTime = 0; private purrTimer = 0; private listeners: Array<[string, EventListener]> = []; // Cursor velocity tracking (screen coordinates, px/s). private prevCursorX = 0; private prevCursorY = 0; private prevCursorSet = false; private smoothVX = 0; private smoothVY = 0; constructor(cfg: InteractionConfig) { this.cfg = { enabled: cfg.enabled ?? true, landingDuration: cfg.landingDuration ?? 0.5, petDelay: cfg.petDelay ?? 0.25, petLinger: cfg.petLinger ?? 0.3, petSoundInterval: cfg.petSoundInterval ?? 0.9, maxThrowVelocity: cfg.maxThrowVelocity ?? 1200, throwSmoothing: clamp(cfg.throwSmoothing ?? 0.35, 0.05, 1) }; } attach(target: Window): void { if (!this.cfg.enabled) return; const down: EventListener = (e) => this.onPointerDown(e as PointerEvent); const up: EventListener = () => this.onPointerUp(); const move: EventListener = (e) => this.onPointerMove(e as PointerEvent); const leave: EventListener = () => this.onPointerLeave(); const pairs: Array<[string, EventListener]> = [ ['pointerdown', down], ['pointerup', up], ['pointermove', move], ['pointerleave', leave] ]; for (const [type, fn] of pairs) target.addEventListener(type, fn); this.listeners = pairs; } detach(target: Window): void { for (const [type, fn] of this.listeners) target.removeEventListener(type, fn); this.listeners = []; } setBounds(b: PointerBounds): void { this.bounds = b; } setGrabAnchor(x: number, y: number): void { this.grabAnchor = { x, y }; } getHeldPosition(): { x: number; y: number } | null { if (this.state !== 'held' || !this.grabAnchor) return null; const dx = this.cursorX - this.grabPointer.x; const dy = this.cursorY - this.grabPointer.y; return { x: this.grabAnchor.x + dx, y: this.grabAnchor.y - dy }; } /** * Cursor velocity at the moment of release, converted to internal coordinates. * `x` matches screen x; `y` is flipped because internal y grows upward. */ getThrowVelocity(): { vx: number; vy: number } { const cap = this.cfg.maxThrowVelocity; return { vx: clamp(this.smoothVX, -cap, cap), vy: clamp(-this.smoothVY, -cap, cap) }; } setState(s: InteractionState): void { if (this.state === s) return; this.state = s; this.timeInState = 0; if (s === 'held') this.onGrab?.(); if (s === 'falling') this.onRelease?.(); if (s === 'landing') this.onLanding?.(); if (s === 'petted') { this.purrTimer = 0; this.onPetStart?.(); } } update(dt: number): void { this.timeInState += dt; // Cursor velocity tracking. Only meaningful while held; otherwise decay // to zero so a stale flick doesn't linger between grabs. if (this.state === 'held' && this.prevCursorSet && dt > 0) { const rawVX = (this.cursorX - this.prevCursorX) / dt; const rawVY = (this.cursorY - this.prevCursorY) / dt; const a = this.cfg.throwSmoothing; this.smoothVX = lerp(this.smoothVX, rawVX, a); this.smoothVY = lerp(this.smoothVY, rawVY, a); } else if (this.state !== 'held') { this.smoothVX = 0; this.smoothVY = 0; } this.prevCursorX = this.cursorX; this.prevCursorY = this.cursorY; this.prevCursorSet = true; if (this.state === 'landing' && this.timeInState >= this.cfg.landingDuration) { this.setState('free'); } if (this.state === 'free') { if (this.hoveringHead()) { this.hoverTime += dt; if (this.hoverTime >= this.cfg.petDelay) { this.hoverTime = 0; this.setState('petted'); } } else { this.hoverTime = 0; } } else { this.hoverTime = 0; } if (this.state === 'petted') { if (this.hoveringHead()) { this.lingerTime = 0; } else { this.lingerTime += dt; if (this.lingerTime >= this.cfg.petLinger) { this.lingerTime = 0; this.setState('free'); } } this.purrTimer += dt; if (this.purrTimer >= this.cfg.petSoundInterval) { this.purrTimer = 0; this.onPetSound?.(); } } } private hoveringHead(): boolean { const b = this.bounds; if (!b) return false; return ( this.cursorX >= b.left && this.cursorX <= b.left + b.width && this.cursorY >= b.top && this.cursorY <= b.top + b.height / 2 ); } private hoveringBody(): boolean { const b = this.bounds; if (!b) return false; return ( this.cursorX >= b.left && this.cursorX <= b.left + b.width && this.cursorY >= b.top && this.cursorY <= b.top + b.height ); } private onPointerDown(e: PointerEvent): void { if (this.state !== 'free' && this.state !== 'petted') return; this.cursorX = e.clientX; this.cursorY = e.clientY; if (!this.hoveringBody()) return; e.preventDefault(); this.grabPointer = { x: this.cursorX, y: this.cursorY }; this.setState('held'); } private onPointerUp(): void { if (this.state === 'held') this.setState('falling'); } private onPointerMove(e: PointerEvent): void { this.cursorX = e.clientX; this.cursorY = e.clientY; } private onPointerLeave(): void { if (this.state === 'held') this.setState('falling'); } }