import { _decorator, BitmapFont, Button, Component, director, Game, game, Label, Node, ResolutionPolicy, Sprite, SpriteFrame, UITransform, view } from 'cc'; import { ART_KEYS } from './OrchardAssets'; import { GameSession } from './GameSession'; import { OrchardRuntime } from './OrchardRuntime'; import { OrchardDialogs, type DialogKind } from './OrchardDialogs'; import { ReelPresentation } from './ReelPresentation'; import { BETS, type SpinResult } from './SlotsMath'; import { AutoSpinQueue } from './AutoSpinQueue'; const { ccclass, property } = _decorator; /** 只编排输入和表现;概率、资金与存档由独立领域服务管理。 / Coordinates input and presentation; domain services own probability, money and persistence. */ @ccclass('OrchardView') export class OrchardView extends Component { @property({ type: [SpriteFrame] }) artFrames: SpriteFrame[] = []; @property({ type: BitmapFont }) numerals: BitmapFont | null = null; @property({ type: BitmapFont }) celebrationNumerals: BitmapFont | null = null; ready = false; private session: GameSession | null = null; private reels: ReelPresentation | null = null; private dialogs: OrchardDialogs | null = null; private readonly automatic = new AutoSpinQueue(); private get autoActive(): boolean { return this.automatic.active; } private get autoRemaining(): number { return this.automatic.remaining; } private wait = -1; private hidden = false; private fatal = false; private frameKey = ''; private readonly listeners: { node: Node; action: () => void }[] = []; private readonly cache = new Map(); onLoad(): void { try { if (this.artFrames.length !== ART_KEYS.length || !this.numerals) throw new Error('Game artwork is incomplete.'); this.session = OrchardRuntime.getSession(); this.reels = new ReelPresentation(this.node, key => this.art(key)); this.dialogs = new OrchardDialogs(this.node, this.session, key => this.art(key), count => this.startAuto(count), () => this.resumeFree(), () => this.render()); this.bind('LaunchAccess', () => this.autoActive ? this.stopAuto() : this.launch()); this.bind('WagerDecrease', () => this.changeBet(-1)); this.bind('WagerIncrease', () => this.changeBet(1)); this.bind('RepeatAccess', () => this.open('repeat')); this.bind('PreferencesAccess', () => this.open('preferences')); this.bind('GuideAccess', () => this.open('guide')); this.bind('GiftAccess', () => this.open('gift')); game.on(Game.EVENT_HIDE, this.onHide, this); game.on(Game.EVENT_SHOW, this.onShow, this); this.reels.show(this.session.state.board); this.fit(); this.render(); this.ready = true; const state = this.session.state; if (state.pending) this.present(state.pending.result); else if (state.phase === 'free-trigger') this.dialogs.show('award'); else if (state.phase === 'free-result') this.dialogs.show('summary'); else if (state.phase === 'free-ready') this.wait = .7; } catch { this.fatal = true; const notice = this.node.getChildByName('GameNotice'); if (notice) { notice.active = true; notice.getComponent(Label)!.string = 'Sign in to continue. Please reopen the login screen.'; notice.setSiblingIndex(this.node.children.length - 1); } } } private art(key: string): SpriteFrame { const frame = this.artFrames[ART_KEYS.indexOf(key)]; if (!frame) throw new Error('Game artwork is unavailable.'); return frame; } private item(name: string): Node { if (this.cache.has(name)) return this.cache.get(name)!; const find = (node: Node): Node | null => { if (node.name === name) return node; for (const child of node.children) { const value = find(child); if (value) return value; } return null; }; const node = find(this.node); if (!node) throw new Error(`Game control is missing: ${name}`); this.cache.set(name, node); return node; } private bind(name: string, action: () => void): void { const node = this.item(name); node.on(Button.EventType.CLICK, action, this); this.listeners.push({ node, action }); } private text(name: string, amount: number): void { this.item(name).getComponent(Label)!.string = amount.toLocaleString('en-US'); } private setInteractable(name: string, value: boolean): void { this.item(name).getComponent(Button)!.interactable = value; } private changeBet(step: number): void { if (!this.session || this.autoActive || this.dialogs?.opened || this.session.state.phase !== 'idle') return; const index = (BETS as readonly number[]).indexOf(this.session.state.bet); const bet = BETS[index + step]; if (bet !== undefined) this.act(() => this.session!.setBet(bet)); } private launch(): void { if (!this.session || this.fatal || this.hidden || this.dialogs?.opened) return; const state = this.session.state; if (state.phase !== 'idle' && state.phase !== 'free-ready') return; if (state.phase === 'idle' && state.balance < state.bet) { this.stopAuto(); this.dialogs?.show('empty'); return; } this.wait = -1; this.act(() => { const result = this.session!.beginSpin(); if (this.autoActive) this.automatic.consume(); if (result.awarded > 0) this.stopAuto(); this.present(result); }); } private present(result: SpinResult): void { this.reels!.play(result, () => { if (!this.isValid || !this.session) return; this.act(() => { this.session!.completeSpin(); const state = this.session!.state; if (state.phase === 'free-trigger') { this.stopAuto(); this.dialogs!.show('award'); } else if (state.phase === 'free-result') this.dialogs!.show('summary'); else if (state.phase === 'free-ready') this.wait = .9; else if (this.autoActive && this.autoRemaining > 0) this.wait = .65; else this.stopAuto(); if (result.win > 0 && state.settings.vibration && typeof navigator !== 'undefined' && navigator.vibrate) navigator.vibrate(45); }); }); this.render(); } private startAuto(count: number): void { if (!this.session || this.session.state.phase !== 'idle' || ![10, 25, 50, 100].includes(count)) return; this.automatic.start(count); this.launch(); } private stopAuto(): void { this.automatic.stop(); if (this.session?.state.phase === 'idle') this.wait = -1; if (this.ready) this.render(); } private resumeFree(): void { this.render(); if (this.session?.state.phase === 'free-ready') this.wait = .5; } private open(kind: DialogKind): void { if (!this.session || this.fatal || this.reels?.spinning || this.session.state.phase !== 'idle') return; this.stopAuto(); this.dialogs?.show(kind); } private act(action: () => void): void { try { action(); this.render(); } catch { this.fatal = true; this.automatic.stop(); this.wait = -1; this.dialogs?.error('Unable to save game progress. Tap OK to return to login and safely restore your saved round.', true); } } private render(): void { if (!this.session) return; const state = this.session.state; const bonus = !!state.free && (state.pending?.free === true || state.phase !== 'spinning'); const automated = this.autoActive && !bonus; this.item('NormalPrize').active = !bonus; this.item('RoundsRibbon').active = bonus || automated; this.item('BonusPrize').active = bonus; this.item('NormalPrize').setPosition(0, 0, 0); const prizeY = automated ? -442 : -327; this.item('NormalPrizePlate').setPosition(0, prizeY, 0); this.item('NormalPrizePlate').getComponent(UITransform)!.setContentSize(automated ? 610 : 712, automated ? 120 : 153); this.item('NormalPrizeTitle').setPosition(-160, prizeY, 0); this.item('NormalPrizeAmount').setPosition(101, prizeY, 0); this.item('NormalPrizeAmount').getComponent(Label)!.fontSize = automated ? 84 : 108; this.item('WagerControls').setPosition(0, bonus || automated ? -582 : -500, 0); this.item('WagerPlate').getComponent(UITransform)!.setContentSize(773, bonus || automated ? 110 : 139); this.item('WagerDecrease').getComponent(UITransform)!.setContentSize(bonus || automated ? 100 : 126, bonus || automated ? 81 : 102); this.item('WagerIncrease').getComponent(UITransform)!.setContentSize(bonus || automated ? 102 : 128, bonus || automated ? 81 : 102); this.item('RoundsHeading').getComponent(Sprite)!.spriteFrame = this.art(bonus ? 'bonus-heading' : 'repeat-heading'); this.text('RoundsCount', bonus ? state.free!.remaining : this.autoRemaining); this.text('WalletAmount', state.balance); this.text('NormalPrizeAmount', state.lastWin); this.text('RoundPrizeAmount', state.lastWin); this.text('BonusPrizeAmount', state.free?.total ?? 0); this.text('WagerAmount', state.free?.bet ?? state.bet); const idle = state.phase === 'idle' && !this.reels?.spinning && !this.fatal; this.setInteractable('WagerDecrease', idle && !this.autoActive && state.bet > BETS[0]); this.setInteractable('WagerIncrease', idle && !this.autoActive && state.bet < BETS[BETS.length - 1]); this.item('RepeatAccess').active = !bonus && !automated; this.setInteractable('RepeatAccess', idle && !automated); this.setInteractable('LaunchAccess', !this.fatal && (automated || idle) && !bonus); const spinSprite = this.item('LaunchAccess').getComponent(Sprite)!; spinSprite.spriteFrame = this.art(automated ? 'halt-orb' : bonus || !idle ? 'resting-orb' : 'launch-orb'); this.item('LaunchAccess').setPosition(0, bonus || automated ? -795 : -754, 0); this.item('LaunchAccess').getComponent(UITransform)!.setContentSize(bonus || automated ? 312 : 342, bonus ? 300 : automated ? 312 : 341); for (const key of ['PreferencesAccess', 'GuideAccess', 'GiftAccess']) this.setInteractable(key, idle && !automated); } update(dt: number): void { this.fit(); if (!this.ready || this.fatal || this.hidden) return; this.reels?.update(dt); this.dialogs?.tick(); if (this.wait >= 0 && !this.reels?.spinning && !this.dialogs?.opened) { this.wait -= Math.min(.1, Math.max(0, dt)); if (this.wait <= 0) { this.wait = -1; this.launch(); } } } private fit(): void { const frame = view.getFrameSize(); if (frame.width <= 0 || frame.height <= 0) return; const key = `${frame.width}:${frame.height}`; if (key === this.frameKey) return; this.frameKey = key; view.setDesignResolutionSize(1080, 1920, frame.height / frame.width < 1920 / 1080 ? ResolutionPolicy.FIXED_HEIGHT : ResolutionPolicy.FIXED_WIDTH); const size = view.getVisibleSize(); this.node.getComponent(UITransform)?.setContentSize(size); const cover = Math.max(size.width / 1080, size.height / 1920); this.node.getChildByName('OrchardScenery')?.setScale(cover, cover, 1); } private onHide(): void { this.hidden = true; this.stopAuto(); } private onShow(): void { this.hidden = false; } onDisable(): void { this.stopAuto(); } onDestroy(): void { this.dialogs?.dispose(); if (this.session) OrchardRuntime.clear(this.session); game.off(Game.EVENT_HIDE, this.onHide, this); game.off(Game.EVENT_SHOW, this.onShow, this); for (const { node, action } of this.listeners) if (node.isValid) node.off(Button.EventType.CLICK, action, this); } }