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, //商店购买 及 双倍奖励资源
@@ -65,7 +79,7 @@ export class SingletonModuleComp extends ecs.Comp {
shop: false, // 购买药品面板按钮
},
}
vmdata: any = {
game_over: false,
game_pause: false,
@@ -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;