增加游戏玩法界面和各个弹窗初版

This commit is contained in:
zpx committed 2026-11-20 18:51:48 +08:00
1 parent e84e14783e
commit eb48d8e845
211 files changed
+59920 -21

No files matched your search

+144 -14
View File
@@ -1,6 +1,8 @@
import { _decorator, Component, Label, Node, Sprite, SpriteFrame, UITransform } from 'cc';
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;
@@ -16,45 +18,160 @@ export class StartupLoadingView extends Component {
@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<Node, boolean>();
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.notified = false;
this.completedFor = 0;
this.progress.begin();
this.show(this.enabledInHierarchy);
this.draw();
} else if (state.phase === 'authenticated') {
this.progress.finish();
this.draw();
} else {
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<void> {
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') this.completedFor += Math.max(0, Math.min(.1, dt));
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;
// 当前无主场景,收尾后返回既有认证成功落点;后续由就绪接口承接。 / Until a game scene exists, retain the existing authenticated landing state.
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);
}
@@ -86,8 +203,21 @@ export class StartupLoadingView extends Component {
}
if (!visible) this.previousVisibility.clear();
}
onDisable(): void { this.show(false); }
onDisable(): void {
this.operation++;
if (!this.handingOff) this.cancelPreparation();
this.preparing = false;
this.nextScene = null;
this.progress.reset();
this.show(false);
}
onDestroy(): void {
if (this.login?.isValid) this.login.node.off(GuestLoginView.STATE_CHANGED, this.onLoginState, this);
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);
}
}