本地数据存取 完成
This commit is contained in:
@@ -3,6 +3,7 @@ import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ec
|
||||
import { Initialize } from "../initialize/Initialize";
|
||||
import { GameMap } from "../map/GameMap";
|
||||
import { HeroUI, MissionData, VmInfo } from "./config/Mission";
|
||||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||||
|
||||
|
||||
// import { Role } from "../role/Role";
|
||||
@@ -19,8 +20,14 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
play:false,
|
||||
pause:false,
|
||||
in_select:false,
|
||||
score:888,
|
||||
in_fight:false,
|
||||
};
|
||||
data:any={
|
||||
score:0,
|
||||
mission:1,
|
||||
gold:100,
|
||||
diamond:100,
|
||||
}
|
||||
fight_heros:any={
|
||||
0:5001,
|
||||
1:5005,
|
||||
@@ -33,6 +40,7 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
5005:{lv:1},
|
||||
5007:{lv:1},
|
||||
};
|
||||
|
||||
monsters:any = [];
|
||||
sk_info:any = []
|
||||
monsters_dead:any = []
|
||||
@@ -55,6 +63,466 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
delete this.vmdata[key];
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 本地存储管理方法 ====================
|
||||
|
||||
/**
|
||||
* 初始化默认游戏数据
|
||||
*/
|
||||
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}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取英雄等级
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取游戏数据属性
|
||||
* @param property 属性名
|
||||
* @param defaultValue 默认值
|
||||
*/
|
||||
getGameProperty(property: string, defaultValue: any = null): any {
|
||||
return this.data[property] !== undefined ? this.data[property] : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加金币
|
||||
* @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 消耗数量
|
||||
* @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}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
this.addGold(-amount, autoSave);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取金币数量
|
||||
*/
|
||||
getGold(): number {
|
||||
return this.getGameProperty("gold", 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加钻石
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取钻石数量
|
||||
*/
|
||||
getDiamond(): number {
|
||||
return this.getGameProperty("diamond", 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置关卡进度
|
||||
* @param mission 关卡号
|
||||
* @param autoSave 是否自动保存 (默认true)
|
||||
*/
|
||||
setMission(mission: number, autoSave: boolean = true) {
|
||||
const currentMission = this.getGameProperty("mission", 1);
|
||||
if (mission > currentMission) {
|
||||
this.setGameProperty("mission", mission, autoSave);
|
||||
console.log(`[SMC]: 关卡进度更新: ${currentMission} -> ${mission}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前关卡进度
|
||||
*/
|
||||
getMission(): number {
|
||||
return this.getGameProperty("mission", 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加分数
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前分数
|
||||
*/
|
||||
getScore(): number {
|
||||
return this.getGameProperty("score", 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置游戏数据为默认值
|
||||
* @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);
|
||||
@@ -90,7 +90,9 @@ export enum FightSet {
|
||||
export const MissionData = {
|
||||
gold:1000,//金币
|
||||
score:0,//分数
|
||||
diamond:0,//钻石
|
||||
current_wave:1,
|
||||
mission:1,//关卡
|
||||
mon_num:0,//怪物数量
|
||||
wave_time_num:0,//波次时间
|
||||
in_fight:false,
|
||||
|
||||
Reference in New Issue
Block a user