// MexicanTrainTiles.js — one persistent sprite per domino (keyed by tile id), // with its own soft shadow. Tiles never get redrawn: they slide, lift, flip // and clack between the boneyard, the racks, the hub and the trains. // // const ts = new TileSprites(scene, { colored }); // ts.reset(set) — (re)create sprites for a whole set // ts.set(id, pose) — snap: { x, y, rot, s, faceUp, depth, visible } // await ts.move(id, pose, { dur, lift, delay, ease }) — tween (flips mid-way) // ts.pick(x, y, ids) — top-most of `ids` under a point import * as Phaser from 'phaser'; import { tileKey } from './MexicanTrainArt.js'; const SHADOW = { dx: 3, dy: 5 }; export class TileSprites { constructor(scene, { colored = true, depth = 20 } = {}) { this.scene = scene; this.colored = colored; this.depth = depth; this.items = new Map(); } reset(set) { for (const it of this.items.values()) { it.face.destroy(); it.shadow.destroy(); } this.items.clear(); for (let a = 0; a <= set; a++) for (let b = a; b <= set; b++) this.create({ id: this.items.size, a, b }); } create(tile) { const s = this.scene; const shadow = s.add.image(0, 0, 'mt-tshadow').setVisible(false).setDepth(this.depth - 1); const face = s.add.image(0, 0, 'mt-back').setVisible(false).setDepth(this.depth); const it = { tile, face, shadow, x: 0, y: 0, rot: 0, s: 0.4, faceUp: false, lift: 0, sq: 1, tween: null }; this.items.set(tile.id, it); return it; } get(id) { return this.items.get(id); } /** Stop a tile's tween; anyone awaiting it is released (never left hanging). */ halt(it) { if (it.tween) { it.tween.stop(); it.tween = null; } const done = it.done; it.done = null; done?.(); } tileOf(id) { return this.items.get(id)?.tile; } setColored(colored) { this.colored = colored; for (const it of this.items.values()) if (it.faceUp) it.face.setTexture(tileKey(it.tile.a, it.tile.b, colored)); } apply(it) { const sx = it.s, sy = it.s * it.sq; it.face.setPosition(it.x, it.y - it.lift).setRotation(it.rot).setScale(sx, sy); const off = 1 + it.lift * 0.09; it.shadow.setPosition(it.x + SHADOW.dx * off * it.s * 2.2, it.y + SHADOW.dy * off * it.s * 2.2).setRotation(it.rot) .setScale(sx * (1 + it.lift * 0.004), sy * (1 + it.lift * 0.004)).setAlpha(Math.max(0.25, 0.85 - it.lift * 0.012)); } setFace(it, up) { it.faceUp = up; it.face.setTexture(up ? tileKey(it.tile.a, it.tile.b, this.colored) : 'mt-back'); } setDepth(id, d) { const it = this.items.get(id); if (!it) return; it.face.setDepth(d); it.shadow.setDepth(d - 0.5); } /** Snap a tile to a pose. */ set(id, pose) { const it = this.items.get(id); if (!it) return null; this.halt(it); Object.assign(it, { x: pose.x ?? it.x, y: pose.y ?? it.y, rot: pose.rot ?? it.rot, s: pose.s ?? it.s, lift: pose.lift ?? 0, sq: 1 }); if (pose.faceUp != null) this.setFace(it, pose.faceUp); if (pose.depth != null) this.setDepth(id, pose.depth); const vis = pose.visible ?? true; it.face.setVisible(vis); it.shadow.setVisible(vis && !pose.noShadow); it.face.setAlpha(pose.alpha ?? 1); this.apply(it); return it; } /** Tween a tile to a pose. Turns face-up/down half-way when asked. */ move(id, pose, { dur = 380, lift = 0, delay = 0, ease = 'Cubic.InOut', onLand = null } = {}) { const it = this.items.get(id); if (!it) return Promise.resolve(); this.halt(it); it.face.setVisible(true); it.shadow.setVisible(!pose.noShadow); if (pose.alpha != null) it.face.setAlpha(pose.alpha); if (pose.depth != null) this.setDepth(id, pose.depth); const from = { x: it.x, y: it.y, rot: it.rot, s: it.s }; const to = { x: pose.x ?? it.x, y: pose.y ?? it.y, rot: pose.rot ?? it.rot, s: pose.s ?? it.s }; // shortest way round let dr = to.rot - from.rot; while (dr > Math.PI) dr -= Math.PI * 2; while (dr < -Math.PI) dr += Math.PI * 2; const flip = pose.faceUp != null && pose.faceUp !== it.faceUp; let flipped = false; if (dur <= 0) { this.set(id, { ...pose }); onLand?.(); return Promise.resolve(); } return new Promise((resolve) => { it.done = resolve; const p = { t: 0 }; it.tween = this.scene.tweens.add({ targets: p, t: 1, duration: dur, delay, ease, onUpdate: () => { const t = p.t; it.x = Phaser.Math.Linear(from.x, to.x, t); it.y = Phaser.Math.Linear(from.y, to.y, t); it.rot = from.rot + dr * t; it.s = Phaser.Math.Linear(from.s, to.s, t); it.lift = lift * Math.sin(Math.PI * t); if (flip) { it.sq = Math.max(0.04, Math.abs(Math.cos(Math.PI * t))); if (!flipped && t >= 0.5) { flipped = true; this.setFace(it, pose.faceUp); } } this.apply(it); }, onComplete: () => { it.tween = null; if (flip && !flipped) this.setFace(it, pose.faceUp); Object.assign(it, { x: to.x, y: to.y, rot: to.rot, s: to.s, lift: 0, sq: 1 }); this.apply(it); if (pose.visible === false) { it.face.setVisible(false); it.shadow.setVisible(false); } onLand?.(); it.done = null; resolve(); }, }); }); } hide(id) { const it = this.items.get(id); if (!it) return; this.halt(it); it.face.setVisible(false); it.shadow.setVisible(false); } /** Lift (or drop) a resting tile without moving it — hover feedback. */ hover(id, lift, dur = 120) { const it = this.items.get(id); if (!it || it.tween) return; const p = { l: it.lift }; this.scene.tweens.add({ targets: p, l: lift, duration: dur, ease: 'Quad.Out', onUpdate: () => { it.lift = p.l; this.apply(it); } }); } /** Top-most visible tile of `ids` under (x, y). */ pick(x, y, ids) { let best = null, bd = -Infinity; for (const id of ids) { const it = this.items.get(id); if (!it || !it.face.visible) continue; const c = Math.cos(-it.rot), s = Math.sin(-it.rot); const dx = x - it.x, dy = y - (it.y - it.lift); const lx = dx * c - dy * s, ly = dx * s + dy * c; const hw = (it.face.width * it.s) / 2, hh = (it.face.height * it.s * it.sq) / 2; if (Math.abs(lx) <= hw && Math.abs(ly) <= hh && it.face.depth > bd) { bd = it.face.depth; best = id; } } return best; } }