// OthelloPieces.js: the discs on the felt. Each disc is a shadow, a // container holding the face (and an edge band for the moment it stands on // its side), and a highlight that never rotates. // // A flip squashes the container along the capture ray (the container is // rotated to that ray and the face counter-rotated, so the art stays // upright), shows the edge band at the half-way point, swaps the face, and // lands with a small bounce. Placement throws a disc in from the tray. import { squareXY, geom } from './OthelloLayout.js'; import { RES, EDGE_TEX } from './OthelloArt.js'; const EDGE_PX = 8; // the disc's thickness on screen at the 8×8 size export class DiscSet { constructor(scene, depth) { this.scene = scene; this.depth = depth; this.items = new Map(); // sq → item this.size = 8; this.live = new Set(); // running tweens, stopped when the board is cleared } get base() { return geom(this.size).scale / RES; } build(board, size) { this.clear(); this.size = size; board.forEach((v, sq) => { if (v) this.items.set(sq, this.make(sq, v)); }); return this; } make(sq, color, at = null) { const s = this.scene; const { x, y } = at ?? squareXY(sq, this.size); const shadow = s.add.image(x, y, 'ot-shadow').setDepth(this.depth - 1); const body = s.add.image(0, 0, `ot-disc-${color}`); const edge = s.add.image(0, 0, 'ot-edge').setVisible(false); const cont = s.add.container(x, y, [edge, body]); const sheen = s.add.image(x, y, 'ot-sheen'); const it = { sq, color, shadow, body, edge, cont, sheen, x, y, lift: 0, axis: 0, phi: 0, grow: 1 }; this.render(it); return it; } destroyItem(it) { it.dead = true; it.shadow.destroy(); it.cont.destroy(); it.sheen.destroy(); } clear() { for (const t of this.live) t.stop(); this.live.clear(); for (const it of this.items.values()) this.destroyItem(it); this.items.clear(); } get(sq) { return this.items.get(sq); } all() { return [...this.items.values()]; } /** Push an item's state (position, lift, flip angle) to its game objects. */ render(it) { if (it.dead) return; const b = this.base * it.grow; const s = Math.abs(Math.cos(it.phi)); const lift = it.lift * geom(this.size).scale; it.cont.setPosition(it.x, it.y - lift).setRotation(it.axis).setScale(b, b * Math.max(0.02, s)); it.body.setRotation(-it.axis); const side = Math.sin(it.phi); if (side > 0.05) { const want = side * EDGE_PX * geom(this.size).scale; it.edge.setVisible(true).setScale(1, want / (EDGE_TEX.h * b * Math.max(0.02, s))); } else it.edge.setVisible(false); it.sheen.setPosition(it.x, it.y - lift).setScale(b).setAlpha(s * s * 0.9); const sh = 1 + lift / 140; it.shadow.setPosition(it.x + 3 + lift * 0.3, it.y + 6 + lift * 0.45).setScale(b * sh, b * sh * (0.4 + 0.6 * s)) .setAlpha(0.75 - Math.min(0.45, lift / 110)); const d = this.depth + (lift > 1 ? 20 : 0) + it.y / 2000; it.cont.setDepth(d); it.sheen.setDepth(d + 0.0001); } tween(cfg) { return new Promise((res) => { const t = this.scene.tweens.add({ ...cfg, onComplete: () => { this.live.delete(t); res(); } }); this.live.add(t); }); } async bounce(it, amt = 0.1, dur = 120) { const o = { t: 0 }; await this.tween({ targets: o, t: 1, duration: dur, ease: 'Sine.Out', onUpdate: () => { it.grow = 1 + Math.sin(o.t * Math.PI) * amt; this.render(it); }, }); it.grow = 1; this.render(it); } /** * Flip the disc on sq to `color`, squashing along the ray (dr, dc). * onSwap fires at the moment it stands on edge (for the sound). */ async flip(sq, color, { dr = 0, dc = 1, delay = 0, dur = 300, lift = 22, onSwap = null } = {}) { const it = this.items.get(sq); if (!it || it.dead) return; it.axis = Math.atan2(dr, dc) - Math.PI / 2; const o = { p: 0 }; let swapped = false; await this.tween({ targets: o, p: 1, delay, duration: dur, ease: 'Sine.InOut', onUpdate: () => { it.phi = o.p * Math.PI; it.lift = Math.sin(o.p * Math.PI) * lift; if (it.dead) return; if (!swapped && o.p >= 0.5) { swapped = true; it.color = color; it.body.setTexture(`ot-disc-${color}`); onSwap?.(); } this.render(it); }, }); if (it.dead) return; if (!swapped) { it.color = color; it.body.setTexture(`ot-disc-${color}`); } it.phi = 0; it.lift = 0; it.axis = 0; this.render(it); await this.bounce(it, 0.06, 110); } /** Set a disc's colour with no animation. */ setColor(sq, color) { const it = this.items.get(sq); if (!it || it.dead) return; it.color = color; it.body.setTexture(`ot-disc-${color}`); } /** Throw a disc from `from` (the tray) to sq, arcing over the table. */ async place(sq, color, from, { dur = 380, arc = 90 } = {}) { const to = squareXY(sq, this.size); const it = this.make(sq, color, from); this.items.set(sq, it); it.grow = 0.7; const o = { t: 0 }; await this.tween({ targets: o, t: 1, duration: dur, ease: 'Sine.InOut', onUpdate: () => { const t = o.t; it.x = from.x + (to.x - from.x) * t; it.y = from.y + (to.y - from.y) * t; it.lift = Math.sin(Math.PI * t) * arc + (1 - t) * 10; it.grow = 0.7 + 0.3 * t; this.render(it); }, }); it.x = to.x; it.y = to.y; it.lift = 0; it.grow = 1; this.render(it); await this.bounce(it, 0.12, 130); return it; } /** Drop a disc straight down onto sq (the starting four). */ async drop(sq, color, { delay = 0, height = 160, dur = 260 } = {}) { const it = this.make(sq, color); this.items.set(sq, it); it.lift = height; it.cont.setAlpha(0); it.sheen.setAlpha(0); it.shadow.setAlpha(0); this.render(it); await new Promise((res) => this.scene.time.delayedCall(delay, res)); if (it.dead) return; it.cont.setAlpha(1); const o = { l: height }; await this.tween({ targets: o, l: 0, duration: dur, ease: 'Quad.In', onUpdate: () => { it.lift = o.l; this.render(it); } }); it.lift = 0; this.render(it); await this.bounce(it, 0.1, 110); } /** Slide discs to new squares (the count-out). moves: [[fromSq, toSq]…]. */ async arrange(moves, { stagger = 18, dur = 360 } = {}) { const jobs = []; const next = new Map(); moves.forEach(([from, to], i) => { const it = this.items.get(from); if (!it) return; next.set(to, it); const a = { x: it.x, y: it.y }, b = squareXY(to, this.size); it.sq = to; const o = { t: 0 }; jobs.push(this.tween({ targets: o, t: 1, delay: i * stagger, duration: dur, ease: 'Cubic.InOut', onUpdate: () => { it.x = a.x + (b.x - a.x) * o.t; it.y = a.y + (b.y - a.y) * o.t; it.lift = Math.sin(Math.PI * o.t) * 16; this.render(it); }, })); }); // discs not mentioned stay where they are for (const [sq, it] of this.items) if (!moves.some(([f]) => f === sq)) next.set(sq, it); this.items = next; await Promise.all(jobs); } setVisible(v) { for (const it of this.items.values()) { it.cont.setVisible(v); it.sheen.setVisible(v); it.shadow.setVisible(v); } } }