| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import { _decorator, Component, Node, Label, ProgressBar } from 'cc';
- import EventBus, { GameEvents } from '../Core/EventBus';
- const { ccclass, property } = _decorator;
- /**
- * 资源预加载进度显示UI组件
- * 用于在游戏启动时显示资源加载进度
- */
- @ccclass('PreloadProgressUI')
- export class PreloadProgressUI extends Component {
-
- @property({ type: Node, tooltip: '预加载进度UI根节点' })
- public progressUINode: Node = null;
-
- @property({ type: ProgressBar, tooltip: '进度条组件' })
- public progressBar: ProgressBar = null;
-
- @property({ type: Label, tooltip: '进度文本标签' })
- public progressLabel: Label = null;
-
- @property({ type: Label, tooltip: '状态文本标签' })
- public statusLabel: Label = null;
-
- private eventBus: EventBus = null;
-
- onLoad() {
- this.eventBus = EventBus.getInstance();
-
- // 监听预加载事件
- this.eventBus.on(GameEvents.RESOURCE_PRELOAD_START, this.onPreloadStart, this);
- this.eventBus.on(GameEvents.RESOURCE_PRELOAD_PROGRESS, this.onPreloadProgress, this);
- this.eventBus.on(GameEvents.RESOURCE_PRELOAD_COMPLETE, this.onPreloadComplete, this);
- this.eventBus.on(GameEvents.RESOURCE_PRELOAD_ERROR, this.onPreloadError, this);
-
- // 初始状态隐藏UI
- this.hideProgressUI();
- }
-
- onDestroy() {
- if (this.eventBus) {
- this.eventBus.off(GameEvents.RESOURCE_PRELOAD_START, this.onPreloadStart, this);
- this.eventBus.off(GameEvents.RESOURCE_PRELOAD_PROGRESS, this.onPreloadProgress, this);
- this.eventBus.off(GameEvents.RESOURCE_PRELOAD_COMPLETE, this.onPreloadComplete, this);
- this.eventBus.off(GameEvents.RESOURCE_PRELOAD_ERROR, this.onPreloadError, this);
- }
- }
-
- /**
- * 预加载开始事件处理
- */
- private onPreloadStart() {
- console.log('[PreloadProgressUI] 预加载开始');
- this.showProgressUI();
- this.updateProgress(0, '开始加载资源...');
- this.updateStatus('正在预加载关卡资源');
- }
-
- /**
- * 预加载进度更新事件处理
- */
- private onPreloadProgress(data: { progress: number; message: string }) {
- console.log(`[PreloadProgressUI] 预加载进度: ${data.progress}% - ${data.message}`);
- this.updateProgress(data.progress, data.message);
- }
-
- /**
- * 预加载完成事件处理
- */
- private onPreloadComplete() {
- console.log('[PreloadProgressUI] 预加载完成');
- this.updateProgress(100, '资源加载完成');
- this.updateStatus('预加载完成,准备开始游戏');
-
- // 延迟隐藏UI,让用户看到完成状态
- this.scheduleOnce(() => {
- this.hideProgressUI();
- }, 0.5);
- }
-
- /**
- * 预加载错误事件处理
- */
- private onPreloadError(data: { error: string }) {
- console.warn(`[PreloadProgressUI] 预加载错误: ${data.error}`);
- this.updateStatus(`预加载失败: ${data.error}`);
-
- // 延迟隐藏UI
- this.scheduleOnce(() => {
- this.hideProgressUI();
- }, 2.0);
- }
-
- /**
- * 显示进度UI
- */
- private showProgressUI() {
- if (this.progressUINode) {
- this.progressUINode.active = true;
- }
- }
-
- /**
- * 隐藏进度UI
- */
- private hideProgressUI() {
- if (this.progressUINode) {
- this.progressUINode.active = false;
- }
- }
-
- /**
- * 更新进度条和进度文本
- */
- private updateProgress(progress: number, message: string) {
- if (this.progressBar) {
- this.progressBar.progress = progress / 100;
- }
-
- if (this.progressLabel) {
- this.progressLabel.string = `${Math.round(progress)}%`;
- }
-
- if (message && this.statusLabel) {
- this.updateStatus(message);
- }
- }
-
- /**
- * 更新状态文本
- */
- private updateStatus(status: string) {
- if (this.statusLabel) {
- this.statusLabel.string = status;
- }
- }
-
- /**
- * 手动显示预加载进度(用于测试)
- */
- public showPreloadProgress() {
- this.showProgressUI();
- this.updateProgress(0, '手动测试预加载进度');
- this.updateStatus('测试模式');
- }
-
- /**
- * 手动隐藏预加载进度(用于测试)
- */
- public hidePreloadProgress() {
- this.hideProgressUI();
- }
- }
|