import { _decorator, Button, Component, director, Label, Node, SceneAsset, Sprite, SpriteFrame, UITransform } from 'cc'; import { GuestLoginView } from '../../login/scripts/GuestLoginView'; import type { LoginState } from '../../login/scripts/GuestLoginPresenter'; import { OrchardRuntime } from '../../orchard/scripts/OrchardRuntime'; import type { GameSession } from '../../orchard/scripts/GameSession'; import { StartupProgress } from './StartupProgress'; const { ccclass, property } = _decorator; /** 加载展示与认证解耦;仅显隐控件,不禁用拥有请求生命周期的节点。 / Display controls without disabling the node that owns authentication. */ @ccclass('StartupLoadingView') export class StartupLoadingView extends Component { static readonly READY = 'startup-preparation-ready'; @property({ type: GuestLoginView }) login: GuestLoginView | null = null; @property({ type: Node }) panel: Node | null = null; @property({ type: [Node] }) loginControls: Node[] = []; @property({ type: Sprite }) meter: Sprite | null = null; @property({ type: Label }) percentage: Label | null = null; @property({ type: Label }) caption: Label | null = null; @property({ type: SpriteFrame }) frameArtwork: SpriteFrame | null = null; @property({ type: SpriteFrame }) fillArtwork: SpriteFrame | null = null; @property transitionToGame = true; private readonly progress = new StartupProgress(); private notified = false; private completedFor = 0; private visible = false; private operation = 0; private preparing = false; private retryPreparation = false; private nextScene: SceneAsset | null = null; private preparation: AbortController | null = null; private preparedSession: GameSession | null = null; private handingOff = false; private readonly previousVisibility = new Map(); onLoad(): void { if (!this.login || !this.panel || !this.meter || !this.percentage || !this.caption) { throw new Error('Startup loading controls are not fully bound.'); } this.login.node.on(GuestLoginView.STATE_CHANGED, this.onLoginState, this); this.login.node.on(GuestLoginView.AUTHENTICATED, this.onAuthenticated, this); this.login.guestButton?.node.on(Button.EventType.CLICK, this.onRetry, this); this.show(false); } onEnable(): void { this.show(this.progress.phase !== 'idle' && !this.notified); if (this.visible) this.draw(); if (this.login?.signedIn && !this.notified && !this.retryPreparation) this.onAuthenticated(); } private onLoginState(state: LoginState): void { if (state.phase === 'signing-in') { this.operation++; this.cancelPreparation(); this.nextScene = null; this.preparing = false; this.retryPreparation = false; this.begin(); } else if (state.phase !== 'authenticated') { this.operation++; this.cancelPreparation(); this.nextScene = null; this.preparing = false; this.retryPreparation = false; this.progress.reset(); this.show(false); } } private begin(): void { this.notified = false; this.completedFor = 0; this.progress.begin(); this.show(this.enabledInHierarchy); this.draw(); } private onAuthenticated(): void { // The presenter renders authenticated BEFORE the view receives its session. if (!this.enabledInHierarchy || !this.login?.signedIn || this.notified) return; if (this.transitionToGame) { if (!this.preparing && !this.nextScene) void this.prepareGame(); } else { if (this.progress.phase === 'idle') this.begin(); this.progress.finish(); this.draw(); } } private onRetry(): void { if (this.retryPreparation) this.onAuthenticated(); } private async prepareGame(): Promise { const operation = ++this.operation; this.cancelPreparation(); const preparation = new AbortController(); this.preparation = preparation; this.preparing = true; this.retryPreparation = false; this.nextScene = null; if (this.progress.phase === 'idle') this.begin(); if (this.login?.guestButton) this.login.guestButton.interactable = false; let failure = 'Unable to prepare saved game data. Please tap the guest button to retry. Your saved game has not been reset.'; try { const session = await OrchardRuntime.prepareExclusive(this.login?.guestId ?? '', preparation.signal); if (!this.isCurrent(operation)) { OrchardRuntime.clear(session); return; } this.preparedSession = session; failure = 'Unable to load the game. Please tap the guest button to retry.'; const scene = await OrchardRuntime.loadScene(); if (!this.isCurrent(operation)) return; const view = scene.scene?.getComponentInChildren('OrchardView'); if (!view || !view.enabled) throw new Error('Game view is not bound.'); this.nextScene = scene; this.preparing = false; this.progress.finish(); this.draw(); } catch (error) { if (this.isCurrent(operation)) { if (error instanceof Error && (error.message === OrchardRuntime.LOCK_IN_USE || error.message === OrchardRuntime.LOCK_UNAVAILABLE)) { failure = error.message; } this.preparationFailed(failure); } } } private cancelPreparation(): void { const preparation = this.preparation; const session = this.preparedSession; this.preparation = null; this.preparedSession = null; preparation?.abort(); if (session) OrchardRuntime.clear(session); } private isCurrent(operation: number): boolean { return this.isValid && this.enabledInHierarchy && this.operation === operation; } private preparationFailed(message: string): void { this.operation++; this.preparing = false; this.nextScene = null; this.notified = false; this.retryPreparation = true; this.progress.reset(); this.cancelPreparation(); this.show(false); if (this.login?.statusLabel) { this.login.statusLabel.string = message; this.login.statusLabel.node.active = true; } if (this.login?.guestButton) { this.login.guestButton.node.active = true; this.login.guestButton.interactable = true; } } update(dt: number): void { if (this.progress.phase === 'idle') return; this.progress.tick(dt); this.draw(); if (this.progress.phase === 'complete' && Number.isFinite(dt)) this.completedFor += Math.max(0, Math.min(.1, dt)); if (this.progress.phase === 'complete' && this.completedFor >= .12 && !this.notified) { this.notified = true; if (this.transitionToGame) { const scene = this.nextScene; try { if (!scene) throw new Error('Game preparation is incomplete.'); // All dependencies and the local session are ready. OrchardView initializes // synchronously in onLoad; no frame is rendered between the two scenes. this.handingOff = true; director.runSceneImmediate(scene); this.preparation = null; this.preparedSession = null; } catch { this.handingOff = false; this.cancelPreparation(); if (this.isValid && this.enabledInHierarchy) { this.preparationFailed('Unable to open the game. Please tap the guest button to retry.'); } } return; } this.show(false); this.node.emit(StartupLoadingView.READY); } } private draw(): void { // 认证状态可更新文字,但加载层收尾前不显示到同一位置。 / Keep the latest auth message hidden until the loading overlay finishes. if (this.visible && this.login?.statusLabel) this.login.statusLabel.node.active = false; if (this.meter) { this.meter.node.active = this.progress.value > 0; this.meter.getComponent(UITransform)?.setContentSize(906 * this.progress.value, 102); } if (this.percentage) this.percentage.string = `${Math.floor(this.progress.value * 100)}%`; if (this.caption) this.caption.string = 'Getting things ready'; } private show(visible: boolean): void { if (this.panel) this.panel.active = visible; if (visible === this.visible) return; this.visible = visible; for (const control of this.loginControls) { if (!control.isValid) continue; if (visible) { this.previousVisibility.set(control, control.active); control.active = false; } else { control.active = control === this.login?.statusLabel?.node ? this.login.statusLabel.string.length > 0 : (this.previousVisibility.get(control) ?? control.active); } } if (!visible) this.previousVisibility.clear(); } onDisable(): void { this.operation++; if (!this.handingOff) this.cancelPreparation(); this.preparing = false; this.nextScene = null; this.progress.reset(); this.show(false); } onDestroy(): void { this.operation++; if (!this.handingOff) this.cancelPreparation(); if (this.login?.isValid) { this.login.node.off(GuestLoginView.STATE_CHANGED, this.onLoginState, this); this.login.node.off(GuestLoginView.AUTHENTICATED, this.onAuthenticated, this); } if (this.login?.guestButton?.isValid) this.login.guestButton.node.off(Button.EventType.CLICK, this.onRetry, this); } }