Files
heros/assets/script/game/initialize/Initialize.ts
2025-10-19 20:10:19 +08:00

213 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @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 { WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
import { Test } from "../common/Test";
// 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) => {
try {
// 加载多语言对应字体
oops.res.load("language/font/" + oops.language.current, async () => {
// 统一的数据加载流程
await this.loadGameDataUnified();
next();
});
//加载精灵配置表
// oops.res.load("config/game/heros", next);
} catch (error) {
console.error("[Initialize]: 自定义内容加载失败:", error);
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 isWxClient(): boolean {
// 检查是否存在微信API
return typeof wx !== 'undefined' && typeof (wx as any).getSystemInfoSync === 'function';
}
/**
* 统一的游戏数据加载流程
* 微信客户端:使用云端数据
* 非微信客户端:使用本地调试数据
*/
private async loadGameDataUnified() {
try {
console.log("[Initialize]: 开始统一数据加载流程...");
if (this.isWxClient()) {
// 微信客户端:加载云端数据
console.log("[Initialize]: 检测到微信客户端,使用云端数据");
await this.loadFromCloud();
} else {
// 非微信客户端:使用本地调试数据
console.log("[Initialize]: 非微信客户端,使用本地调试数据");
await this.loadFromLocalDebug();
}
} catch (error) {
console.error("[Initialize]: 统一数据加载失败:", error);
// 失败时使用默认数据 游戏需要退出
}
}
/**
* 从云端加载数据(微信客户端)
*/
private async loadFromCloud() {
try {
// 1. 初始化微信云环境
this.initWxCloudEnv();
// 2. 登录并获取云端数据
const loginResult = await WxCloudApi.login();
const response = loginResult.result;
if (loginResult.result.code === 200) {
console.log("[Initialize]: 云端登录成功");
const cloudData = loginResult.result.data;
try {
// 直接覆盖基础游戏数据
if (cloudData.openid) {
smc.openid=cloudData.openid
}
// 直接覆盖出战英雄配置
if (cloudData.game_data) {
let gameDate=cloudData.game_data
if( gameDate.gold ) smc.vmdata.gold=gameDate.gold
if( gameDate.heros ) smc.heros=gameDate.heros
if( gameDate.fight_hero ) smc.fight_hero=gameDate.fight_hero
}
} catch (error) {
console.error(`[SMC]: 数据覆盖失败:`, error);
}
} else {
console.warn("[Initialize]: 云端登录失败:", response.msg);
// 登录失败时使用本地数据 游戏需要退出
console.log("[Initialize]: 云端登录失败:", response.msg);
}
} catch (error) {
console.error("[Initialize]: 云端数据加载异常:", error);
// 异常时使用本地数据 游戏需要退出
}
}
/**
* 从本地调试数据加载(非微信客户端)
*/
private async loadFromLocalDebug() {
try {
// 使用本地调试API模拟云端接口
// 用本地调试数据覆盖客户端数据
} catch (error) {
console.error("[Initialize]: 本地调试数据加载异常:", error);
}
}
/**
* 初始化微信云环境
*/
private initWxCloudEnv() {
try {
// 请替换为您的实际云环境ID
const cloudEnvId = "cloud1-6gknw0qk911036d8"; // TODO: 配置您的云环境ID
WxCloudApi.init(cloudEnvId);
console.log("[Initialize]: 微信云环境初始化完成");
} catch (error) {
console.error("[Initialize]: 微信云环境初始化失败:", error);
}
}
/** 加载完成进入游戏内容加载界面 */
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);
};
}
}