// ChessPieces.js — the men on the board. Each is a contact shadow + a baked // carved piece (ChessArt), positioned by square (sq64) and view orientation. // Moves lift, glide and land with a little dust; knights hop; captured men // tip over and slide off to the capturer's side; promotion shimmers; a mated // king topples. import { squareXY } from './ChessLayout.js'; import { RES, pieceKey } from './ChessArt.js'; const SCALE = 1 / RES; const FOOT = 35; // from square centre down to where the base meets the board export class PieceSet { constructor(scene, depth, { style = 'carved' } = {}) { this.scene = scene; this.depth = depth; this.style = style; this.items = new Map(); // sq64 → item this.flipped = false; } tex(p) { return pieceKey(this.style, p.color, p.type); } build(board, flipped) { this.clear(); this.flipped = flipped; board.forEach((p, sq) => { if (p) this.items.set(sq, this.make(sq, p)); }); return this; } make(sq, p) { const s = this.scene; const { x, y } = squareXY(sq, this.flipped); const shadow = s.add.image(x, y + FOOT, 'cs-shadow').setScale(SCALE * 0.9).setAlpha(0.75); const body = s.add.image(x, y, this.tex(p)).setScale(SCALE); const it = { sq, p: { ...p }, shadow, body, x, y, lift: 0 }; this.place(it, x, y, 0); return it; } clear() { for (const it of this.items.values()) { it.shadow.destroy(); it.body.destroy(); } this.items.clear(); } get(sq) { return this.items.get(sq); } all() { return [...this.items.values()]; } place(it, x, y, lift = it.lift) { it.x = x; it.y = y; it.lift = lift; it.body.setPosition(x, y - lift); it.shadow.setPosition(x + lift * 0.18, y + FOOT + lift * 0.1) .setScale(SCALE * 0.9 * (1 + lift / 160), SCALE * 0.9 * (1 + lift / 220)) .setAlpha(0.75 - Math.min(0.45, lift / 110)); const d = this.depth + (lift > 0 ? 30 : 0) + y / 2000; it.body.setDepth(d); it.shadow.setDepth(this.depth - 1); } home(sq) { return squareXY(sq, this.flipped); } setFlipped(f) { this.flipped = f; for (const it of this.items.values()) { const p = this.home(it.sq); this.place(it, p.x, p.y, 0); } } setStyle(style) { this.style = style; for (const it of this.items.values()) it.body.setTexture(this.tex(it.p)); } setVisible(v) { for (const it of this.items.values()) { it.body.setVisible(v); it.shadow.setVisible(v); } } tween(cfg) { return new Promise((res) => this.scene.tweens.add({ ...cfg, onComplete: res })); } async liftTo(it, lift, dur = 110) { if (!it) return; const o = { l: it.lift }; await this.tween({ targets: o, l: lift, duration: dur, ease: 'Quad.Out', onUpdate: () => this.place(it, it.x, it.y, o.l) }); } /** Glide (or hop, for a knight) to a square. */ async moveTo(it, sq, { dur = 260, arc = 14 } = {}) { if (!it) return; const from = { x: it.x, y: it.y, l: it.lift }; const to = this.home(sq); const o = { t: 0 }; const dist = Math.hypot(to.x - from.x, to.y - from.y); const d = dur * (0.75 + Math.min(1, dist / 500) * 0.5); await this.tween({ targets: o, t: 1, duration: d, ease: 'Cubic.InOut', onUpdate: () => { const t = o.t; const lift = from.l * (1 - t) + Math.sin(Math.PI * t) * arc; this.place(it, from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t, lift); }, }); this.place(it, to.x, to.y, 0); } async squash(it, amt = 0.08, dur = 110) { if (!it?.body?.active) return; await this.tween({ targets: it.body, scaleX: SCALE * (1 + amt), scaleY: SCALE * (1 - amt), duration: dur / 2, yoyo: true, ease: 'Quad.Out' }); it.body.setScale(SCALE); } /** A puff of dust where a piece lands. */ dust(x, y, n = 6) { const s = this.scene; for (let i = 0; i < n; i++) { const d = s.add.image(x + (Math.random() - 0.5) * 30, y + FOOT - 2, 'cs-dust').setDepth(this.depth - 0.5).setScale(0.2).setAlpha(0.55); const a = (i / n) * Math.PI * 2; s.tweens.add({ targets: d, x: d.x + Math.cos(a) * 28, y: d.y + Math.sin(a) * 8 - 4, scale: 0.55, alpha: 0, duration: 420 + Math.random() * 200, ease: 'Quad.Out', onComplete: () => d.destroy() }); } } rekey(it, sq) { this.items.delete(it.sq); it.sq = sq; this.items.set(sq, it); } /** Remove a piece without animation. */ remove(sq) { const it = this.items.get(sq); if (!it) return; it.body.destroy(); it.shadow.destroy(); this.items.delete(sq); } /** Captured: tip over, then slide off toward (tx, ty) shrinking. */ async capture(sq, tx, ty, { dur = 520, delay = 0 } = {}) { const it = this.items.get(sq); if (!it) return; this.items.delete(sq); const dir = tx < it.x ? -1 : 1; it.body.setDepth(this.depth + 40); await this.tween({ targets: it.body, angle: dir * 82, x: it.x + dir * 22, y: it.y + 16, duration: 200, delay, ease: 'Quad.In' }); this.dust(it.x + dir * 30, it.y, 4); const from = { x: it.body.x, y: it.body.y }; const o = { t: 0 }; await this.tween({ targets: o, t: 1, duration: dur, ease: 'Quad.InOut', onUpdate: () => { const t = o.t; const x = from.x + (tx - from.x) * t, y = from.y + (ty - from.y) * t - Math.sin(Math.PI * t) * 70; it.body.setPosition(x, y).setScale(SCALE * (1 - 0.6 * t)).setAngle(dir * (82 + t * 100)); it.shadow.setPosition(x, y + FOOT * (1 - t) + Math.sin(Math.PI * t) * 70).setAlpha(0.5 * (1 - t)); }, }); it.body.destroy(); it.shadow.destroy(); } /** Promotion: the pawn shimmers into the new piece. */ async promote(it, type) { if (!it) return; const s = this.scene; const flash = s.add.image(it.x, it.y - 10, 'cs-glow').setDepth(this.depth + 45).setScale(0.3).setAlpha(0).setBlendMode('ADD'); await this.tween({ targets: flash, alpha: 1, scale: 1.4, duration: 220, ease: 'Quad.Out' }); it.p = { ...it.p, type }; it.body.setTexture(this.tex(it.p)); await this.tween({ targets: flash, alpha: 0, scale: 2, duration: 360, ease: 'Quad.In' }); flash.destroy(); await this.squash(it, 0.12, 160); } /** Checkmate: the king rocks, then falls. */ async topple(sq) { const it = this.items.get(sq); if (!it) return; const dir = Math.random() < 0.5 ? -1 : 1; await this.tween({ targets: it.body, angle: dir * 8, duration: 140, yoyo: true, repeat: 1, ease: 'Sine.InOut' }); await this.tween({ targets: it.body, angle: dir * 92, x: it.x + dir * 30, y: it.y + 18, duration: 620, ease: 'Bounce.Out' }); this.dust(it.x + dir * 36, it.y, 8); } /** Bring every piece in, rank by rank, from above the table. */ async dropIn(k = 1, onLand = null) { const list = this.all().sort((a, b) => (a.y - b.y) || (a.x - b.x)); const jobs = list.map((it, i) => new Promise((res) => { const { x, y } = it; this.place(it, x, y, 160); it.body.setAlpha(0); it.shadow.setAlpha(0); this.scene.time.delayedCall(i * 22 * k, () => { it.body.setAlpha(1); const o = { l: 160 }; this.scene.tweens.add({ targets: o, l: 0, duration: 240, ease: 'Quad.In', onUpdate: () => this.place(it, x, y, o.l), onComplete: () => { onLand?.(i); this.squash(it, 0.08, 90).then(res); }, }); }); })); await Promise.all(jobs); } }