83 lines
3.4 KiB
TypeScript
83 lines
3.4 KiB
TypeScript
import { Color, Graphics, Node, Sprite, SpriteFrame, UITransform } from 'cc';
|
|
import { PAYLINES, SYMBOLS, type SpinResult, type SymbolId } from './SlotsMath';
|
|
import { SYMBOL_ART } from './OrchardAssets';
|
|
|
|
/** 动画只展示已提交的结果,不参与随机或金币结算。 / Animation presents committed outcomes without generating or settling them. */
|
|
export class ReelPresentation {
|
|
private elapsed = 0;
|
|
private result: SpinResult | null = null;
|
|
private completed: (() => void) | null = null;
|
|
private highlight = 0;
|
|
private readonly cells: Sprite[][];
|
|
private readonly lines: Graphics;
|
|
constructor(private readonly root: Node, private readonly art: (key: string) => SpriteFrame) {
|
|
this.cells = Array.from({ length: 5 }, (_, c) => Array.from({ length: 6 }, (_, r) =>
|
|
root.getChildByPath(`ReelWindow${c}/Cell${r}`)!.getComponent(Sprite)!));
|
|
this.lines = root.getChildByName('WinningLines')!.getComponent(Graphics)!;
|
|
}
|
|
get spinning(): boolean { return this.result !== null; }
|
|
show(board: SymbolId[][]): void {
|
|
for (let c = 0; c < 5; c++) for (let r = 0; r < 6; r++) {
|
|
this.symbol(this.cells[c][r], board[c][r % 3]);
|
|
this.cells[c][r].node.setPosition(0, 230 - r * 230, 0);
|
|
}
|
|
}
|
|
play(result: SpinResult, done: () => void): void {
|
|
this.result = result; this.completed = done; this.elapsed = 0; this.highlight = 0;
|
|
this.lines.clear();
|
|
}
|
|
update(dt: number): void {
|
|
if (!Number.isFinite(dt)) return;
|
|
dt = Math.min(.1, Math.max(0, dt));
|
|
if (!this.result) { if (this.highlight > 0) this.highlight -= dt; return; }
|
|
this.elapsed += dt;
|
|
for (let c = 0; c < 5; c++) {
|
|
const stop = .65 + c * .16;
|
|
if (this.elapsed >= stop) {
|
|
for (let r = 0; r < 6; r++) {
|
|
this.symbol(this.cells[c][r], this.result.board[c][r % 3]);
|
|
const bounce = this.elapsed < stop + .18 ? Math.sin((this.elapsed - stop) / .18 * Math.PI) * 9 : 0;
|
|
this.cells[c][r].node.setPosition(0, 230 - r * 230 + bounce, 0);
|
|
}
|
|
} else {
|
|
const distance = this.elapsed * (1400 + c * 90);
|
|
for (let r = 0; r < 6; r++) {
|
|
const y = 460 - ((distance + r * 230) % 1380);
|
|
this.cells[c][r].node.setPosition(0, y, 0);
|
|
const cycle = Math.floor((distance + r * 230) / 1380);
|
|
this.symbol(this.cells[c][r], SYMBOLS[(cycle * 6 + r + c * 3) % SYMBOLS.length]);
|
|
}
|
|
}
|
|
}
|
|
if (this.elapsed >= 1.55) {
|
|
const result = this.result, done = this.completed;
|
|
this.result = null; this.completed = null;
|
|
this.show(result.board); this.mark(result); done?.();
|
|
}
|
|
}
|
|
private symbol(sprite: Sprite, symbol: SymbolId): void {
|
|
const frame = this.art(SYMBOL_ART[symbol]);
|
|
sprite.spriteFrame = frame;
|
|
const size = frame.originalSize;
|
|
const scale = Math.min(198 / size.width, 212 / size.height);
|
|
sprite.getComponent(UITransform)!.setContentSize(size.width * scale, size.height * scale);
|
|
}
|
|
private mark(result: SpinResult): void {
|
|
this.lines.clear();
|
|
this.lines.strokeColor = new Color(255, 229, 113, 235); this.lines.lineWidth = 5;
|
|
for (const win of result.wins) {
|
|
const line = PAYLINES[win.line];
|
|
line.slice(0, win.count).forEach((row, col) => {
|
|
const x = -410 + col * 205, y = 375 - row * 230;
|
|
if (col === 0) this.lines.moveTo(x, y); else this.lines.lineTo(x, y);
|
|
});
|
|
this.lines.stroke();
|
|
}
|
|
if (result.wildColumn !== null) {
|
|
this.lines.strokeColor = new Color(64, 255, 212, 220);
|
|
this.lines.roundRect(-510 + result.wildColumn * 205, -200, 199, 690, 24); this.lines.stroke();
|
|
}
|
|
this.highlight = 1.2;
|
|
}
|
|
}
|