refactor(game): 重构全局数据管理逻辑

1. 重构GameDate接口,新增多个游戏数据字段并优化结构
2. 将全局数据从SingletonModuleComp实例迁移至data对象中统一管理
3. 简化云端数据同步逻辑,使用Object.assign批量赋值
4. 调整getGameDate方法,直接展开data对象生成返回数据
5. 修复代码缩进与空白行格式问题
This commit is contained in:
pan
2026-08-07 10:04:48 +08:00
parent f841779e94
commit 076e03742d
2 changed files with 35 additions and 37 deletions

View File

@@ -15,7 +15,21 @@ import { FightSet } from "./config/GameSet";
* @param remoteData 远程数据(云端或本地调试)
*/
export interface GameDate {
gold: number,
openid?: string,
score?: number,
mission?: number,
diamond?: number,
gold?: number,
task?: number,
noStop?: boolean,
showInfo?: boolean,
tip_done?: {
start: boolean,
hero: boolean,
equip: boolean,
skill: boolean,
shop: boolean,
},
switch_effect?: boolean, // 音效开关
switch_music?: boolean, // 音乐开关
timestamp?: number, // 用于比对本地与云端数据的最新状态
@@ -37,7 +51,6 @@ export class SingletonModuleComp extends ecs.Comp {
/** 全局缓存的通用图集 */
uiconsAtlas: SpriteAtlas | null = null;
openid: string = ''
mission: any = {
status: 0, //0:未开始 1:进行中 2:胜利 3:失败
play: false,
@@ -49,6 +62,7 @@ export class SingletonModuleComp extends ecs.Comp {
monGrid: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
};
data: any = {
openid: '',
score: 0,
mission: 1,
diamond: 100, //商店购买 及 双倍奖励资源
@@ -220,25 +234,11 @@ export class SingletonModuleComp extends ecs.Comp {
try {
// 直接覆盖基础游戏数据
if (cloudData.openid) {
this.openid = cloudData.openid;
this.data.openid = cloudData.openid;
}
// 直接覆盖出战英雄配置
if (cloudData.data) {
const data = cloudData.data;
// 同步金币
if (data.gold !== undefined) {
this.vmdata.gold = data.gold;
}
// 同步音效开关
if (data.switch_effect !== undefined) {
this.vmdata.switch_effect = data.switch_effect;
if (oops.audio) oops.audio.switchEffect = data.switch_effect;
}
// 同步音乐开关
if (data.switch_music !== undefined) {
this.vmdata.switch_music = data.switch_music;
if (oops.audio) oops.audio.switchMusic = data.switch_music;
}
Object.assign(this.data, cloudData.data);
}
// 触发UI更新
@@ -250,9 +250,7 @@ export class SingletonModuleComp extends ecs.Comp {
}
getGameDate() {
let data: GameDate = {
gold: this.vmdata.gold,
switch_effect: this.vmdata.switch_effect,
switch_music: this.vmdata.switch_music,
...this.data,
timestamp: Date.now() // 每次获取当前数据结构时都附带最新的时间戳
};
return data;

View File

@@ -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(); // 即使失败也要继续,不阻塞游戏启动
@@ -145,7 +145,7 @@ export class Initialize extends ecs.Entity {
const cloudData = response.data;
if (cloudData && cloudData.openid) {
smc.openid = cloudData.openid;
smc.data.openid = cloudData.openid;
}
// 将 login 获取的 game_data 封装为统一下发格式,交给 gameDataSync 对比与合并