Files
heros/assets/script/game/initialize/Initialize.ts
2025-08-12 16:58:29 +08:00

135 lines
4.6 KiB
TypeScript

/*
* @Author: dgflash
* @Date: 2021-11-11 17:45:23
* @LastEditors: dgflash
* @LastEditTime: 2022-08-17 12:38:59
*/
import { Node } from "cc";
import { UICallbacks } from "../../../../extensions/oops-plugin-framework/assets/core/gui/layer/Defines";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { AsyncQueue, NextFunction } from "../../../../extensions/oops-plugin-framework/assets/libs/collection/AsyncQueue";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { UIID } from "../common/config/GameUIConfig";
import { LoadingViewComp } from "./view/LoadingViewComp";
import { smc } from "../common/SingletonModuleComp";
// import {data} from "../data/data";
/**
* 游戏进入初始化模块
* 1、热更新
* 2、加载默认资源
*/
@ecs.register(`Initialize`)
export class Initialize extends ecs.Entity {
LoadingView!: LoadingViewComp;
// data:data=null!;
protected init() {
// this.data=ecs.getEntity<data>(data);
// this.data.dataModel.vmAdd();
var queue: AsyncQueue = new AsyncQueue();
// 加载自定义资
this.loadCustom(queue);
// 加载多语言包
this.loadLanguage(queue);
// 加载公共资源
this.loadCommon(queue);
// 加载游戏内容加载进度提示界面
this.onComplete(queue);
queue.play();
}
/** 加载自定义内容(可选) */
private loadCustom(queue: AsyncQueue) {
queue.push(async (next: NextFunction, params: any, args: any) => {
// 加载多语言对应字体
oops.res.load("language/font/" + oops.language.current, () => {
// 加载本地英雄数据
this.loadHeroDataFromLocal();
next();
});
//加载精灵配置表
// oops.res.load("config/game/heros", next);
});
}
/** 加载化语言包(可选) */
private loadLanguage(queue: AsyncQueue) {
queue.push((next: NextFunction, params: any, args: any) => {
// 设置默认语言
let lan = oops.storage.get("language");
// if (lan == null) {
if (lan == null || lan == "") {
lan = "zh";
oops.storage.set("language", lan);
}
// 设置语言包路径
// oops.language.pack.json = oops.config.game.languagePathJson;
// oops.language.pack.texture = oops.config.game.languagePathTexture;
// 加载语言包资源
oops.language.setLanguage(lan, next);
});
}
/** 加载公共资源(必备) */
private loadCommon(queue: AsyncQueue) {
queue.push((next: NextFunction, params: any, args: any) => {
oops.res.loadDir("common", next);
});
}
/**
* 从本地存储加载游戏数据
*/
private loadHeroDataFromLocal() {
// 检查本地存储状态
const storageStatus = smc.checkLocalStorage();
if (storageStatus.isFirstTime) {
console.log("[Initialize]: 检测到首次启动游戏");
}
// 使用SingletonModuleComp的加载方法
// loadGameData方法已经包含了首次启动的处理逻辑
const success = smc.loadGameData();
if (success) {
console.log("[Initialize]: 游戏数据加载完成");
} else {
console.log("[Initialize]: 游戏数据加载失败,使用默认配置");
// 如果加载失败,初始化默认数据并保存
smc.initDefaultData();
smc.saveGameData();
}
// 再次检查存储状态,确认数据已保存
if (storageStatus.isFirstTime) {
const newStatus = smc.checkLocalStorage();
if (newStatus.hasFightHeros && newStatus.hasHeros && newStatus.hasGameData) {
console.log("[Initialize]: 首次启动数据保存成功");
} else {
console.warn("[Initialize]: 首次启动数据保存可能失败");
}
}
}
/** 加载完成进入游戏内容加载界面 */
private onComplete(queue: AsyncQueue) {
queue.complete = () => {
var uic: UICallbacks = {
onAdded: (node: Node, params: any) => {
var comp = node.getComponent(LoadingViewComp) as ecs.Comp;
this.add(comp);
}
};
// 界面管理 - 打开游戏内容资源加载进度提示界面
oops.gui.open(UIID.Loading, null, uic);
};
}
}