Files
heros/assets/script/game/common/SingletonModuleComp.ts
2025-08-14 17:10:36 +08:00

590 lines
18 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { VM } from "../../../../extensions/oops-plugin-framework/assets/libs/model-view/ViewModel";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
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 { Role } from "../role/Role";
// import { data } from "../data/data";
/** 游戏模块 */
@ecs.register('SingletonModule')
export class SingletonModuleComp extends ecs.Comp {
/** 游戏初始化模块 */
initialize: Initialize = null!;
/** 游戏地图 */
map: GameMap = null!;
mission:any={
status:0, //0:未开始 1:进行中 2:胜利 3:失败
play:false,
pause:false,
in_select:false,
in_fight:false,
};
data:any={
score:0,
mission:1,
gold:100,
diamond:100,
meat:0,
}
fight_heros:any={
0:5001,
1:5005,
2:0,
3:0,
4:0,
}
heros:any = {
5001:{uuid:5001,lv:1,slv:0},
5005:{uuid:5005,lv:1,slv:0},
5007:{uuid:5007,lv:1,slv:0},
};
monsters:any = [];
sk_info:any = []
monsters_dead:any = []
heros_dead:any = []
enhancements:any=[]
vmdata: any = {
game_over:false,
game_pause:false,
mission_data:{
gold:1000,//金币
score:0,//分数
diamond:0,//钻石
mission:1,//关卡
chapter:1,//章节
level:1,//关卡等级
max_mission:10,//最大关卡
meat:0,//肉
mon_num:0,//怪物数量
hero_num:0,//英雄数量
wave_time_num:0,//波次时间
in_fight:false,
fight_time:0,//战斗时间
},
reward:{
gold:0,
meat:0,
diamond:0,
score:0,
exp:0,
gcard:0,
bcard:0,
pcard:0,
ycard:0,
}
};
vmAdd() {
VM.add(this.vmdata, "data");
}
reset() {
for (var key in this.vmdata) {
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 英雄ID0表示移除
* @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();
}
}
/**
* 获取游戏数据属性
* @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;
}
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)
// 计算章节和关卡等级
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)
* @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}`);
}
}
/**
* 增加关卡进度
* @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;
}
/**
* 获取当前关卡进度
*/
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);