refactor(game): 重构全局数据管理逻辑
1. 重构GameDate接口,新增多个游戏数据字段并优化结构 2. 将全局数据从SingletonModuleComp实例迁移至data对象中统一管理 3. 简化云端数据同步逻辑,使用Object.assign批量赋值 4. 调整getGameDate方法,直接展开data对象生成返回数据 5. 修复代码缩进与空白行格式问题
This commit is contained in:
@@ -31,7 +31,7 @@ export class Initialize extends ecs.Entity {
|
||||
// --- 全局注入:为所有 Button 组件增加点击音效 ---
|
||||
const originalOnTouchEnded = Button.prototype["_onTouchEnded"];
|
||||
if (originalOnTouchEnded) {
|
||||
Button.prototype["_onTouchEnded"] = function(event) {
|
||||
Button.prototype["_onTouchEnded"] = function (event) {
|
||||
if (this.interactable && this.node && this.node.activeInHierarchy) {
|
||||
oops.audio.playEffect("music/button");
|
||||
}
|
||||
@@ -57,15 +57,15 @@ export class Initialize extends ecs.Entity {
|
||||
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);
|
||||
//加载精灵配置表
|
||||
// oops.res.load("config/game/heros", next);
|
||||
} catch (error) {
|
||||
mLogger.error(this.debugMode, 'Initialize', "[Initialize]: 自定义内容加载失败:", error);
|
||||
next(); // 即使失败也要继续,不阻塞游戏启动
|
||||
@@ -110,7 +110,7 @@ export class Initialize extends ecs.Entity {
|
||||
private async loadGameDataUnified() {
|
||||
try {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 开始统一数据加载流程...");
|
||||
|
||||
|
||||
if (gameDataSync.isWxClient()) {
|
||||
// 微信客户端:加载云端数据
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 检测到微信客户端,使用云端数据");
|
||||
@@ -120,7 +120,7 @@ export class Initialize extends ecs.Entity {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 非微信客户端,使用本地调试数据");
|
||||
await this.loadFromLocalDebug();
|
||||
}
|
||||
|
||||
|
||||
} catch (error) {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 统一数据加载失败:", error);
|
||||
// 失败时使用默认数据 游戏需要退出
|
||||
@@ -134,34 +134,34 @@ export class Initialize extends ecs.Entity {
|
||||
try {
|
||||
// 1. 初始化微信云环境
|
||||
this.initWxCloudEnv();
|
||||
|
||||
|
||||
// 2. 登录并获取云端数据
|
||||
const loginResult = await WxCloudApi.login();
|
||||
const response = loginResult.result;
|
||||
|
||||
if (response && response.code === 200) {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 云端登录成功");
|
||||
|
||||
|
||||
const cloudData = response.data;
|
||||
|
||||
|
||||
if (cloudData && cloudData.openid) {
|
||||
smc.openid = cloudData.openid;
|
||||
smc.data.openid = cloudData.openid;
|
||||
}
|
||||
|
||||
|
||||
// 将 login 获取的 game_data 封装为统一下发格式,交给 gameDataSync 对比与合并
|
||||
const formattedCloudData = {
|
||||
openid: cloudData?.openid || '',
|
||||
data: cloudData?.game_data || {}
|
||||
};
|
||||
|
||||
|
||||
gameDataSync.syncWithCloudData(formattedCloudData);
|
||||
|
||||
|
||||
} else {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 云端登录失败:", response?.msg);
|
||||
// 登录失败时使用本地数据
|
||||
gameDataSync.syncWithCloudData(null);
|
||||
}
|
||||
|
||||
|
||||
} catch (error) {
|
||||
mLogger.log(this.debugMode, 'Initialize', "[Initialize]: 云端数据加载异常:", error);
|
||||
// 异常时使用本地数据兜底
|
||||
|
||||
Reference in New Issue
Block a user