Download src/main.js from DJLougen/moonshot: direct link, hf CLI and curl.
- Browser
- Download file 24.9 kB
-
https://huggingface.co/spaces/DJLougen/moonshot/resolve/main/src/main.js
- Command line
-
hf download hf://spaces/DJLougen/moonshot/src/main.js
-
curl -L -o main.js https://huggingface.co/spaces/DJLougen/moonshot/resolve/main/src/main.js
24.9 kB
| // MOONSHOT — entry: wires world, ball/bat views, pure game state machine, audio, | |
| // DOM HUD, mouse aim + click swing, and the deterministic capture mode for QA. | |
| import * as THREE from 'three'; | |
| import { P } from './palette.js'; | |
| import { createWorld } from './world.js'; | |
| import { createBallView } from './ball.js'; | |
| import { createBatView, createReticle } from './bat.js'; | |
| import { createFX } from './fx.js'; | |
| import { createGame } from './game.js'; | |
| import { createAudio } from './audio.js'; | |
| import { mulberry32, SWING } from './physics.js'; | |
| import { LEVELS } from './levels.js'; | |
| import { PLANET, SIGN } from './dims.js'; | |
| const $ = (id) => document.getElementById(id); | |
| const params = new URLSearchParams(location.search); | |
| const CAPTURE = params.get('capture') === '1'; | |
| const BEAT = params.get('beat') || 'title'; | |
| // ---------------------------------------------------------------- renderer | |
| const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' }); | |
| renderer.setPixelRatio(Math.min(devicePixelRatio, 2)); | |
| renderer.setSize(innerWidth, innerHeight); | |
| $('game').appendChild(renderer.domElement); | |
| const scene = new THREE.Scene(); | |
| scene.background = new THREE.Color(P.night); | |
| scene.fog = new THREE.Fog(P.night, 60, 480); | |
| const camera = new THREE.PerspectiveCamera(55, innerWidth / innerHeight, 0.1, 700); | |
| const CAM_BASE = new THREE.Vector3(0, 1.7, 6.4); | |
| const CAM_LOOK = new THREE.Vector3(0, 1.25, -6); | |
| camera.position.copy(CAM_BASE); | |
| camera.lookAt(CAM_LOOK); | |
| // Lighting is owned by createWorld (hemisphere wash + planet-shine directional). | |
| // ---------------------------------------------------------------- actors | |
| const world = createWorld(scene); | |
| const ball = createBallView(scene); | |
| const bat = createBatView(scene); | |
| const reticle = createReticle(scene); | |
| const fx = createFX(scene); | |
| const audio = createAudio(); | |
| // R21/R22: real-scale collisions. The ball reads true feet and can carom off | |
| // the billboard, the outfield wall, or — on a perfectly-timed moonshot — the | |
| // planet itself. world.colliders is plain data; onImpact fires once per shot. | |
| ball.setColliders(world.colliders); | |
| let planetFlash = 0; | |
| ball.onImpact = (kind, pos) => { | |
| if (kind === 'planet') { | |
| stamp('planet'); | |
| fx.burst(pos, P.cream); | |
| fx.burst(pos, P.gold); | |
| fx.ring(pos); | |
| flashA = 1.0; | |
| shake = 0.7; | |
| planetFlash = 1.0; | |
| audio.cheer(); | |
| audio.fanfare(); | |
| world.cheer(); | |
| } else if (kind === 'sign') { | |
| stamp('sign'); | |
| fx.burst(pos, P.red); | |
| flashA = 0.5; | |
| shake = 0.4; | |
| audio.crack(0.7); | |
| audio.cheer(); | |
| world.cheer(); | |
| } else if (kind === 'crowd') { | |
| // R27: the ball plows into the seated crowd — fans duck and ripple, | |
| // the whole park roars. Distinct from a quiet over-wall landing. | |
| stamp('crowd'); | |
| fx.burst(pos, P.gold); | |
| fx.burst(pos, P.cream); | |
| shake = 0.4; | |
| audio.thud(); | |
| audio.cheer(); | |
| world.crowdHit(pos); | |
| world.cheer(); | |
| } else { | |
| stamp('wall'); | |
| fx.burst(pos, P.teal); | |
| shake = 0.3; | |
| audio.thud(); | |
| } | |
| }; | |
| // ---------------------------------------------------------------- HR follow-cam | |
| // On a home run the camera chases the ball out toward the planet, then eases | |
| // back to the catcher POV. A distance readout counts up in feet. | |
| let followT = 0; // seconds into the HR follow | |
| let following = false; | |
| let hrAwaitCam = false; // R25: HR result waits for the frame shift home | |
| let distanceShown = 0; // smoothed displayed feet | |
| const FOLLOW_DUR = 6.0; // chase the ball out, then ease home | |
| const camPos = new THREE.Vector3().copy(CAM_BASE); | |
| const camLook = new THREE.Vector3().copy(CAM_LOOK); | |
| const tmpV = new THREE.Vector3(); | |
| function startFollow() { | |
| following = true; | |
| followT = 0; | |
| distanceShown = 0; | |
| $('distance').hidden = false; | |
| // force reflow so the .show transition plays | |
| void $('distance').offsetWidth; | |
| $('distance').classList.add('show'); | |
| } | |
| function endFollow() { | |
| following = false; | |
| $('distance').classList.remove('show'); | |
| setTimeout(() => { if (!following) $('distance').hidden = true; }, 350); | |
| } | |
| function updateFollowCam(dt) { | |
| if (following) { | |
| followT += dt; | |
| const bp = ball.pos; | |
| // chase from behind-and-above the ball, looking out toward deep outfield | |
| tmpV.set(bp.x * 0.6, Math.max(2.2, bp.y + 1.6), bp.z + 7.5); | |
| const blend = THREE.MathUtils.clamp(followT / 0.5, 0, 1); // ease into the chase | |
| camPos.lerp(tmpV, 0.08 + 0.12 * blend); | |
| camLook.lerp(tmpV.set(bp.x, bp.y, bp.z - 4), 0.12); | |
| // count the distance up toward the live value | |
| const ft = ball.flightDistanceFt; | |
| distanceShown += (ft - distanceShown) * Math.min(1, dt * 6); | |
| $('distance-ft').textContent = Math.round(distanceShown); | |
| if (followT >= FOLLOW_DUR) endFollow(); | |
| } else { | |
| // ease back to the catcher POV | |
| camPos.lerp(CAM_BASE, Math.min(1, dt * 4)); | |
| camLook.lerp(CAM_LOOK, Math.min(1, dt * 4)); | |
| // R25: once the frame shift home is complete, let the pitcher wind up. | |
| if (hrAwaitCam && | |
| camPos.distanceTo(CAM_BASE) < 0.05 && | |
| camLook.distanceTo(CAM_LOOK) < 0.05) { | |
| hrAwaitCam = false; | |
| game.finishResult(); | |
| } | |
| } | |
| } | |
| // ---------------------------------------------------------------- game + DOM | |
| const rng = mulberry32(CAPTURE ? 1969 : ((Date.now() ^ (Math.random() * 0xffffffff)) >>> 0)); | |
| let shake = 0; | |
| let flashA = 0; | |
| let stampTimer = 0; | |
| let windupT = 0; | |
| // R23 QA: latest result's contact quality + spray; cleared on every other | |
| // event so __state only exposes them while that result is the live event. | |
| let lastQuality = null; | |
| let lastSpray = null; | |
| const STAMP_TEXT = { | |
| homerun: ['HOME RUN!', 'get that ball a telescope'], | |
| inplay: ['IN PLAY', 'just a single. swing harder.'], | |
| popup: ['POP UP', 'skied it. right at the third baseman.'], | |
| foul: ['FOUL BALL', 'into the seats. nobody caught it.'], | |
| whiff: ['WHIFF', 'nothing but night air'], | |
| strike: ['STRIKE', 'you just watched it go by'], | |
| ball: ['BALL', 'even the planet saw that one miss'], | |
| planet: ['MOON SHOT!', 'you hit the planet. literally.'], | |
| sign: ['OFF THE SIGN', 'the MOONSHOT board never stood a chance'], | |
| wall: ['OFF THE WALL', 'rattled the fence. almost.'], | |
| crowd: ['INTO THE CROWD', 'souvenir! the whole grandstand felt that one'], | |
| }; | |
| function showCard(id) { | |
| for (const c of document.querySelectorAll('#overlay .card')) c.hidden = c.id !== id; | |
| $('overlay').classList.toggle('dim', !!id); | |
| } | |
| function hideCards() { showCard(null); } | |
| function updateHUD() { | |
| const def = LEVELS[game.level]; | |
| $('level-num').textContent = `INNING ${game.level + 1}/${LEVELS.length}`; | |
| $('level-name').textContent = def?.name ?? ''; | |
| $('hr-count').textContent = `${game.hr} / ${def?.hr ?? 0}`; | |
| $('total-hr').textContent = `TOTAL ${game.totalHR}`; | |
| const dots = $('pitch-dots'); | |
| dots.innerHTML = ''; | |
| const total = def?.pitches ?? 0; | |
| for (let i = 0; i < total; i++) { | |
| const d = document.createElement('i'); | |
| if (i >= game.pitchesLeft) d.className = 'used'; | |
| dots.appendChild(d); | |
| } | |
| } | |
| function stamp(kind, sub) { | |
| const [main, fallback] = STAMP_TEXT[kind] ?? [kind.toUpperCase(), '']; | |
| $('stamp').textContent = main; | |
| $('stamp-sub').textContent = sub || fallback; | |
| $('stamp').className = `show ${kind}`; | |
| $('stamp-sub').className = 'show'; | |
| stampTimer = kind === 'homerun' ? 1.6 : 1.0; | |
| } | |
| function onEvent(type, data) { | |
| // expose the last result's judgment to capture QA; any other event clears it | |
| lastQuality = null; | |
| lastSpray = null; | |
| switch (type) { | |
| case 'intro': { | |
| const def = data.def; | |
| $('intro-name').textContent = `INNING ${data.level + 1} — ${def.name}`; | |
| $('intro-goal').textContent = `${def.hr} HOME RUNS IN ${def.pitches} PITCHES`; | |
| $('intro-mods').innerHTML = ''; | |
| for (const m of def.mods) { | |
| const chip = document.createElement('span'); | |
| chip.className = 'chip'; | |
| chip.textContent = m; | |
| $('intro-mods').appendChild(chip); | |
| } | |
| $('intro-blurb').textContent = def.blurb; | |
| $('hud').hidden = false; | |
| updateHUD(); | |
| showCard('intro-card'); | |
| break; | |
| } | |
| case 'windup': | |
| hideCards(); | |
| windupT = 0; | |
| hrAwaitCam = false; | |
| ball.reset(); | |
| reticle.setVisible(true); | |
| break; | |
| case 'release': | |
| world.pitcher.release(); | |
| audio.whoosh(); | |
| ball.showFlight(); | |
| break; | |
| case 'result': { | |
| updateHUD(); | |
| lastQuality = data.quality; | |
| lastSpray = data.spray; | |
| stamp(data.kind === 'popup' && data.foul ? 'foul' : data.kind); | |
| reticle.pulse(data.kind === 'homerun' || data.kind === 'inplay' || data.kind === 'popup'); | |
| audio.stamp(); | |
| if (data.kind === 'homerun') { | |
| hrAwaitCam = true; // R25: pitcher waits for the frame shift | |
| ball.launchHomeRun(data.pos, data.spray ?? 0, data.quality); | |
| fx.burst(data.pos, P.gold); | |
| fx.ring(data.pos); | |
| flashA = 0.85; | |
| shake = 0.5; | |
| audio.crack(data.quality); | |
| audio.cheer(); | |
| world.cheer(); | |
| startFollow(); | |
| } else if (data.kind === 'inplay') { | |
| ball.continuePast(data.pos, { x: 0, y: 2.2, z: 9 }); | |
| fx.burst(data.pos, P.teal); | |
| audio.crack(data.quality * 0.5); | |
| shake = 0.15; | |
| } else if (data.kind === 'popup') { | |
| ball.launchPopup(data.pos, data.foul); | |
| fx.burst(data.pos, P.cream); | |
| audio.crack(data.quality * 0.4); | |
| shake = 0.1; | |
| } else { | |
| ball.continuePast(data.pos, { x: 0, y: 0.4, z: 10 }); | |
| audio.thud(); | |
| } | |
| break; | |
| } | |
| case 'clear': | |
| $('clear-title').textContent = 'INNING CLEARED'; | |
| $('clear-stats').textContent = `${data.hr} moonshots · ${data.pitchesUsed} pitches used · ${data.totalHR} career`; | |
| showCard('clear-card'); | |
| audio.fanfare(); | |
| break; | |
| case 'victory': | |
| $('win-title').textContent = 'LEAGUE CHAMPION'; | |
| $('win-stats').textContent = `${data.totalHR} career home runs. The planet is still ringing.`; | |
| showCard('win-card'); | |
| audio.fanfare(); | |
| break; | |
| case 'over': | |
| $('over-title').textContent = 'OUT OF PITCHES'; | |
| $('over-stats').textContent = `${data.hr}/${data.target} home runs. The night league is unforgiving.`; | |
| showCard('over-card'); | |
| audio.gameOver(); | |
| break; | |
| } | |
| } | |
| const game = createGame({ rng, emit: onEvent }); | |
| // ---------------------------------------------------------------- input | |
| const aim = { x: 0, y: 1.0 }; | |
| const raycaster = new THREE.Raycaster(); | |
| // Last pointer position in NDC. step() re-projects it every frame while the | |
| // ball is in flight and the swing has NOT committed, so a stationary mouse | |
| // tracks the ball's LIVE depth plane. Once the swing commits the aim locks | |
| // (a committed swing is judged at the click), so depth drift during the swing | |
| // can't slide the aim off the ball. | |
| const ndc = new THREE.Vector2(); | |
| let pointerAim = false; | |
| // Aim projection plane: normal +z, constant set to the ball's live depth | |
| // during flight (z = ballPos.z < 0), the plate plane (z=0) otherwise. | |
| const aimPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0); | |
| const hit = new THREE.Vector3(); | |
| // World depth of the aim plane R23 is judged against: the ball's z while it | |
| // is in flight, the plate otherwise. | |
| function aimDepth() { | |
| return game.state === 'flight' ? game.ballPos.z : 0; | |
| } | |
| // Project the stored pointer NDC onto the live aim plane and push the result | |
| // to the game, reticle, and bat. Called on every pointer event AND once per | |
| // frame BEFORE game.update so a stationary pointer rides the ball's plane. | |
| function applyPointerAim() { | |
| aimPlane.constant = -aimDepth(); | |
| raycaster.setFromCamera(ndc, camera); | |
| if (raycaster.ray.intersectPlane(aimPlane, hit)) { | |
| aim.x = THREE.MathUtils.clamp(hit.x, -0.9, 0.9); | |
| aim.y = THREE.MathUtils.clamp(hit.y, 0.2, 2.0); | |
| game.setAim(aim.x, aim.y); | |
| reticle.setPoint(aim.x, aim.y, aimDepth()); | |
| bat.setAim(aim.x, aim.y); | |
| } | |
| } | |
| function aimFromEvent(e) { | |
| ndc.set((e.clientX / innerWidth) * 2 - 1, -(e.clientY / innerHeight) * 2 + 1); | |
| pointerAim = true; | |
| applyPointerAim(); | |
| } | |
| addEventListener('pointermove', aimFromEvent); | |
| renderer.domElement.addEventListener('pointerdown', (e) => { | |
| audio.unlock(); | |
| aimFromEvent(e); | |
| if (game.state === 'flight' && game.swing()) { | |
| pointerAim = false; // commit: judge the aim at the click, no depth drift | |
| bat.swing(); | |
| audio.whoosh(); | |
| } | |
| }); | |
| // keyboard accessibility: arrows nudge aim in world x/y on the live depth | |
| // plane, space swings, enter activates cards. Arrow input takes aim back | |
| // from the pointer until the next pointer event. | |
| addEventListener('keydown', (e) => { | |
| if (e.key === 'ArrowLeft') { aim.x = Math.max(-0.9, aim.x - 0.08); pointerAim = false; } | |
| else if (e.key === 'ArrowRight') { aim.x = Math.min(0.9, aim.x + 0.08); pointerAim = false; } | |
| else if (e.key === 'ArrowUp') { aim.y = Math.min(2.0, aim.y + 0.08); pointerAim = false; } | |
| else if (e.key === 'ArrowDown') { aim.y = Math.max(0.2, aim.y - 0.08); pointerAim = false; } | |
| else if (e.key === ' ') { | |
| audio.unlock(); | |
| if (game.state === 'flight' && game.swing()) { pointerAim = false; bat.swing(); audio.whoosh(); } | |
| e.preventDefault(); | |
| return; | |
| } else return; | |
| game.setAim(aim.x, aim.y); | |
| reticle.setPoint(aim.x, aim.y, aimDepth()); | |
| bat.setAim(aim.x, aim.y); | |
| }); | |
| function wire(id, fn) { | |
| $(id).addEventListener('click', () => { audio.unlock(); audio.stamp(); fn(); }); | |
| } | |
| wire('btn-play', () => { hideCards(); game.newGame(); }); | |
| wire('btn-stepin', () => { hideCards(); game.play(); }); | |
| wire('btn-next', () => { hideCards(); game.startLevel(game.level + 1); }); | |
| wire('btn-retry', () => { hideCards(); game.retry(); }); | |
| wire('btn-new', () => { hideCards(); game.newGame(); }); | |
| wire('btn-again', () => { hideCards(); game.newGame(); }); | |
| $('btn-mute').addEventListener('click', () => { | |
| audio.unlock(); | |
| audio.setMuted(!audio.muted); | |
| $('btn-mute').textContent = audio.muted ? 'SOUND OFF' : 'SOUND ON'; | |
| }); | |
| // ---------------------------------------------------------------- loop | |
| const clock = new THREE.Clock(); | |
| let simTime = 0; | |
| function step(dt) { | |
| simTime += dt; | |
| // R23: a stationary pointer must track the ball's live depth plane, so the | |
| // judgment sees aim projected through the CURRENT ball position. | |
| if (pointerAim) applyPointerAim(); | |
| const prevState = game.state; | |
| game.update(dt); | |
| if (game.state === 'windup') { | |
| windupT += dt; | |
| world.pitcher.windup(Math.min(1, windupT / 0.8)); | |
| } | |
| if (prevState === 'windup' && game.state === 'flight') world.pitcher.reset(); | |
| if (game.state === 'flight') { | |
| ball.setPosition(game.ballPos); | |
| ball.setSpin(14); | |
| } | |
| bat.update(dt); | |
| ball.update(dt); | |
| reticle.update(dt); | |
| fx.update(dt); | |
| world.update(dt, simTime); | |
| updateFollowCam(dt); | |
| if (stampTimer > 0) { | |
| stampTimer -= dt; | |
| if (stampTimer <= 0) { $('stamp').className = ''; $('stamp-sub').className = ''; } | |
| } | |
| flashA = Math.max(0, flashA - dt * 2.4); | |
| $('flash').style.opacity = flashA.toFixed(3); | |
| shake = Math.max(0, shake - dt * 1.6); | |
| // R22: planet impact flash — a quick scale pulse that settles back to 1. | |
| if (planetFlash > 0) { | |
| planetFlash = Math.max(0, planetFlash - dt * 1.4); | |
| const s = 1 + 0.12 * Math.sin(planetFlash * Math.PI); | |
| world.planetGroup.scale.setScalar(s); | |
| } | |
| } | |
| function render() { | |
| const jx = CAPTURE ? Math.sin(simTime * 91.7) : Math.random() - 0.5; | |
| const jy = CAPTURE ? Math.cos(simTime * 73.3) : Math.random() - 0.5; | |
| const sx = jx * shake * 0.22; | |
| const sy = jy * shake * 0.22; | |
| camera.position.set(camPos.x + sx, camPos.y + sy, camPos.z); | |
| camera.lookAt(camLook); | |
| renderer.render(scene, camera); | |
| } | |
| function loop() { | |
| requestAnimationFrame(loop); | |
| step(Math.min(clock.getDelta(), 0.05)); | |
| render(); | |
| } | |
| addEventListener('resize', () => { | |
| camera.aspect = innerWidth / innerHeight; | |
| camera.updateProjectionMatrix(); | |
| renderer.setSize(innerWidth, innerHeight); | |
| }); | |
| // ---------------------------------------------------------------- capture mode | |
| if (CAPTURE) { | |
| // Direct aim, bypassing the pointer. Optional z overrides the reticle's | |
| // depth; default tracks the live aim plane. Also hands aim back to script | |
| // control (stops the per-frame stationary-pointer reapply). | |
| window.__aim = (x, y, z = aimDepth()) => { | |
| pointerAim = false; | |
| aim.x = x; aim.y = y; | |
| game.setAim(x, y); | |
| reticle.setPoint(x, y, z); | |
| bat.setAim(x, y); | |
| }; | |
| window.__swing = () => { if (game.swing()) { bat.swing(); return true; } return false; }; | |
| window.__click = (id) => $(id).click(); | |
| window.__frame = (n = 1) => { for (let i = 0; i < n; i++) step(1 / 60); render(); return game.state; }; | |
| window.__step = (dt) => { step(dt); render(); return game.state; }; | |
| // QA: render one frame and report the renderer's draw stats (capture-only). | |
| window.__renderInfo = () => { | |
| render(); | |
| const { calls, triangles } = renderer.info.render; | |
| return { calls, triangles }; | |
| }; | |
| // Project a world point (default: the live ball) through the currently | |
| // rendered camera → CSS pixels, for CDP real-pointer clicks. Returns | |
| // {x, y, pos:{x,y,z}, z} where pos is the world point and z its NDC depth. | |
| window.__screenPoint = (pos = game.ballPos) => { | |
| camera.updateMatrixWorld(); | |
| const v = new THREE.Vector3(pos.x, pos.y, pos.z).project(camera); | |
| return { | |
| x: (v.x * 0.5 + 0.5) * innerWidth, | |
| y: (-v.y * 0.5 + 0.5) * innerHeight, | |
| pos: { x: pos.x, y: pos.y, z: pos.z }, | |
| z: v.z, | |
| }; | |
| }; | |
| Object.defineProperty(window, '__state', { | |
| get: () => ({ | |
| state: game.state, level: game.level, hr: game.hr, | |
| pitchesLeft: game.pitchesLeft, totalHR: game.totalHR, | |
| flightT: game.flightT, duration: game.pitch?.duration ?? 0, | |
| ballPos: game.ballPos, resultKind: game.resultKind, | |
| distanceFt: ball.flightDistanceFt, following, | |
| impactKind: ball.impactKind, | |
| camHome: !following && camPos.distanceTo(CAM_BASE) < 0.05 && camLook.distanceTo(CAM_LOOK) < 0.05, | |
| ballX: ball.pos.x, ballY: ball.pos.y, | |
| lastQuality, lastSpray, | |
| }), | |
| }); | |
| Object.defineProperty(window, '__pitchTarget', { get: () => game.pitch?.target ?? null }); | |
| // R23/R25 QA precision: a frame-aligned swing quantizes timingErr to ~0.13–0.15 s, | |
| // capping quality at ~0.968 — just under the 0.97 moonshot band. Perfect-play | |
| // scripts advance sub-frame to EXACTLY duration - ideal, then aim dead-on the | |
| // ball's live position → quality 1.0. | |
| // __advanceToIdeal(): advance to exactly pitch.duration - SWING.ideal — | |
| // full 1/60 steps, then the exact sub-frame remainder — render, and return | |
| // whether the game is still in flight. Advances only: no aim, no swing. | |
| window.__advanceToIdeal = () => { | |
| if (game.state !== 'flight' || game.swingUsed) return false; | |
| const p = game.pitch; | |
| while (game.state === 'flight' && game.flightT + 1 / 60 < p.duration - SWING.ideal) step(1 / 60); | |
| if (game.state !== 'flight') return false; | |
| const rem = (p.duration - SWING.ideal) - game.flightT; | |
| if (rem > 1e-9) step(rem); | |
| render(); | |
| return game.state === 'flight'; | |
| }; | |
| // __perfectSwing(off, yOff): advance to the ideal instant, then aim at the | |
| // ball's live position + (off, yOff) and swing. Quality uses hypot(off,yOff); | |
| // spray uses ONLY the x offset (off/0.4). So a purely VERTICAL offset | |
| // (off=0, yOff=0.25 → q~0.85, spray=0) drives the centered billboard-sign | |
| // collision arm without pulling the ball to right field. Call during flight. | |
| window.__perfectSwing = (off = 0, yOff = 0) => { | |
| if (!window.__advanceToIdeal()) return false; | |
| window.__aim(game.ballPos.x + off, game.ballPos.y + yOff); | |
| return window.__swing(); | |
| }; | |
| const perfectSwing = window.__perfectSwing; // beats below keep their calls | |
| // Scripted beats: deterministic setup, then the driver steps frames. | |
| const script = { | |
| title() { showCard('title-card'); }, | |
| pitch() { | |
| hideCards(); game.newGame(); game.play(); | |
| // step through windup into mid-flight | |
| for (let i = 0; i < 200 && game.state !== 'flight'; i++) step(1 / 60); | |
| const target = (game.pitch?.duration ?? 1.5) * 0.55; | |
| for (let i = 0; i < 200 && game.flightT < target; i++) step(1 / 60); | |
| window.__aim(game.pitch?.target.x ?? 0, game.pitch?.target.y ?? 1); | |
| render(); | |
| }, | |
| homerun() { | |
| hideCards(); game.newGame(); game.play(); | |
| for (let i = 0; i < 200 && game.state !== 'flight'; i++) step(1 / 60); | |
| window.__aim(game.pitch.target.x, game.pitch.target.y); | |
| // R23: sub-frame perfect timing + dead-on live aim → quality 1.0 | |
| perfectSwing(); | |
| // a few frames into the result so stamp + burst are visible | |
| for (let i = 0; i < 12; i++) step(1 / 60); | |
| render(); | |
| }, | |
| 'homerun-follow'() { | |
| hideCards(); game.newGame(); game.play(); | |
| for (let i = 0; i < 200 && game.state !== 'flight'; i++) step(1 / 60); | |
| window.__aim(game.pitch.target.x, game.pitch.target.y); | |
| perfectSwing(); | |
| // ~1.4s into the result: camera mid-chase, distance counting up | |
| for (let i = 0; i < 84; i++) step(1 / 60); | |
| render(); | |
| }, | |
| planet() { | |
| hideCards(); game.newGame(); game.play(); | |
| for (let i = 0; i < 200 && game.state !== 'flight'; i++) step(1 / 60); | |
| window.__aim(game.pitch.target.x, game.pitch.target.y); | |
| // perfect timing + perfect aim → quality 1.0 → moonshot at the planet | |
| perfectSwing(); | |
| // step until the ball reaches the planet (~5s) or impact fires | |
| for (let i = 0; i < 360 && !ball.impactKind; i++) step(1 / 60); | |
| for (let i = 0; i < 10; i++) step(1 / 60); | |
| render(); | |
| }, | |
| crowd() { | |
| hideCards(); game.newGame(); game.play(); | |
| for (let i = 0; i < 200 && game.state !== 'flight'; i++) step(1 / 60); | |
| // R27: off-center, non-perfect contact (off=0.22 → q~0.87, spray~0.55) | |
| // carries ~418 ft and descends INTO the seated grandstand → 'crowd'. | |
| perfectSwing(0.22); | |
| // step until the ball hits the crowd (~3.9 s) or impact fires | |
| for (let i = 0; i < 360 && !ball.impactKind; i++) step(1 / 60); | |
| // a few frames into the impact so the flinch ripple + burst are visible | |
| for (let i = 0; i < 12; i++) step(1 / 60); | |
| render(); | |
| }, | |
| popup() { | |
| hideCards(); game.newGame(); game.play(); | |
| for (let i = 0; i < 200 && game.state !== 'flight'; i++) step(1 / 60); | |
| const dur = game.pitch.duration; | |
| // R23: offset from the ball's LIVE position (judged at click) + swing | |
| // early → weak contact → pop-up. Re-aim live just before the swing below. | |
| window.__aim(game.pitch.target.x + 0.4, game.pitch.target.y); | |
| for (let i = 0; i < 600 && game.state === 'flight'; i++) { | |
| if (!game.swingUsed && dur - game.flightT <= 0.06) { window.__aim(game.ballPos.x + 0.4, game.ballPos.y); window.__swing(); } | |
| step(1 / 60); | |
| } | |
| // a few frames into the result so the pop-up arc is visible | |
| for (let i = 0; i < 26; i++) step(1 / 60); | |
| render(); | |
| }, | |
| clear() { | |
| hideCards(); game.newGame(); | |
| for (let p = 0; p < 12 && game.state !== 'clear'; p++) { | |
| if (game.state === 'intro') game.play(); | |
| for (let i = 0; i < 200 && game.state !== 'flight'; i++) step(1 / 60); | |
| if (game.state !== 'flight') break; | |
| window.__aim(game.pitch.target.x, game.pitch.target.y); | |
| perfectSwing(); | |
| // finish the remaining ~0.14 s of flight to the crossing | |
| for (let i = 0; i < 60 && game.state === 'flight'; i++) step(1 / 60); | |
| // HR results wait for the camera to ease home (R25) — step until the | |
| // game leaves 'result' (camera gate or the 12s safety cap). | |
| for (let i = 0; i < 600 && game.state === 'result'; i++) step(1 / 60); | |
| } | |
| render(); | |
| }, | |
| spray() { | |
| hideCards(); game.newGame(); game.play(); | |
| for (let i = 0; i < 200 && game.state !== 'flight'; i++) step(1 / 60); | |
| // aim to the RIGHT of the ball → solid contact, pulled to right field | |
| window.__aim(game.pitch.target.x + 0.22, game.pitch.target.y); | |
| perfectSwing(0.22); | |
| // finish the remaining ~0.14 s of flight to the crossing | |
| for (let i = 0; i < 60 && game.state === 'flight'; i++) step(1 / 60); | |
| // step out to the landing so the spray direction is visible | |
| for (let i = 0; i < 300 && game.state === 'result'; i++) step(1 / 60); | |
| render(); | |
| }, | |
| over() { | |
| hideCards(); game.newGame(); game.play(); | |
| // never swing: burn all pitches | |
| for (let i = 0; i < 60 * 90 && game.state !== 'over'; i++) { | |
| if (game.state === 'intro') game.play(); | |
| step(1 / 60); | |
| } | |
| render(); | |
| }, | |
| }; | |
| (script[BEAT] ?? script.title)(); | |
| window.__ready = true; | |
| } else { | |
| showCard('title-card'); | |
| loop(); | |
| } |