Lavori in corso (non pubblicati): Bertovo al tavolo di mezzo coi rinquarti (Codex), Bini, Yuri, il Gianno e la Brauda in gara, l'Audi «Let it be», sensibilità e riquadro dello swipe, pulsanti su iPhone (Andrea)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GiECdCPptNRwbVzjzHbohi
This commit is contained in:
@@ -24,8 +24,8 @@ export function tablePatches() {
|
||||
}
|
||||
|
||||
// Ombra rivolta verso l'esterno rispetto alla lampada centrale.
|
||||
function sphere(ctx, role, x, y, r, shadow = true) {
|
||||
const [dark, mid, light] = BALL[role];
|
||||
export function sphere(ctx, role, x, y, r, shadow = true) {
|
||||
const [dark, mid, light] = BALL[role] ?? ['#43285f', '#bd83ef', '#f7e4ff'];
|
||||
const lx = shadow ? (sx(LEN / 2) - x) / (LEN * VIEW.k / 2) : -0.6;
|
||||
const ly = shadow ? (sy(WID / 2) - y) / (WID * VIEW.k / 2) - 0.25 : -0.6;
|
||||
if (shadow) {
|
||||
@@ -50,7 +50,7 @@ export function pinFall(p, time) {
|
||||
if (p.visualDownAt === undefined) return 1;
|
||||
return Math.min(1, Math.max(0, (time - p.visualDownAt) / 0.24));
|
||||
}
|
||||
function pin(ctx, p, time) {
|
||||
export function pin(ctx, p, time) {
|
||||
const x = sx(p.x), y = sy(p.y), fall = pinFall(p, time);
|
||||
const tilt = Math.sin(fall * Math.PI / 2), lift = Math.cos(fall * Math.PI / 2);
|
||||
const tx = x + Math.cos(p.dir ?? 0) * 7 * tilt;
|
||||
@@ -65,7 +65,7 @@ function pin(ctx, p, time) {
|
||||
}
|
||||
|
||||
let lighting;
|
||||
function table(ctx, bg) {
|
||||
export function table(ctx, bg) {
|
||||
if (bg) for (const patch of tablePatches()) ctx.drawImage(bg, ...patch);
|
||||
else {
|
||||
ctx.fillStyle = '#21170f'; ctx.fillRect(0, 0, 640, 360);
|
||||
@@ -117,7 +117,7 @@ function aimGuide(ctx, c) {
|
||||
}
|
||||
}
|
||||
|
||||
function cue(ctx, x, y, angle, back, alpha = 1) {
|
||||
export function cue(ctx, x, y, angle, back, alpha = 1) {
|
||||
ctx.save(); ctx.globalAlpha = alpha;
|
||||
ctx.translate(sx(x), sy(y)); ctx.rotate(angle);
|
||||
ctx.fillStyle = '#25170e'; ctx.fillRect(-back - 150, -2, 150, 5);
|
||||
|
||||
@@ -20,7 +20,7 @@ const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
||||
function random(c) { c.seed = (Math.imul(c.seed, 1664525) + 1013904223) >>> 0; return c.seed / 4294967296; }
|
||||
const deviation = c => random(c) + random(c) + random(c) - 1.5;
|
||||
const ball = (role, x, y) => ({ role, x, y, vx: 0, vy: 0 });
|
||||
const list = c => [c.balls.player, c.balls.cpu, c.balls.pallino];
|
||||
const list = c => Object.values(c.balls);
|
||||
const angleTo = (a, b) => Math.atan2(b.y - a.y, b.x - a.x);
|
||||
|
||||
// Il castello: il rosso in mezzo al tavolo, i quattro bianchi a croce intorno.
|
||||
@@ -179,12 +179,12 @@ function strike(c, who, angle, power) {
|
||||
return true;
|
||||
}
|
||||
// Il colpo provato a vuoto, su una copia del tavolo: serve all'avversario per scegliere.
|
||||
export function simulate(c, who, angle, power) {
|
||||
export function simulate(c, who, angle, power, { onContact, stop } = {}) {
|
||||
const balls = list(c).map(b => ({ ...b })), pins = c.pins.map(p => ({ ...p }));
|
||||
const w = { balls, pins, shot: { shooter: who, first: null, pallino: false, pins: [] } };
|
||||
const w = { balls, pins, onContact, shot: { shooter: who, first: null, pallino: false, pins: [] } };
|
||||
const b = balls.find(x => x.role === who), v = speedFor(power);
|
||||
b.vx = Math.cos(angle) * v; b.vy = Math.sin(angle) * v;
|
||||
for (let i = 0; i < 15 / FIXED_STEP && balls.some(x => x.vx || x.vy); i++) step(w, FIXED_STEP);
|
||||
for (let i = 0; i < 15 / FIXED_STEP && !stop?.() && balls.some(x => x.vx || x.vy); i++) step(w, FIXED_STEP);
|
||||
return judge(w.shot);
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ function cpuThink(c, budget = 12) {
|
||||
c.angle = c.plan.angle; c.power = c.plan.power; c.thinking = null;
|
||||
}
|
||||
|
||||
export function update(c, dt) {
|
||||
export function update(c, dt, onContact) {
|
||||
if (!Number.isFinite(dt) || dt <= 0) return;
|
||||
dt = Math.min(dt, 0.25); c.t += dt;
|
||||
c.visual.time += dt;
|
||||
@@ -255,7 +255,7 @@ export function update(c, dt) {
|
||||
}
|
||||
if (c.step === 'rolling' && !c.windup) {
|
||||
c.accumulator += dt;
|
||||
const w = { balls: list(c), pins: c.pins, shot: c.shot, events: c.events };
|
||||
const w = { balls: list(c), pins: c.pins, shot: c.shot, events: c.events, onContact };
|
||||
while (c.accumulator >= FIXED_STEP) { step(w, FIXED_STEP); c.accumulator -= FIXED_STEP; }
|
||||
if (!moving(c)) { c.step = 'settle'; c.t = 0; }
|
||||
} else if (c.step === 'settle' && c.t >= REST) finish(c);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { findPath, dist, inBox, boxDistance, nearestInside } from './geometry.js';
|
||||
import * as bocce from './bocce.js';
|
||||
import * as biliardo from './biliardo.js';
|
||||
import * as rinquarto from './rinquarto.js';
|
||||
import * as basket from './basket.js';
|
||||
import * as pesca from './pesca.js';
|
||||
import * as briscola from './briscola.js';
|
||||
@@ -17,6 +18,7 @@ export const MINIGAMES = {
|
||||
scopa: { create: (seed, level, deck) => scopa.newScopa(seed, { level, deck }), update: scopa.update, play: scopa.play },
|
||||
bocce: { create: (seed, level) => bocce.newBocce(seed, { level }), update: bocce.update, press: bocce.press, release: bocce.release, cancel: bocce.cancel, aim: bocce.aimScreen },
|
||||
biliardo: { create: (seed, level) => biliardo.newBiliardo(seed, { level }), update: biliardo.update, press: biliardo.press, release: biliardo.release, cancel: biliardo.cancel, aim: biliardo.aimScreen },
|
||||
rinquarto: { create: (seed, level) => rinquarto.newRinquarto(seed, { level }), update: rinquarto.update, press: rinquarto.press, release: rinquarto.release, cancel: rinquarto.cancel, aim: rinquarto.aimScreen },
|
||||
basket: { create: (seed, level) => basket.newBasket(seed, { level }), update: basket.update, press: basket.press, release: basket.release, cancel: basket.cancel, aim: basket.aimScreen },
|
||||
pesca: { create: (seed, level) => pesca.newPesca(seed, { level }), update: pesca.update, press: pesca.press, release: pesca.release, cancel: pesca.cancel, aim: pesca.aimScreen },
|
||||
gara: { create: (seed, level, deck, def) => gara.newGara(seed, { level, riders: def.riders }), update: gara.update, press: gara.press, release: gara.release, cancel: gara.cancel, aim: gara.aimScreen },
|
||||
|
||||
+17
-8
@@ -29,7 +29,10 @@ export const LAPS = 3, STEP = 2, ROAD = 9, VERGE = 15, COUNTDOWN = 3, FIXED_STEP
|
||||
export const INTRO = { each: 4, ride: 1.8, from: 110, after: 1 };
|
||||
// Sterzo (rad/s a velocità piena di sterzo), freno, rallentamento senza gas, e quanto si scivola di lato in curva.
|
||||
export const TURN = 3.4, BRAKE = 150, COAST = 45, GRIP = 9, SLIDE = 0.5;
|
||||
export const SWIPE = { dead: 10, reach: 60, gain: 2.6 };
|
||||
// Lo swipe: zona morta e portata in pixel della scena; la sensibilità (0..1, cursore del pannello) va dal guadagno
|
||||
// `soft` al `hard`, e lo sterzo del dito insegue quello voluto a `follow` al secondo (niente scatti). Sul telefono
|
||||
// la prima versione era ingiocabile: si parte poco sensibili.
|
||||
export const SWIPE = { dead: 14, reach: 60, soft: 1.2, hard: 3, follow: 7, sens: 0.3 };
|
||||
export const BOOST = { speed: 1.35, time: 1.2 }, CAN = { time: 1.6, respawn: 6 }, HOLE = { fast: 45, out: 1.1 };
|
||||
const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
||||
const wrapAngle = a => Math.atan2(Math.sin(a), Math.cos(a));
|
||||
@@ -49,7 +52,7 @@ export const RIDERS = [
|
||||
];
|
||||
// I livelli: quanto vanno gli altri, quanto si resta fermi nella paglia, e la guida disegnata in pista.
|
||||
export const LEVELS = {
|
||||
bona: { rivals: 0.78, stun: 0.4, guide: 'zone' },
|
||||
bona: { rivals: 0.74, stun: 0.4, guide: 'zone' },
|
||||
seria: { rivals: 0.84, stun: 0.9, guide: 'coni' },
|
||||
male: { rivals: 0.93, stun: 1.4, guide: null },
|
||||
};
|
||||
@@ -152,16 +155,18 @@ export function newGara(seed = Date.now() >>> 0, def = {}) {
|
||||
if (!riders.some(r => r.id === 'andrea')) throw new Error('Nella gara manca Andrea');
|
||||
for (const r of riders) { r.grid = r.s; r.shown = false; }
|
||||
return { step: 'intro', t: 0, age: 0, seed, level, skill, riders, holding: false, braking: false, keys: { left: false, right: false, gas: new Set(), brake: false },
|
||||
pointer: null, toggle: false, brakeButton: false, drag: 0, buttons: 0, anchor: null, finger: null,
|
||||
power: 0, accumulator: 0, events: [], result: null, order: riders.map(r => r.id), taken: {} };
|
||||
pointer: null, toggle: false, brakeButton: false, drag: 0, buttons: 0, anchor: null, finger: null, sens: SWIPE.sens,
|
||||
power: SWIPE.sens, accumulator: 0, events: [], result: null, order: riders.map(r => r.id), taken: {} };
|
||||
}
|
||||
export const player = c => c.riders.find(r => r.id === 'andrea');
|
||||
export const lapOf = r => clamp(Math.floor(Math.max(0, r.s) / TRACK.length) + 1, 1, LAPS);
|
||||
export const canThrow = c => c.step === 'intro' || c.step === 'start' || c.step === 'race';
|
||||
export const canAdjust = c => c.step === 'race'; // lo sterzo del pannello
|
||||
// Lo sterzo e il freno del pannello, e il cursore della sensibilità (regolabile già prima del via).
|
||||
export const canAdjust = c => c.step !== 'result';
|
||||
export const aimFree = () => true;
|
||||
export const nudge = () => false;
|
||||
export const setPower = () => false;
|
||||
// Il cursore del pannello, nella gara, è la sensibilità dello sterzo col dito.
|
||||
export function setPower(c, value) { c.sens = c.power = clamp(Number(value) || 0, 0, 1); return true; }
|
||||
|
||||
// Il gas ha più fonti, ognuna con la sua: i tasti tenuti giù (Spazio, Invio, ↑), il dito o il mouse premuto sulla
|
||||
// scena (uno solo, col suo pointerId) e il pulsante Gas del pannello, che resta acceso finché non lo si ritocca.
|
||||
@@ -238,12 +243,16 @@ export function key(c, name, down) {
|
||||
export function steerButton(c, dir) { c.buttons = dir; return true; }
|
||||
export function brakeButton(c, down) { c.brakeButton = down; sync(c); return true; }
|
||||
export function steering(c) {
|
||||
c.drag = 0;
|
||||
let want = 0;
|
||||
if (c.pointer !== null && c.anchor && c.finger) {
|
||||
const dx = c.finger[0] - c.anchor[0], dy = c.finger[1] - c.anchor[1];
|
||||
const gain = SWIPE.soft + (SWIPE.hard - SWIPE.soft) * (c.sens ?? SWIPE.sens);
|
||||
// Si ricalcola anche col dito fermo: il motorino intanto cambia direzione.
|
||||
if (Math.hypot(dx, dy) >= SWIPE.dead) c.drag = clamp(wrapAngle(Math.atan2(dy, dx) - player(c).a) * SWIPE.gain, -1, 1);
|
||||
if (Math.hypot(dx, dy) >= SWIPE.dead) want = clamp(wrapAngle(Math.atan2(dy, dx) - player(c).a) * gain, -1, 1);
|
||||
}
|
||||
// Lo sterzo del dito si avvicina a quello voluto un po' per volta (a ogni passo fisso), e si azzera subito alzandolo.
|
||||
c.drag = c.pointer === null ? 0 : c.drag + (want - c.drag) * Math.min(1, SWIPE.follow * FIXED_STEP);
|
||||
if (Math.abs(want - c.drag) < 1e-6) c.drag = want;
|
||||
return clamp((c.keys.right ? 1 : 0) - (c.keys.left ? 1 : 0) + c.buttons + c.drag, -1, 1);
|
||||
}
|
||||
// La distanza davanti, sul giro, da s a un punto della pista (sempre fra 0 e la lunghezza del giro).
|
||||
|
||||
@@ -5,6 +5,7 @@ import { ambientEffects, ambientHash } from './ambient.js';
|
||||
import { drawTorch } from './torch.js';
|
||||
import { drawBocce } from './bocce-render.js';
|
||||
import { drawBiliardo } from './biliardo-render.js';
|
||||
import { drawRinquarto } from './rinquarto-render.js';
|
||||
import { drawBasket } from './basket-render.js';
|
||||
import { drawPesca } from './pesca-render.js';
|
||||
import { drawGara } from './gara-render.js';
|
||||
@@ -701,6 +702,11 @@ function drawCloseup(ctx, game, time) {
|
||||
drawBasket(ctx, c, def, backgrounds.get(def.picture), sprites.get('andrea_basket'));
|
||||
return;
|
||||
}
|
||||
if (def.type === 'rinquarto') {
|
||||
if (!backgrounds.has(def.picture)) loadBackground(def.picture);
|
||||
drawRinquarto(ctx, c, def, backgrounds.get(def.picture));
|
||||
return;
|
||||
}
|
||||
if (def.type === 'biliardo') {
|
||||
if (!backgrounds.has(def.picture)) loadBackground(def.picture);
|
||||
drawBiliardo(ctx, c, def, backgrounds.get(def.picture), sprites.get('ing_tozzi'));
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { LEN, WID, R, VIEW, WINDUP } from './biliardo.js';
|
||||
import { table, sphere, pin, cue } from './biliardo-render.js';
|
||||
import { guide, canThrow } from './rinquarto.js';
|
||||
import { text, panel } from './bocce-render.js';
|
||||
|
||||
const sx = x => VIEW.x + x * VIEW.k, sy = y => VIEW.y + y * VIEW.k;
|
||||
export function drawRinquarto(ctx, c, def, bg) {
|
||||
ctx.save(); ctx.imageSmoothingEnabled = false;
|
||||
table(ctx, bg);
|
||||
ctx.save(); ctx.beginPath(); ctx.rect(VIEW.x - 15, VIEW.y - 15, LEN * VIEW.k + 30, WID * VIEW.k + 30); ctx.clip();
|
||||
if (canThrow(c)) {
|
||||
const points = guide(c);
|
||||
if (points.length) {
|
||||
ctx.strokeStyle = '#fff8dc'; ctx.lineWidth = 1; ctx.setLineDash([4, 3]);
|
||||
ctx.beginPath(); ctx.moveTo(sx(points[0][0]), sy(points[0][1]));
|
||||
for (const [x, y] of points.slice(1)) ctx.lineTo(sx(x), sy(y));
|
||||
ctx.stroke(); ctx.setLineDash([]);
|
||||
}
|
||||
const b = c.balls.player;
|
||||
cue(ctx, b.x, b.y, c.angle, R * VIEW.k + 6 + c.power * 26);
|
||||
} else if (c.visual.shot && c.windup > 0) {
|
||||
const s = c.visual.shot;
|
||||
cue(ctx, s.x, s.y, s.angle, R * VIEW.k + (6 + s.power * 26) * c.windup / WINDUP);
|
||||
}
|
||||
for (const p of c.pins) pin(ctx, p, c.visual.time);
|
||||
for (const b of Object.values(c.balls)) {
|
||||
sphere(ctx, b.role, sx(b.x), sy(b.y), R * VIEW.k);
|
||||
// La croce distingue i disturbi anche senza riconoscere il colore viola.
|
||||
if (!['player', 'cpu'].includes(b.role)) {
|
||||
ctx.strokeStyle = '#2b163e'; ctx.lineWidth = 1;
|
||||
ctx.beginPath(); ctx.moveTo(sx(b.x) - 2, sy(b.y) - 2); ctx.lineTo(sx(b.x) + 2, sy(b.y) + 2);
|
||||
ctx.moveTo(sx(b.x) + 2, sy(b.y) - 2); ctx.lineTo(sx(b.x) - 2, sy(b.y) + 2); ctx.stroke();
|
||||
}
|
||||
}
|
||||
ctx.restore();
|
||||
ctx.fillStyle = '#17160ff0'; ctx.fillRect(54, 4, 532, 64);
|
||||
text(ctx, 'I RINQUARTI DI BERTOVO', 320, 13, 9, '#efd49e', 'center');
|
||||
text(ctx, `Posizione ${c.position}/${c.skill.positions} Punti ${c.points}/${c.skill.glory} Sponde ${c.attempt.cushions}/${c.skill.cushions}`, 320, 28, 8, '#fff0c8', 'center');
|
||||
text(ctx, canThrow(c) ? `Bianca: ${def.names.player} · Gialla: bersaglio · Viola e birilli: disturbi` : c.line, 320, 43, 7, '#d9ccba', 'center');
|
||||
text(ctx, canThrow(c) ? `«Oh tirami questo rinquarto!?» Forza ${Math.round(c.power * 100)}%` : c.step === 'rolling' ? 'Biglie in movimento…' : 'Un tiro per posizione.', 320, 59, 8, '#9fd0ff', 'center');
|
||||
if (c.step === 'settle' || c.step === 'result') {
|
||||
const final = c.step === 'result';
|
||||
panel(ctx, 155, 145, 330, final ? 96 : 58);
|
||||
text(ctx, c.attempt.point ? 'Punto!' : 'Niente', 320, 164, 12, c.attempt.point ? '#b9e5b3' : '#ffbf9f', 'center');
|
||||
text(ctx, final ? `${c.result.winner === 'player' ? 'GLORIA!' : 'SFIDA PERSA'} ${c.points} punti su ${c.skill.positions}` : c.attempt.point ? 'Rinquarto riuscito.' : c.attempt.reason, 320, 188, 8, '#efd49e', 'center');
|
||||
if (final) text(ctx, `Obiettivo: ${c.skill.glory}. Torna da Bertovo per uscire.`, 320, 221, 7, '#d9ccba', 'center');
|
||||
}
|
||||
ctx.restore();
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
// I rinquarti di Bertovo: posizioni coperte, un solo tentativo per posizione.
|
||||
import * as billiard from './biliardo.js';
|
||||
const { LEN, WID, R, PIN, simulate, predict } = billiard;
|
||||
export { aimAt, aimScreen, nudge, setPower, press, release, cancel, shoot, canThrow } from './biliardo.js';
|
||||
|
||||
export const LEVELS = {
|
||||
bona: { positions: 6, cushions: 1, glory: 3, guideBounces: 2, guideLength: Infinity },
|
||||
seria: { positions: 6, cushions: 2, glory: 3, guideBounces: 1, guideLength: 65 },
|
||||
male: { positions: 6, cushions: 3, glory: 2, guideBounces: 0, guideLength: 0 },
|
||||
};
|
||||
export const canAdjust = c => billiard.canThrow(c) && !c.holding;
|
||||
export const aimFree = c => !billiard.canThrow(c);
|
||||
export const button = c => c.step === 'result' ? 'Finita' : c.step === 'aim' ? 'Tira' : 'In movimento…';
|
||||
const ball = (role, x, y) => ({ role, x, y, vx: 0, vy: 0 });
|
||||
function random(c) { c.seed = (Math.imul(c.seed, 1664525) + 1013904223) >>> 0; return c.seed / 4294967296; }
|
||||
const attempt = () => ({ cushions: 0, decided: false, point: false, reason: null });
|
||||
|
||||
// Conta solo le sponde della propria biglia prima del primo ostacolo.
|
||||
function contact(result, required, { kind, a, b }) {
|
||||
if (result.decided || (a.role !== 'player' && b?.role !== 'player')) return;
|
||||
if (kind === 'rail') { result.cushions++; return; }
|
||||
result.decided = true;
|
||||
const other = a.role === 'player' ? b : a;
|
||||
result.point = kind === 'ball' && other.role === 'cpu' && result.cushions >= required;
|
||||
result.reason = result.point ? null : kind === 'ball' && other.role === 'cpu' ? 'poche sponde' : 'disturbo toccato';
|
||||
}
|
||||
|
||||
export function tryShot(c, angle, power) {
|
||||
const result = attempt();
|
||||
simulate(c, 'player', angle, power, {
|
||||
onContact: event => contact(result, c.skill.cushions, event),
|
||||
stop: () => result.decided,
|
||||
});
|
||||
if (!result.decided) result.reason = 'bersaglio mancato';
|
||||
return result;
|
||||
}
|
||||
|
||||
// Distanza dal segmento dei centri, con i raggi sommati (biglia in movimento inclusa).
|
||||
export function directCovered(c) {
|
||||
const a = c.balls.player, b = c.balls.cpu, dx = b.x - a.x, dy = b.y - a.y, d2 = dx * dx + dy * dy;
|
||||
if (!d2) return false;
|
||||
const obstacles = [...Object.values(c.balls).filter(b => !['player', 'cpu'].includes(b.role)).map(b => ({ ...b, radius: R })),
|
||||
...c.pins.filter(p => !p.down).map(p => ({ ...p, radius: PIN }))];
|
||||
return obstacles.some(p => {
|
||||
const t = ((p.x - a.x) * dx + (p.y - a.y) * dy) / d2;
|
||||
return t > 0 && t < 1 && Math.hypot(p.x - a.x - t * dx, p.y - a.y - t * dy) < R + p.radius;
|
||||
});
|
||||
}
|
||||
|
||||
export function solve(c) {
|
||||
// Angoli intercalati: si esplora subito tutto il tavolo, poi si affina a mezzo grado.
|
||||
for (let offset = 0; offset < 8; offset++) for (let i = offset; i < 720; i += 8) {
|
||||
const angle = i * Math.PI / 360;
|
||||
for (const power of [1, 0.82, 0.65]) {
|
||||
if (tryShot(c, angle, power).point) return { angle, power };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function position(c) {
|
||||
for (;;) {
|
||||
const place = role => ball(role, 20 + random(c) * (LEN - 40), 18 + random(c) * (WID - 36));
|
||||
const player = place('player'), cpu = place('cpu');
|
||||
if (Math.hypot(cpu.x - player.x, cpu.y - player.y) < 70) continue;
|
||||
const t = 0.3 + random(c) * 0.4;
|
||||
const blocker = ball('blocker', player.x + (cpu.x - player.x) * t, player.y + (cpu.y - player.y) * t);
|
||||
c.balls = { player, cpu, blocker, extra: place('extra') };
|
||||
c.pins = Array.from({ length: 2 }, () => ({ x: 20 + random(c) * (LEN - 40), y: 18 + random(c) * (WID - 36), red: false, down: false }));
|
||||
const objects = [...Object.values(c.balls).map(b => ({ ...b, radius: R })), ...c.pins.map(p => ({ ...p, radius: PIN }))];
|
||||
if (objects.some((a, i) => objects.slice(i + 1).some(b => Math.hypot(a.x - b.x, a.y - b.y) < a.radius + b.radius + 2))) continue;
|
||||
if (!directCovered(c)) continue;
|
||||
const solution = solve(c);
|
||||
if (!solution) continue;
|
||||
c.solution = solution;
|
||||
break;
|
||||
}
|
||||
c.position++;
|
||||
c.step = 'aim'; c.t = c.accumulator = 0; c.power = 0.75; c.holding = false; c.aimLocked = false;
|
||||
c.shot = c.pending = c.visual.shot = null; c.windup = 0;
|
||||
c.angle = Math.atan2(c.balls.cpu.y - c.balls.player.y, c.balls.cpu.x - c.balls.player.x);
|
||||
c.attempt = attempt();
|
||||
c.line = 'Bertovo: «Oh tirami questo rinquarto!?»';
|
||||
}
|
||||
|
||||
export function newRinquarto(seed = Date.now() >>> 0, def = {}) {
|
||||
const level = LEVELS[def.level] ? def.level : 'seria';
|
||||
const c = { ...billiard.newBiliardo(seed), level, skill: LEVELS[level], position: 0, points: 0, history: [] };
|
||||
position(c);
|
||||
return c;
|
||||
}
|
||||
|
||||
export function update(c, dt) {
|
||||
if (!Number.isFinite(dt) || dt <= 0) return;
|
||||
dt = Math.min(dt, 0.25);
|
||||
if (c.step === 'result') {
|
||||
// L’ultimo birillo può essere ancora in caduta: il riepilogo ferma la partita, non l’animazione.
|
||||
c.visual.time += dt;
|
||||
return;
|
||||
}
|
||||
if (c.step === 'settle') {
|
||||
c.t += dt;
|
||||
// I birilli finiscono di cadere anche mentre si legge l'esito del tiro.
|
||||
c.visual.time += dt;
|
||||
if (c.t >= 1.8) position(c);
|
||||
return;
|
||||
}
|
||||
billiard.update(c, dt, event => contact(c.attempt, c.skill.cushions, event));
|
||||
if (c.step !== 'settle') return;
|
||||
if (!c.attempt.decided) c.attempt.reason = 'bersaglio mancato';
|
||||
c.points += Number(c.attempt.point);
|
||||
c.history.push({ ...c.attempt });
|
||||
c.line = c.attempt.point ? 'Punto!' : `Niente: ${c.attempt.reason}.`;
|
||||
if (c.position === c.skill.positions) {
|
||||
c.step = 'result';
|
||||
c.result = { winner: c.points >= c.skill.glory ? 'player' : 'cpu', points: c.points };
|
||||
}
|
||||
}
|
||||
|
||||
// La guida si ferma al contatto, al limite di lunghezza o alla sponda consentita.
|
||||
export function guide(c) {
|
||||
if (!c.skill.guideLength) return [];
|
||||
const source = predict(c).points, points = [source[0]];
|
||||
let remaining = c.skill.guideLength;
|
||||
for (const end of source.slice(1, c.skill.guideBounces + 1)) {
|
||||
const start = points.at(-1), dx = end[0] - start[0], dy = end[1] - start[1], distance = Math.hypot(dx, dy);
|
||||
if (distance > remaining) { points.push([start[0] + dx * remaining / distance, start[1] + dy * remaining / distance]); break; }
|
||||
points.push(end); remaining -= distance;
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
export function status(c) {
|
||||
const prefix = `Posizione ${c.position} di ${c.skill.positions}. Punti: ${c.points}.`;
|
||||
if (c.step === 'result') return `${prefix} ${c.line} ${c.result.winner === 'player' ? 'Gloria!' : 'Sfida persa.'} Obiettivo: ${c.skill.glory}.`;
|
||||
if (c.step === 'rolling') return `${prefix} Sponde: ${c.attempt.cushions}/${c.skill.cushions}.`;
|
||||
return `${prefix} ${c.line} ${c.skill.cushions === 1 ? 'Serve almeno 1 sponda.' : `Servono almeno ${c.skill.cushions} sponde.`}`;
|
||||
}
|
||||
Reference in New Issue
Block a user