云环境和本地调试 添加
This commit is contained in:
568
assets/script/game/common/GameDataSyncManager.ts
Normal file
568
assets/script/game/common/GameDataSyncManager.ts
Normal file
@@ -0,0 +1,568 @@
|
||||
import { WxCloudApi ,UserGameData} from "../wx_clound_client_api/WxCloudApi";
|
||||
import { smc } from "./SingletonModuleComp";
|
||||
|
||||
/**
|
||||
* 游戏数据同步管理器
|
||||
* 负责管理fight_heros、heros、items、tals、equips的远程操作和本地同步
|
||||
* 只有在远程修改成功后才同步修改本地数据
|
||||
*/
|
||||
export class GameDataSyncManager {
|
||||
private static instance: GameDataSyncManager;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): GameDataSyncManager {
|
||||
if (!GameDataSyncManager.instance) {
|
||||
GameDataSyncManager.instance = new GameDataSyncManager();
|
||||
}
|
||||
return GameDataSyncManager.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用远程数据覆盖本地数据(统一方法)
|
||||
* @param remoteData 远程数据(云端或本地调试)
|
||||
* @param dataSource 数据源描述
|
||||
*/
|
||||
async overrideLocalDataWithRemote(remoteData: UserGameData, dataSource: string) {
|
||||
try {
|
||||
console.log(`[Initialize]: 开始用${dataSource}数据覆盖客户端数据...`);
|
||||
|
||||
// 直接覆盖基础游戏数据
|
||||
if (remoteData.data) {
|
||||
Object.assign(smc.data, remoteData.data);
|
||||
console.log(`[Initialize]: 基础游戏数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖出战英雄配置
|
||||
if (remoteData.fight_heros) {
|
||||
Object.assign(smc.fight_heros, remoteData.fight_heros);
|
||||
console.log(`[Initialize]: 出战英雄配置已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖英雄数据
|
||||
if (remoteData.heros) {
|
||||
smc.heros = { ...remoteData.heros };
|
||||
console.log(`[Initialize]: 英雄数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖道具数据
|
||||
if (remoteData.items) {
|
||||
Object.assign(smc.itmes, remoteData.items);
|
||||
console.log(`[Initialize]: 道具数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖天赋数据
|
||||
if (remoteData.tals) {
|
||||
Object.assign(smc.tals, remoteData.tals);
|
||||
console.log(`[Initialize]: 天赋数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 直接覆盖装备数据
|
||||
if (remoteData.equips) {
|
||||
Object.assign(smc.equips, remoteData.equips);
|
||||
console.log(`[Initialize]: 装备数据已从${dataSource}覆盖`);
|
||||
}
|
||||
|
||||
// 同步ViewModel数据
|
||||
smc.syncData();
|
||||
|
||||
// 保存到本地存储(确保数据持久化)
|
||||
// smc.saveGameData();
|
||||
|
||||
console.log(`[Initialize]: ${dataSource}数据覆盖完成,已保存到本地`);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[Initialize]: ${dataSource}数据覆盖失败:`, error);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 批量更新出战英雄配置
|
||||
* @param fightHeros 出战英雄配置对象
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async updateFightHeros(fightHeros: any): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 批量更新出战英雄配置:`, fightHeros);
|
||||
|
||||
const result = await WxCloudApi.updateFightHeros(fightHeros);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
Object.assign(smc.fight_heros, fightHeros);
|
||||
console.log(`[GameDataSyncManager]: 出战英雄配置更新成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 出战英雄配置更新失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 更新出战英雄配置异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重置出战英雄配置为默认值
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async resetFightHeros(): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 重置出战英雄配置`);
|
||||
|
||||
const result = await WxCloudApi.resetFightHeros();
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.fight_heros = result.result.data;
|
||||
console.log(`[GameDataSyncManager]: 出战英雄配置重置成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 出战英雄配置重置失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 重置出战英雄配置异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 英雄管理 ====================
|
||||
|
||||
/**
|
||||
* 添加新英雄到用户库存
|
||||
* @param heroId 英雄ID
|
||||
* @param heroData 英雄数据(可选)
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addHero(heroId: number, heroData?: any): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 添加英雄 ID:${heroId}, 数据:`, heroData);
|
||||
|
||||
const result = await WxCloudApi.addHero(heroId, heroData);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.heros[heroId] = result.result.data;
|
||||
console.log(`[GameDataSyncManager]: 英雄添加成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄添加失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 添加英雄异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新英雄的多个属性
|
||||
* @param heroId 英雄ID
|
||||
* @param updateData 要更新的属性
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async updateHero(heroId: number, updateData: any): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 更新英雄 ID:${heroId}, 更新数据:`, updateData);
|
||||
|
||||
const result = await WxCloudApi.updateHero(heroId, updateData);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
Object.assign(smc.heros[heroId], result.result.data.new_data);
|
||||
console.log(`[GameDataSyncManager]: 英雄更新成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄更新失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 更新英雄异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置英雄的单个属性值
|
||||
* @param heroId 英雄ID
|
||||
* @param property 属性名
|
||||
* @param value 属性值
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async setHeroProperty(heroId: number, property: any, value: any): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 设置英雄属性 ID:${heroId}, 属性:${property}, 值:${value}`);
|
||||
|
||||
const result = await WxCloudApi.setHeroProperty(heroId, property, value);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.heros[heroId][property] = value;
|
||||
console.log(`[GameDataSyncManager]: 英雄属性设置成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄属性设置失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 设置英雄属性异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 英雄升级指定级数
|
||||
* @param heroId 英雄ID
|
||||
* @param levels 升级级数(默认1级)
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async levelUpHero(heroId: number, exp:number,gold:number,levels: number = 1,): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 英雄升级 ID:${heroId}, 级数:${levels}`);
|
||||
|
||||
const result = await WxCloudApi.levelUpHero(heroId, exp,gold,levels);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
smc.heros[heroId].lv = result.result.data.new_value;
|
||||
console.log(`[GameDataSyncManager]: 英雄升级成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄升级失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 英雄升级异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定英雄
|
||||
* @param heroId 英雄ID
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async deleteHero(heroId: number): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 删除英雄 ID:${heroId}`);
|
||||
|
||||
const result = await WxCloudApi.deleteHero(heroId);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
delete smc.heros[heroId];
|
||||
console.log(`[GameDataSyncManager]: 英雄删除成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 英雄删除失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 删除英雄异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 库存管理 (items, tals, equips) ====================
|
||||
|
||||
/**
|
||||
* 增加指定物品的数量
|
||||
* @param type 库存类型 ('items', 'tals', 'equips')
|
||||
* @param itemId 物品ID
|
||||
* @param count 添加数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addInventoryItem(type: 'items' | 'tals' | 'equips', itemId: number, count: number): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 增加库存物品 类型:${type}, ID:${itemId}, 数量:${count}`);
|
||||
|
||||
const result = await WxCloudApi.addInventoryItem(type, itemId, count);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
const targetData = this.getTargetData(type);
|
||||
targetData[itemId] = (targetData[itemId] || 0) + count;
|
||||
console.log(`[GameDataSyncManager]: 库存物品增加成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 库存物品增加失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 增加库存物品异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗指定数量的物品
|
||||
* @param type 库存类型 ('items', 'tals', 'equips')
|
||||
* @param itemId 物品ID
|
||||
* @param count 消耗数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async consumeInventoryItem(type: 'items' | 'tals' | 'equips', itemId: number, count: number): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 消耗库存物品 类型:${type}, ID:${itemId}, 数量:${count}`);
|
||||
|
||||
const result = await WxCloudApi.consumeInventoryItem(type, itemId, count);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
const targetData = this.getTargetData(type);
|
||||
targetData[itemId] = Math.max(0, (targetData[itemId] || 0) - count);
|
||||
console.log(`[GameDataSyncManager]: 库存物品消耗成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 库存物品消耗失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 消耗库存物品异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接设置物品的数量
|
||||
* @param type 库存类型 ('items', 'tals', 'equips')
|
||||
* @param itemId 物品ID
|
||||
* @param count 新的数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async setInventoryItem(type: 'items' | 'tals' | 'equips', itemId: number, count: number): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 设置库存物品 类型:${type}, ID:${itemId}, 数量:${count}`);
|
||||
|
||||
const result = await WxCloudApi.setInventoryItem(type, itemId, count);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
const targetData = this.getTargetData(type);
|
||||
targetData[itemId] = count;
|
||||
console.log(`[GameDataSyncManager]: 库存物品设置成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 库存物品设置失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 设置库存物品异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新多个物品的数量
|
||||
* @param type 库存类型 ('items', 'tals', 'equips')
|
||||
* @param data 更新数据对象
|
||||
* @param merge 是否合并更新(默认true)
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async updateInventory(type: 'items' | 'tals' | 'equips', data: any, merge: boolean = true): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 批量更新库存 类型:${type}, 数据:`, data);
|
||||
|
||||
const result = await WxCloudApi.updateInventory(type, data, merge);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
const targetData = this.getTargetData(type);
|
||||
if (merge) {
|
||||
Object.assign(targetData, data);
|
||||
} else {
|
||||
Object.keys(targetData).forEach(key => delete targetData[key]);
|
||||
Object.assign(targetData, data);
|
||||
}
|
||||
console.log(`[GameDataSyncManager]: 库存批量更新成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 库存批量更新失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 批量更新库存异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置指定类型的库存为默认值
|
||||
* @param type 库存类型 ('items', 'tals', 'equips')
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async resetInventory(type: 'items' | 'tals' | 'equips'): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 重置库存 类型:${type}`);
|
||||
|
||||
const result = await WxCloudApi.resetInventory(type);
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程修改成功,同步本地数据
|
||||
const targetData = this.getTargetData(type);
|
||||
Object.keys(targetData).forEach(key => delete targetData[key]);
|
||||
Object.assign(targetData, result.result.data);
|
||||
console.log(`[GameDataSyncManager]: 库存重置成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 库存重置失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 重置库存异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 便捷方法 ====================
|
||||
|
||||
/**
|
||||
* 增加道具
|
||||
* @param itemId 道具ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addItem(itemId: number, count: number): Promise<boolean> {
|
||||
return this.addInventoryItem('items', itemId, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗道具
|
||||
* @param itemId 道具ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async consumeItem(itemId: number, count: number): Promise<boolean> {
|
||||
return this.consumeInventoryItem('items', itemId, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加天赋点
|
||||
* @param talId 天赋ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addTalent(talId: number, count: number): Promise<boolean> {
|
||||
return this.addInventoryItem('tals', talId, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗天赋点
|
||||
* @param talId 天赋ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async consumeTalent(talId: number, count: number): Promise<boolean> {
|
||||
return this.consumeInventoryItem('tals', talId, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加装备
|
||||
* @param equipId 装备ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async addEquipment(equipId: number, count: number): Promise<boolean> {
|
||||
return this.addInventoryItem('equips', equipId, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗装备
|
||||
* @param equipId 装备ID
|
||||
* @param count 数量
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async consumeEquipment(equipId: number, count: number): Promise<boolean> {
|
||||
return this.consumeInventoryItem('equips', equipId, count);
|
||||
}
|
||||
|
||||
// ==================== 私有辅助方法 ====================
|
||||
|
||||
/**
|
||||
* 根据类型获取对应的目标数据对象
|
||||
* @param type 库存类型
|
||||
* @returns 对应的数据对象
|
||||
*/
|
||||
private getTargetData(type: 'items' | 'tals' | 'equips'): any {
|
||||
switch (type) {
|
||||
case 'items':
|
||||
return smc.itmes;
|
||||
case 'tals':
|
||||
return smc.tals;
|
||||
case 'equips':
|
||||
return smc.equips;
|
||||
default:
|
||||
throw new Error(`未知的库存类型: ${type}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从云端加载所有游戏数据并同步到本地
|
||||
* @returns 是否成功
|
||||
*/
|
||||
async loadAllGameData(): Promise<boolean> {
|
||||
try {
|
||||
console.log(`[GameDataSyncManager]: 从云端加载所有游戏数据`);
|
||||
|
||||
const result = await WxCloudApi.getAllGameData();
|
||||
|
||||
if (result.result.code === 200) {
|
||||
// 远程数据获取成功,同步本地数据
|
||||
const cloudData = result.result.data;
|
||||
smc.data = cloudData.data;
|
||||
smc.fight_heros = cloudData.fight_heros;
|
||||
smc.heros = cloudData.heros;
|
||||
smc.itmes = cloudData.items;
|
||||
smc.tals = cloudData.tals;
|
||||
smc.equips = cloudData.equips;
|
||||
|
||||
// 同步vmdata
|
||||
smc.syncData();
|
||||
|
||||
console.log(`[GameDataSyncManager]: 云端数据加载成功,本地数据已同步`);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`[GameDataSyncManager]: 云端数据加载失败: ${result.result.msg}`);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[GameDataSyncManager]: 加载云端数据异常:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 导出单例实例
|
||||
export const gameDataSyncManager = GameDataSyncManager.getInstance();
|
||||
|
||||
/*
|
||||
使用示例:
|
||||
|
||||
// 1. 出战英雄管理
|
||||
await gameDataSyncManager.setFightHero(0, 5001); // 设置位置0的英雄
|
||||
await gameDataSyncManager.swapFightHeros(0, 1); // 交换位置0和1的英雄
|
||||
await gameDataSyncManager.updateFightHeros({0: 5001, 1: 5005}); // 批量更新
|
||||
|
||||
// 2. 英雄管理
|
||||
await gameDataSyncManager.addHero(5008, {uuid: 5008, lv: 1}); // 添加新英雄
|
||||
await gameDataSyncManager.levelUpHero(5001, 5); // 英雄升级5级
|
||||
await gameDataSyncManager.setHeroProperty(5001, "exp", 1000); // 设置英雄经验
|
||||
|
||||
// 3. 库存管理
|
||||
await gameDataSyncManager.addItem(1001, 10); // 增加道具
|
||||
await gameDataSyncManager.consumeItem(1001, 2); // 消耗道具
|
||||
await gameDataSyncManager.addTalent(2001, 5); // 增加天赋点
|
||||
await gameDataSyncManager.addEquipment(3001, 2); // 增加装备
|
||||
|
||||
// 4. 数据加载
|
||||
await gameDataSyncManager.loadAllGameData(); // 从云端加载所有数据
|
||||
|
||||
注意:所有方法都返回 Promise<boolean>,true表示成功,false表示失败
|
||||
只有在远程修改成功后,本地数据才会被同步修改
|
||||
*/
|
||||
9
assets/script/game/common/GameDataSyncManager.ts.meta
Normal file
9
assets/script/game/common/GameDataSyncManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "89c45a3b-d0bf-4e45-9e27-b7d714ba7e29",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -4,6 +4,10 @@ import { Initialize } from "../initialize/Initialize";
|
||||
import { GameMap } from "../map/GameMap";
|
||||
import { HeroUI, VmInfo } from "./config/Mission";
|
||||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||||
import { WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
|
||||
import { gameDataSyncManager } from "./GameDataSyncManager";
|
||||
import { GameSet } from "./config/BoxSet";
|
||||
import { Test } from "./Test";
|
||||
|
||||
|
||||
// import { Role } from "../role/Role";
|
||||
@@ -15,6 +19,8 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
initialize: Initialize = null!;
|
||||
/** 游戏地图 */
|
||||
map: GameMap = null!;
|
||||
/** 游戏数据同步管理器 */
|
||||
private gameDataSyncManager = gameDataSyncManager;
|
||||
mission:any={
|
||||
status:0, //0:未开始 1:进行中 2:胜利 3:失败
|
||||
play:false,
|
||||
@@ -37,21 +43,19 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
ckey:0, //铜钥匙 解锁稀有英雄 也可以直接兑换金币
|
||||
skey:0, //银钥匙 解锁史诗英雄 也可以直接兑换金币
|
||||
gkey:0, //金钥匙 解锁传说英雄 也可以直接兑换金币
|
||||
|
||||
}
|
||||
fight_heros:any={
|
||||
0:5001,
|
||||
1:5005,
|
||||
2:0,
|
||||
3:0,
|
||||
4:0,
|
||||
}
|
||||
fight_heros:any={ 0:5001, 1:5005, 2:0, 3:0, 4:0, }
|
||||
heros:any = {
|
||||
5001:{uuid:5001,lv:1},
|
||||
5005:{uuid:5005,lv:1},
|
||||
5007:{uuid:5007,lv:1},
|
||||
};
|
||||
|
||||
itmes:any={
|
||||
}
|
||||
tals:any={
|
||||
}
|
||||
equips:any={
|
||||
}
|
||||
monsters:any = [];
|
||||
sk_info:any = []
|
||||
monsters_dead:any = []
|
||||
@@ -60,32 +64,46 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
vmdata: any = {
|
||||
game_over:false,
|
||||
game_pause:false,
|
||||
data:{
|
||||
score:0,
|
||||
mission:1,
|
||||
gold:100, //升级主要资源
|
||||
diamond:100, //商店购买 及 双倍奖励资源
|
||||
meat:0,
|
||||
exp:0, //升级经验
|
||||
ghstone:0, //英雄石 绿色 普通英雄升星
|
||||
bhstone:0, //英雄石 蓝色 稀有英雄升星
|
||||
phlestone:0, //英雄石 紫色 史诗英雄升星
|
||||
rhstone:0, //英雄石 红色 传说英雄升星
|
||||
herocard:0, //英雄卡,抽卡凭证
|
||||
ckey:0, //铜钥匙 解锁稀有英雄 也可以直接兑换金币
|
||||
skey:0, //银钥匙 解锁史诗英雄 也可以直接兑换金币
|
||||
gkey:0, //金钥匙 解锁传说英雄 也可以直接兑换金币
|
||||
},
|
||||
mission_data:{
|
||||
gold:1000,//金币
|
||||
exp:0,//经验
|
||||
score:0,//分数
|
||||
diamond:0,//钻石
|
||||
mission:1,//关卡
|
||||
chapter:1,//章节
|
||||
level:1,//关卡等级
|
||||
max_mission:4,//最大关卡
|
||||
meat:0,//肉
|
||||
mon_num:0,//怪物数量
|
||||
hero_num:0,//英雄数量
|
||||
wave_time_num:0,//波次时间
|
||||
in_fight:false,
|
||||
fight_time:0,//战斗时间
|
||||
level:1,//关卡等级
|
||||
max_mission:4,//最大关卡
|
||||
},
|
||||
reward:{
|
||||
gold:0,
|
||||
meat:0,
|
||||
diamond:0,
|
||||
score:0,
|
||||
exp:0,
|
||||
gcard:0,
|
||||
bcard:0,
|
||||
pcard:0,
|
||||
ycard:0,
|
||||
mission:0,
|
||||
gold:0, //升级主要资源
|
||||
diamond:0, //商店购买 及 双倍奖励资源
|
||||
meat:0,
|
||||
exp:0, //升级经验
|
||||
ghstone:0, //英雄石 绿色 普通英雄升星
|
||||
bhstone:0, //英雄石 蓝色 稀有英雄升星
|
||||
phlestone:0, //英雄石 紫色 史诗英雄升星
|
||||
rhstone:0, //英雄石 红色 传说英雄升星
|
||||
herocard:0, //英雄卡,抽卡凭证
|
||||
ckey:0, //铜钥匙 解锁稀有英雄 也可以直接兑换金币
|
||||
skey:0, //银钥匙 解锁史诗英雄 也可以直接兑换金币
|
||||
gkey:0, //金钥匙 解锁传说英雄 也可以直接兑换金币
|
||||
}
|
||||
};
|
||||
vmAdd() {
|
||||
@@ -97,483 +115,97 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 本地存储管理方法 ====================
|
||||
|
||||
/**
|
||||
* 初始化默认游戏数据
|
||||
*/
|
||||
initDefaultData() {
|
||||
// 确保出战英雄有默认值
|
||||
if (!this.fight_heros || Object.keys(this.fight_heros).length === 0) {
|
||||
this.fight_heros = {
|
||||
0: 5001,
|
||||
1: 5005,
|
||||
2: 0,
|
||||
3: 0,
|
||||
4: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// 确保英雄属性有默认值
|
||||
if (!this.heros || Object.keys(this.heros).length === 0) {
|
||||
this.heros = {
|
||||
5001: {lv: 1},
|
||||
5005: {lv: 1},
|
||||
5007: {lv: 1},
|
||||
};
|
||||
}
|
||||
|
||||
// 确保游戏数据有默认值
|
||||
if (!this.data || Object.keys(this.data).length === 0) {
|
||||
this.data = {
|
||||
score: 888,
|
||||
mission: 1,
|
||||
gold: 100,
|
||||
diamond: 100,
|
||||
};
|
||||
}
|
||||
|
||||
console.log("[SMC]: 默认数据已初始化 - 出战英雄:", this.fight_heros, "英雄属性:", this.heros, "游戏数据:", this.data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存游戏数据到本地存储
|
||||
*/
|
||||
saveGameData() {
|
||||
try {
|
||||
// 保存出战英雄数据
|
||||
const fightHerosJson = JSON.stringify(this.fight_heros);
|
||||
oops.storage.set("fight_heros", fightHerosJson);
|
||||
|
||||
// 保存英雄属性数据
|
||||
const herosJson = JSON.stringify(this.heros);
|
||||
oops.storage.set("heros", herosJson);
|
||||
|
||||
// 保存游戏数据(金币、钻石等)
|
||||
const dataJson = JSON.stringify(this.data);
|
||||
oops.storage.set("game_data", dataJson);
|
||||
|
||||
console.log("[SMC]: 游戏数据已保存 - 出战英雄:", this.fight_heros, "英雄属性:", this.heros, "游戏数据:", this.data);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("[SMC]: 保存游戏数据失败:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存英雄数据到本地存储(兼容旧方法名)
|
||||
*/
|
||||
saveHeroData() {
|
||||
return this.saveGameData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从本地存储加载游戏数据
|
||||
*/
|
||||
loadGameData() {
|
||||
console.log("[SMC]: 加载游戏数据")
|
||||
try {
|
||||
let hasAnyData = false;
|
||||
|
||||
// 加载出战英雄数据
|
||||
const savedFightHeros = oops.storage.get("fight_heros");
|
||||
if (savedFightHeros && savedFightHeros !== "") {
|
||||
const fightHerosData = JSON.parse(savedFightHeros);
|
||||
this.fight_heros = fightHerosData;
|
||||
console.log("[SMC]: 从本地加载出战英雄数据:", fightHerosData);
|
||||
hasAnyData = true;
|
||||
} else {
|
||||
console.log("[SMC]: 未找到本地出战英雄数据,使用默认配置");
|
||||
}
|
||||
|
||||
// 加载英雄属性数据
|
||||
const savedHeros = oops.storage.get("heros");
|
||||
if (savedHeros && savedHeros !== "") {
|
||||
const herosData = JSON.parse(savedHeros);
|
||||
this.heros = herosData;
|
||||
console.log("[SMC]: 从本地加载英雄属性数据:", herosData);
|
||||
hasAnyData = true;
|
||||
} else {
|
||||
console.log("[SMC]: 未找到本地英雄属性数据,使用默认配置");
|
||||
}
|
||||
|
||||
// 加载游戏数据
|
||||
const savedData = oops.storage.get("game_data");
|
||||
if (savedData && savedData !== "") {
|
||||
const gameData = JSON.parse(savedData);
|
||||
this.data = gameData;
|
||||
console.log("[SMC]: 从本地加载游戏数据:", gameData);
|
||||
hasAnyData = true;
|
||||
} else {
|
||||
console.log("[SMC]: 未找到本地游戏数据,使用默认配置");
|
||||
}
|
||||
|
||||
// 如果没有任何本地数据,说明是首次启动,初始化并保存默认数据
|
||||
if (!hasAnyData) {
|
||||
console.log("[SMC]: 首次启动,初始化默认游戏数据");
|
||||
this.initDefaultData();
|
||||
this.saveGameData();
|
||||
return true; // 首次启动也算成功
|
||||
}
|
||||
|
||||
return hasAnyData;
|
||||
} catch (error) {
|
||||
console.error("[SMC]: 加载游戏数据失败:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从本地存储加载英雄数据(兼容旧方法名)
|
||||
*/
|
||||
loadHeroData() {
|
||||
return this.loadGameData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置出战英雄
|
||||
* @param position 出战位置 (0-4)
|
||||
* @param heroId 英雄ID,0表示移除
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
setFightHero(position: number, heroId: number, autoSave: boolean = true) {
|
||||
if (position < 0 || position > 4) {
|
||||
console.warn("[SMC]: 出战位置无效:", position);
|
||||
return false;
|
||||
}
|
||||
|
||||
this.fight_heros[position] = heroId;
|
||||
console.log(`[SMC]: 设置出战位置${position} -> 英雄${heroId}`);
|
||||
|
||||
if (autoSave) {
|
||||
this.saveGameData();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取出战英雄ID
|
||||
* @param position 出战位置 (0-4)
|
||||
*/
|
||||
getFightHero(position: number): number {
|
||||
if (position < 0 || position > 4) {
|
||||
console.warn("[SMC]: 出战位置无效:", position);
|
||||
return 0;
|
||||
}
|
||||
return this.fight_heros[position] || 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前出战英雄列表(不包含空位)
|
||||
*/
|
||||
getActiveFightHeros(): number[] {
|
||||
const activeHeros: number[] = [];
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const heroId = this.fight_heros[i];
|
||||
if (heroId && heroId > 0) {
|
||||
activeHeros.push(heroId);
|
||||
}
|
||||
}
|
||||
return activeHeros;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置英雄属性
|
||||
* @param heroId 英雄ID
|
||||
* @param property 属性名 (如: lv, exp等)
|
||||
* @param value 属性值
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
setHeroProperty(heroId: number, property: string, value: any, autoSave: boolean = true) {
|
||||
if (!this.heros[heroId]) {
|
||||
this.heros[heroId] = {};
|
||||
}
|
||||
|
||||
this.heros[heroId][property] = value;
|
||||
console.log(`[SMC]: 设置英雄${heroId}的${property} = ${value}`);
|
||||
|
||||
if (autoSave) {
|
||||
this.saveGameData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取英雄属性
|
||||
* @param heroId 英雄ID
|
||||
* @param property 属性名
|
||||
* @param defaultValue 默认值
|
||||
*/
|
||||
getHeroProperty(heroId: number, property: string, defaultValue: any = null): any {
|
||||
if (!this.heros[heroId]) {
|
||||
return defaultValue;
|
||||
}
|
||||
return this.heros[heroId][property] !== undefined ? this.heros[heroId][property] : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 英雄升级
|
||||
* @param heroId 英雄ID
|
||||
* @param levels 升级级数 (默认1级)
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
levelUpHero(heroId: number, levels: number = 1, autoSave: boolean = true) {
|
||||
const currentLevel = this.getHeroProperty(heroId, "lv", 1);
|
||||
const newLevel = currentLevel + levels;
|
||||
this.setHeroProperty(heroId, "lv", newLevel, autoSave);
|
||||
console.log(`[SMC]: 英雄${heroId}升级: ${currentLevel} -> ${newLevel}`);
|
||||
}
|
||||
getHasHeroUUID() {
|
||||
return Object.keys(this.heros).map(Number)
|
||||
}
|
||||
/**
|
||||
* 获取英雄等级
|
||||
* @param heroId 英雄ID
|
||||
*/
|
||||
getHeroLevel(heroId: number): number {
|
||||
return this.getHeroProperty(heroId, "lv", 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有本地存储数据
|
||||
*/
|
||||
clearAllSaveData() {
|
||||
try {
|
||||
oops.storage.remove("fight_heros");
|
||||
oops.storage.remove("heros");
|
||||
oops.storage.remove("game_data");
|
||||
console.log("[SMC]: 已清除所有本地存储数据");
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("[SMC]: 清除存储数据失败:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置英雄数据为默认值
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
resetHeroData(autoSave: boolean = true) {
|
||||
// 重置为默认出战英雄
|
||||
this.fight_heros = {
|
||||
0: 5001,
|
||||
1: 5005,
|
||||
2: 0,
|
||||
3: 0,
|
||||
4: 0,
|
||||
};
|
||||
|
||||
// 重置为默认英雄属性
|
||||
this.heros = {
|
||||
5001: {lv: 1},
|
||||
5005: {lv: 1},
|
||||
5007: {lv: 1},
|
||||
};
|
||||
|
||||
console.log("[SMC]: 英雄数据已重置为默认值");
|
||||
|
||||
if (autoSave) {
|
||||
this.saveGameData();
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 游戏数据管理方法 ====================
|
||||
|
||||
/**
|
||||
* 设置游戏数据属性
|
||||
* @param property 属性名 (如: score, mission, gold, diamond)
|
||||
* @param value 属性值
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
setGameProperty(property: string, value: any, autoSave: boolean = true) {
|
||||
this.data[property] = value;
|
||||
this.vmdata.mission_data[property] = value;
|
||||
console.log(`[SMC]: 设置游戏数据 ${property} = ${value}`);
|
||||
if (autoSave) {
|
||||
this.saveGameData();
|
||||
}
|
||||
}
|
||||
addGameProperty(property: string, value: any, autoSave: boolean = true) {
|
||||
this.data[property] = this.data[property] + value;
|
||||
this.vmdata.mission_data[property] = this.data[property]
|
||||
console.log(`[SMC]:增加游戏数据 ${property} = ${value}`);
|
||||
if (autoSave) {
|
||||
this.saveGameData();
|
||||
}
|
||||
}
|
||||
spendGameProperty(property: string, value: any, autoSave: boolean = true) {
|
||||
this.data[property] = this.data[property] - value;
|
||||
this.vmdata.mission_data[property] = this.data[property]
|
||||
console.log(`[SMC]: 消耗游戏数据 ${property} = ${value}`);
|
||||
if (autoSave) {
|
||||
this.saveGameData();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取游戏数据属性
|
||||
* @param property 属性名
|
||||
* @param defaultValue 默认值
|
||||
*/
|
||||
getGameProperty(property: string, defaultValue: any = null): any {
|
||||
return this.data[property] !== undefined ? this.data[property] : defaultValue;
|
||||
}
|
||||
|
||||
|
||||
syncData(){
|
||||
this.vmdata.mission_data.gold=this.getGameProperty("gold", 0)
|
||||
this.vmdata.mission_data.meat=this.getGameProperty("meat", 0)
|
||||
this.vmdata.mission_data.diamond=this.getGameProperty("diamond", 0)
|
||||
this.vmdata.mission_data.mission=this.getGameProperty("mission", 1)
|
||||
this.vmdata.mission_data.score=this.getGameProperty("score", 0)
|
||||
this.vmdata.mission_data.exp=this.getGameProperty("exp", 0)
|
||||
// // 计算章节和关卡等级
|
||||
// const currentMission = this.getGameProperty("mission", 1)
|
||||
// this.vmdata.mission_data.chapter = Math.floor((currentMission - 1) / 10) + 1
|
||||
// this.vmdata.mission_data.level = ((currentMission - 1) % 10) + 1
|
||||
}
|
||||
initReward(){
|
||||
this.vmdata.reward.gold=0
|
||||
this.vmdata.reward.meat=0
|
||||
this.vmdata.reward.diamond=0
|
||||
this.vmdata.reward.score=0
|
||||
this.vmdata.reward.exp=0
|
||||
this.vmdata.reward.gcard=0
|
||||
this.vmdata.reward.bcard=0
|
||||
this.vmdata.reward.pcard=0
|
||||
this.vmdata.reward.ycard=0
|
||||
}
|
||||
/**
|
||||
* 增加金币
|
||||
* @param amount 金币数量
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
addGold(amount: number, autoSave: boolean = true) {
|
||||
const currentGold = this.getGameProperty("gold", 0);
|
||||
const newGold = Math.max(0, currentGold + amount);
|
||||
this.setGameProperty("gold", newGold, autoSave);
|
||||
console.log(`[SMC]: 金币变更: ${currentGold} -> ${newGold} (${amount > 0 ? '+' : ''}${amount})`);
|
||||
return newGold;
|
||||
}
|
||||
// ==================== 数据管理方法 ====================
|
||||
|
||||
/**
|
||||
* 消耗金币
|
||||
* @param amount 消耗数量
|
||||
* 同步数据到vmdata
|
||||
*/
|
||||
syncData(){
|
||||
this.vmdata.data = this.data;
|
||||
}
|
||||
/**
|
||||
* 判断是否为微信客户端
|
||||
*/
|
||||
private isWxClient(): boolean {
|
||||
// 检查是否存在微信API
|
||||
return typeof wx !== 'undefined' && typeof (wx as any).getSystemInfoSync === 'function';
|
||||
}
|
||||
//调试用
|
||||
syncDataFromLocal(){
|
||||
if(this.isWxClient()) return
|
||||
const loginResult = new Test().load_data_from_local()
|
||||
this.gameDataSyncManager.overrideLocalDataWithRemote(loginResult, "本地调试");
|
||||
}
|
||||
|
||||
setFightHero(position:number,heroId:number,autoSave:boolean=true){
|
||||
this.fight_heros[position] = heroId;
|
||||
if(autoSave){
|
||||
this.updateFightHeros()
|
||||
}
|
||||
}
|
||||
updateFightHeros(){
|
||||
this.gameDataSyncManager.updateFightHeros(this.fight_heros);
|
||||
}
|
||||
resetFightHeros(){
|
||||
this.gameDataSyncManager.resetFightHeros();
|
||||
}
|
||||
getHasHeroUUID(){
|
||||
let heros=this.heros
|
||||
let heros_uuid=[]
|
||||
for(let key in heros){
|
||||
heros_uuid.push(heros[key].uuid)
|
||||
}
|
||||
return heros_uuid
|
||||
}
|
||||
levelUpHero(heroId:number,exp:number,gold:number){
|
||||
let result=this.gameDataSyncManager.levelUpHero(heroId,exp,gold);
|
||||
if(result){
|
||||
this.heros[heroId].lv++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// ==================== 统一的数据操作接口 ====================
|
||||
|
||||
/**
|
||||
* 增加游戏数据属性(统一接口)
|
||||
* @param property 属性名
|
||||
* @param value 增加的值
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
* @returns 操作结果
|
||||
*/
|
||||
async addGameProperty(property: string, value: any, autoSave: boolean = true): Promise<any> {
|
||||
const currentValue = this.data[property] || 0;
|
||||
const newValue = currentValue + value;
|
||||
|
||||
this.data[property] = newValue;
|
||||
this.vmdata.data[property] = newValue;
|
||||
|
||||
console.log(`[SMC]: 增加游戏数据 ${property} = ${value}, 当前值: ${newValue}`);
|
||||
return newValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗游戏数据属性(统一接口)
|
||||
* @param property 属性名
|
||||
* @param value 消耗的值
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
* @returns 是否成功消耗
|
||||
*/
|
||||
spendGold(amount: number, autoSave: boolean = true): boolean {
|
||||
const currentGold = this.getGameProperty("gold", 0);
|
||||
if (currentGold < amount) {
|
||||
console.warn(`[SMC]: 金币不足,当前: ${currentGold}, 需要: ${amount}`);
|
||||
async spendGameProperty(property: string, value: any, autoSave: boolean = true): Promise<boolean> {
|
||||
const currentValue = this.data[property] || 0;
|
||||
if (currentValue < value) {
|
||||
console.warn(`[SMC]: ${property} 不足,当前: ${currentValue}, 需要: ${value}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
this.addGold(-amount, autoSave);
|
||||
const newValue = currentValue - value;
|
||||
this.data[property] = newValue;
|
||||
this.vmdata.data[property] = newValue;
|
||||
console.log(`[SMC]: 消耗游戏数据 ${property} = ${value}, 当前值: ${newValue}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 增加钻石
|
||||
* @param amount 钻石数量
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
addDiamond(amount: number, autoSave: boolean = true) {
|
||||
const currentDiamond = this.getGameProperty("diamond", 0);
|
||||
const newDiamond = Math.max(0, currentDiamond + amount);
|
||||
this.setGameProperty("diamond", newDiamond, autoSave);
|
||||
console.log(`[SMC]: 钻石变更: ${currentDiamond} -> ${newDiamond} (${amount > 0 ? '+' : ''}${amount})`);
|
||||
return newDiamond;
|
||||
}
|
||||
|
||||
/**
|
||||
* 消耗钻石
|
||||
* @param amount 消耗数量
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
* @returns 是否成功消耗
|
||||
*/
|
||||
spendDiamond(amount: number, autoSave: boolean = true): boolean {
|
||||
const currentDiamond = this.getGameProperty("diamond", 0);
|
||||
if (currentDiamond < amount) {
|
||||
console.warn(`[SMC]: 钻石不足,当前: ${currentDiamond}, 需要: ${amount}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
this.addDiamond(-amount, autoSave);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 增加关卡进度
|
||||
* @param amount 增加的关卡数 (默认1)
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
addMission(amount: number = 1, autoSave: boolean = true) {
|
||||
const currentMission = this.getGameProperty("mission", 1);
|
||||
const newMission = currentMission + amount;
|
||||
this.setGameProperty("mission", newMission, autoSave);
|
||||
console.log(`[SMC]: 关卡进度增加: ${currentMission} -> ${newMission} (+${amount})`);
|
||||
// 计算章节和关卡等级
|
||||
// this.vmdata.mission_data.chapter = Math.floor((newMission - 1) / 10) + 1
|
||||
// this.vmdata.mission_data.level = ((newMission - 1) % 10) + 1
|
||||
return newMission;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 增加分数
|
||||
* @param score 分数
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
addScore(score: number, autoSave: boolean = true) {
|
||||
const currentScore = this.getGameProperty("score", 0);
|
||||
const newScore = currentScore + score;
|
||||
this.setGameProperty("score", newScore, autoSave);
|
||||
console.log(`[SMC]: 分数增加: ${currentScore} -> ${newScore} (+${score})`);
|
||||
return newScore;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置游戏数据为默认值
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
resetGameData(autoSave: boolean = true) {
|
||||
this.data = {
|
||||
score: 888,
|
||||
mission: 1,
|
||||
gold: 100,
|
||||
diamond: 100,
|
||||
};
|
||||
|
||||
console.log("[SMC]: 游戏数据已重置为默认值:", this.data);
|
||||
|
||||
if (autoSave) {
|
||||
this.saveGameData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查本地存储状态(调试用)
|
||||
*/
|
||||
checkLocalStorage() {
|
||||
const fightHeros = oops.storage.get("fight_heros");
|
||||
const heros = oops.storage.get("heros");
|
||||
const gameData = oops.storage.get("game_data");
|
||||
|
||||
console.log("[SMC]: 本地存储状态检查:");
|
||||
console.log(" - fight_heros:", fightHeros ? "存在" : "不存在", fightHeros);
|
||||
console.log(" - heros:", heros ? "存在" : "不存在", heros);
|
||||
console.log(" - game_data:", gameData ? "存在" : "不存在", gameData);
|
||||
|
||||
return {
|
||||
hasFightHeros: !!fightHeros,
|
||||
hasHeros: !!heros,
|
||||
hasGameData: !!gameData,
|
||||
isFirstTime: !fightHeros && !heros && !gameData
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export var smc: SingletonModuleComp = ecs.getSingleton(SingletonModuleComp);
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "58714c1c-3ffe-4ad1-959a-82e5dfeb2dc3",
|
||||
|
||||
49
assets/script/game/common/Test.ts
Normal file
49
assets/script/game/common/Test.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { oops } from "db://oops-framework/core/Oops"
|
||||
|
||||
export class Test{
|
||||
load_data_from_local() {
|
||||
let local_data = this.get_local_data()
|
||||
if(!local_data.data||!local_data.fight_heros||!local_data.heros||!local_data.items||!local_data.tals||!local_data.equips){
|
||||
return this.init_local_data()
|
||||
}
|
||||
return local_data
|
||||
}
|
||||
get_local_data(){
|
||||
let local_data ={
|
||||
user_id: "local_debug",
|
||||
openid: "local_debug",
|
||||
regist_time: Date.now(),
|
||||
data: JSON.parse(oops.storage.get("data")),
|
||||
fight_heros: JSON.parse(oops.storage.get("fight_heros")),
|
||||
heros: JSON.parse(oops.storage.get("heros")),
|
||||
items: JSON.parse(oops.storage.get("items")),
|
||||
tals: JSON.parse(oops.storage.get("tals")),
|
||||
equips: JSON.parse(oops.storage.get("equips"))
|
||||
}
|
||||
return local_data
|
||||
}
|
||||
init_local_data(){
|
||||
let init_data = {
|
||||
data : {
|
||||
score: 0, mission: 1, gold: 100, diamond: 100, meat: 0, exp: 0,
|
||||
ghstone: 0, bhstone: 0, phlestone: 0, rhstone: 0, herocard: 0,
|
||||
ckey: 0, skey: 0, gkey: 0,},
|
||||
fight_heros: { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0 },
|
||||
heros: {
|
||||
5001: { uuid: 5001, lv: 1 },
|
||||
5005: { uuid: 5005, lv: 1 },
|
||||
5007: { uuid: 5007, lv: 1 }
|
||||
},
|
||||
items: {},
|
||||
tals: {},
|
||||
equips: {}
|
||||
}
|
||||
oops.storage.set("data", JSON.stringify(init_data.data))
|
||||
oops.storage.set("fight_heros", JSON.stringify(init_data.fight_heros))
|
||||
oops.storage.set("heros", JSON.stringify(init_data.heros))
|
||||
oops.storage.set("items", JSON.stringify(init_data.items))
|
||||
oops.storage.set("tals", JSON.stringify(init_data.tals))
|
||||
oops.storage.set("equips", JSON.stringify(init_data.equips))
|
||||
return this.get_local_data()
|
||||
}
|
||||
}
|
||||
9
assets/script/game/common/Test.ts.meta
Normal file
9
assets/script/game/common/Test.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "893646a7-1cf0-4c80-8fe1-39ac111130fc",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "28d11009-6d68-462a-9880-8b31cf5975fd",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ffda71d7-624d-40de-8c09-712edd44bfdc",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "6f6a81bc-e767-4001-b181-2c18c896cfd0",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "28ac0ad6-53bf-471a-9256-ae7c8ad351a7",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "14663cf6-fb92-4921-8ba9-c836b2667737",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "49217bd4-ee11-49f7-b404-e8d40478fa2d",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bbef9ac6-154b-4e27-9c56-e964e27ef2d5",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "26aee2e6-ab33-4155-a0aa-221c6be8d030",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "167eb23c-6d7e-4e30-96ca-06fec16eeaa8",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0388f0b8-c77d-4020-8b9a-dabf774f6502",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bde4950f-acae-4c3e-a6a7-39248c34613d",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bb894d46-7785-4e72-9314-8e384a338ab3",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "5fdc5c44-f438-4c0e-8c5c-a2673d49aafd",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "91ba5d4e-bef8-4b0d-8c64-7ce0f37e43d2",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "196aaacb-556c-4bb2-925c-9a70dc3e56fc",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "9f62614b-42c3-4f21-a3d6-68c9190082e8",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "b44c446b-ce5f-4079-ac42-269837dbf580",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "d26fa84f-22b4-4136-bb46-d7e978683365",
|
||||
|
||||
Reference in New Issue
Block a user