import { _decorator, Component, Node, ProgressBar, Label, UITransform, tween, JsonAsset } from 'cc'; import { callGameApi, getIsRB7, getLanguage, initReqAddr } from './comm'; import { DEBUG } from 'cc/env'; import { initErrorManager } from './manager/ErrorManager'; import { I18nManager } from './manager/I18nManager'; import { LocalizedSprite } from './i18n/LocalizedSprite'; import { AudioManager } from './manager/AudioManager'; import { GameDataManager } from './manager/GameDataManager'; import { ResManager } from './manager/ResManager'; import { NodePoolManager } from './manager/NodePoolManager'; const { ccclass, property } = _decorator; @ccclass('Loading') export class Loading extends Component { // UI组件 @property(Node) progressNode: Node = null; @property(ProgressBar) progressBar: ProgressBar = null; @property(Label) progressLabel: Label = null; @property(Label) loadingLabel: Label = null; @property(Node) maskSpineNode: Node = null; @property(Node) lightSpineNode: Node = null; @property(Node) startBtnNode: Node = null; @property(Node) LoadingUINode: Node = null; @property(Node) rb7Logo: Node = null; @property(JsonAsset) languageJson: JsonAsset = null; // 加载状态 private isNetworkReady = false; private readonly PROGRESS_ANIMATION_DURATION = 0.3; // 进度条阶段 private readonly PROGRESS_INIT = 0.2; // 初始化完成 private readonly PROGRESS_NETWORK = 0.5; // 网络请求完成 private readonly PROGRESS_RESOURCE = 0.9; // 资源加载完成 private readonly PROGRESS_COMPLETE = 1.0; // 完全加载完成 // 游戏节点 private gameContainer: Node = null; private gameNode: Node = null; protected async onLoad() { initReqAddr(); let language = getLanguage(); if (getLanguage() == 'zh') { this.node.getChildByPath("Mask/LoadingUINode/logo_zh").active = true; this.node.getChildByPath("Mask/LoadingUINode/logo_en").active = false; } else { this.node.getChildByPath("Mask/LoadingUINode/logo_zh").active = false; this.node.getChildByPath("Mask/LoadingUINode/logo_en").active = true; } initErrorManager(); await I18nManager.instance.ensureI18nSprite(language, '2'); this.node.getChildByPath('Mask/LoadingUINode/msg').getComponent(LocalizedSprite).updateSpriteForPreload(language); this.node.getChildByPath('Mask/LoadingUINode/msg').active = true; await I18nManager.instance.init(language, this.languageJson); I18nManager.instance.updateSceneRenderers(); this.initializeUI(); // 初始化完成,进度条到20% this.updateProgress(this.PROGRESS_INIT); try { await this.initializeSystem(); AudioManager.instance.init(); this.updateLoadingText('AID_LOADING'); // 延迟一下让用户看到进度条开始 this.scheduleOnce(() => { this.startNetworkLoading(); }, 0.2); } catch (error) { console.error('Network initialization failed:', error); // this.handleError(error); } } /** 初始化系统 */ private async initializeSystem() { this.rb7Logo.active = getIsRB7(); // if (DEBUG) { // await getTestToken('nova006', 'faketrans'); // } } /** 初始化UI */ private initializeUI() { this.startBtnNode.active = false; this.gameContainer = this.node.getChildByPath('Mask/GameNode') // 初始化进度条为0 this.updateProgress(0); } /** 开始网络加载 */ private async startNetworkLoading() { this.updateLoadingText('AID_LOADING'); try { let gameInfo = await callGameApi("gameinfo", {}); GameDataManager.instance.gameInfo = gameInfo; this.isNetworkReady = true; // 网络请求完成,进度条到50% this.updateProgress(this.PROGRESS_NETWORK); // 稍微延迟一下让用户看到进度变化 this.scheduleOnce(() => { this.startResourceLoading(); }, 0.3); } catch (error) { } } /** 开始资源加载 */ private async startResourceLoading() { if (!this.isNetworkReady) return; // this.updateLoadingText('Loading More...'); this.startBtnNode.active = false; this.gameNode = await ResManager.instance.loadPrefabFromBundle( 'Game', 'SlotScene', (finished: number, total: number) => { if (total > 0) { // 资源加载进度从50%到90% const resourceProgress = finished / total; const currentProgress = this.PROGRESS_NETWORK + (resourceProgress * (this.PROGRESS_RESOURCE - this.PROGRESS_NETWORK)); this.updateProgress(currentProgress); } } ); // 资源加载完成,进度条到90% this.updateProgress(this.PROGRESS_RESOURCE); // 延迟一下然后完成最后的10% this.scheduleOnce(() => { this.finishLoading(); }, 0.2); } /** 完成加载 */ private finishLoading() { // this.updateLoadingText('Loading More...'); // 最后10%快速完成 this.updateProgress(this.PROGRESS_COMPLETE); // 0.5秒后显示开始按钮 this.scheduleOnce(() => { this.onLoadComplete(); }, 0.5); } /** 更新进度条 */ private updateProgress(progress: number) { tween(this.progressBar) .to(this.PROGRESS_ANIMATION_DURATION, { progress }, { easing: 'smooth', onUpdate: (target: any) => { const currentProgress = target.progress; this.progressLabel.string = `${Math.floor(currentProgress * 100)}%`; this.maskSpineNode.getComponent(UITransform).setContentSize(currentProgress * 509, 100); this.lightSpineNode.setPosition(currentProgress * 509 - 278, -612, 0) } }) .start(); } /** 更新加载文本 */ private updateLoadingText(key: string) { this.loadingLabel.string = `${I18nManager.instance.t(key)}...`; } /** 加载完成 */ private onLoadComplete() { this.hideProgressUI(); this.showStartButton(); } /** 隐藏进度UI */ private hideProgressUI() { this.loadingLabel.string = ''; this.progressBar.node.active = false; this.progressLabel.node.active = false; this.maskSpineNode.active = false; this.lightSpineNode.active = false; } /** 显示开始按钮 */ private showStartButton() { this.startBtnNode.active = true; } /** 开始游戏 */ // private startGame() { // if (!this.gameContainer) { // console.error('Game container not found'); // return; // } // this.gameContainer.addChild(this.gameNode); // // this.node.destroy(); // } /** 按钮点击事件 */ onStartBtnClick() { this.rb7Logo.active = false; // 添加到场景 this.gameContainer.addChild(this.gameNode); // 隐藏加载界面 this.LoadingUINode.destroy(); } onDestroy() { this.unscheduleAllCallbacks(); tween(this.progressBar).stop(); NodePoolManager.instance.clearAll(); } }