feat: 添加页面管理系统和首页/关卡页面
- 实现 ViewManager 单例页面管理器,支持页面注册、打开、关闭、缓存 - 实现 BaseView 页面基类,提供统一的页面生命周期 - 添加 PageHome 首页,包含开始游戏按钮跳转功能 - 添加 PageLevel 关卡页面,继承 BaseView - 更新 PageLoading 支持进度条显示和页面预加载 - 添加相关图片资源和预制体 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
131
assets/scripts/core/BaseView.ts
Normal file
131
assets/scripts/core/BaseView.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import { _decorator, Component } from 'cc';
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
/**
|
||||
* 页面配置接口
|
||||
*/
|
||||
export interface ViewConfig {
|
||||
prefabPath: string; // 相对于 resources 的路径
|
||||
cache?: boolean; // 是否缓存页面,默认 true
|
||||
zIndex?: number; // 层级,默认 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面打开选项
|
||||
*/
|
||||
export interface ViewOptions {
|
||||
params?: any; // 传递给页面的参数
|
||||
onComplete?: (view: BaseView) => void;
|
||||
onError?: (err: Error) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面基类
|
||||
* 所有页面组件应继承此类,实现生命周期方法
|
||||
*/
|
||||
@ccclass('BaseView')
|
||||
export class BaseView extends Component {
|
||||
/** 页面唯一标识 */
|
||||
viewId: string = '';
|
||||
|
||||
/** 页面配置 */
|
||||
config: ViewConfig | null = null;
|
||||
|
||||
/** 是否正在显示 */
|
||||
isShowing: boolean = false;
|
||||
|
||||
/** 传递给页面的参数 */
|
||||
protected _params: any = null;
|
||||
|
||||
/**
|
||||
* 设置页面参数
|
||||
*/
|
||||
setParams(params: any): void {
|
||||
this._params = params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取页面参数
|
||||
*/
|
||||
getParams(): any {
|
||||
return this._params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面加载时调用(首次创建时)
|
||||
* 子类应重写此方法
|
||||
*/
|
||||
onViewLoad(): void {
|
||||
// 子类实现
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面显示时调用(每次打开时)
|
||||
* 子类应重写此方法
|
||||
*/
|
||||
onViewShow(): void {
|
||||
// 子类实现
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面隐藏时调用(关闭或被其他页面覆盖时)
|
||||
* 子类应重写此方法
|
||||
*/
|
||||
onViewHide(): void {
|
||||
// 子类实现
|
||||
}
|
||||
|
||||
/**
|
||||
* 页面销毁时调用
|
||||
* 子类应重写此方法
|
||||
*/
|
||||
onViewDestroy(): void {
|
||||
// 子类实现
|
||||
}
|
||||
|
||||
// ========== 内部方法,由 ViewManager 调用 ==========
|
||||
|
||||
/**
|
||||
* 内部方法:执行显示逻辑
|
||||
*/
|
||||
_doShow(): void {
|
||||
if (this.isShowing) return;
|
||||
this.isShowing = true;
|
||||
this.node.active = true;
|
||||
this.onViewShow();
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部方法:执行隐藏逻辑
|
||||
*/
|
||||
_doHide(): void {
|
||||
if (!this.isShowing) return;
|
||||
this.isShowing = false;
|
||||
this.onViewHide();
|
||||
this.node.active = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部方法:执行销毁逻辑
|
||||
*/
|
||||
_doDestroy(): void {
|
||||
// 标记已销毁,防止 onDestroy 中重复调用
|
||||
this._destroyed = true;
|
||||
this.node.destroy();
|
||||
}
|
||||
|
||||
/** 是否已标记销毁 */
|
||||
private _destroyed: boolean = false;
|
||||
|
||||
// ========== Cocos 生命周期 ==========
|
||||
|
||||
protected onDestroy(): void {
|
||||
// 仅在未被 _doDestroy 调用时执行生命周期
|
||||
if (!this._destroyed) {
|
||||
if (this.isShowing) {
|
||||
this.onViewHide();
|
||||
}
|
||||
}
|
||||
this.onViewDestroy();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user