refactor(game): 重构全局数据管理逻辑
1. 重构GameDate接口,新增多个游戏数据字段并优化结构 2. 将全局数据从SingletonModuleComp实例迁移至data对象中统一管理 3. 简化云端数据同步逻辑,使用Object.assign批量赋值 4. 调整getGameDate方法,直接展开data对象生成返回数据 5. 修复代码缩进与空白行格式问题
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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