Files
heros/assets/script/game/common/SingletonModuleComp.ts
2025-08-19 19:40:34 +08:00

242 lines
7.7 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 { 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";
// import { data } from "../data/data";
/** 游戏模块 */
@ecs.register('SingletonModule')
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,
pause:false,
in_select:false,
in_fight:false,
};
data:any={
score:0,
mission:1,
gold:100, //升级主要资源
diamond:100, //商店购买 及 双倍奖励资源
meat:0,
exp: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},
};
items:any={
}
tals:any={
}
equips:any={
}
monsters:any = [];
sk_info:any = []
monsters_dead:any = []
heros_dead:any = []
enhancements:any=[]
vmdata: any = {
game_over:false,
game_pause:false,
data:{
score:0,
mission:1,
gold:100, //升级主要资源
diamond:100, //商店购买 及 双倍奖励资源
meat:0,
exp:0,
},
mission_data:{
mon_num:0,//怪物数量
hero_num:0,//英雄数量
wave_time_num:0,//波次时间
in_fight:false,
fight_time:0,//战斗时间
level:1,//关卡等级
max_mission:4,//最大关卡
},
reward:{
score:0,
mission:0,
gold:0, //升级主要资源
diamond:0, //商店购买 及 双倍奖励资源
meat:0,
exp:0, //升级经验
}
};
vmAdd() {
VM.add(this.vmdata, "data");
}
reset() {
for (var key in this.vmdata) {
delete this.vmdata[key];
}
}
// ==================== 数据管理方法 ====================
/**
* 同步数据到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(this.isWxClient()){
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){
if(this.isWxClient()){
let result=this.gameDataSyncManager.levelUpHero(heroId,exp,gold);
if(result){
this.heros[heroId].lv++;
return true
}
return false
}
else{
this.heros[heroId].lv++;
return true
}
}
// ==================== 统一的数据操作接口 ====================
/**
* 增加游戏数据属性(统一接口)
* @param property 属性名
* property list:
* ***gold:金币
* ***diamond:钻石
* ***meat:肉
* ***exp:经验
* ***score:分数
* ***mission:关卡
* @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;
}
/**
* 消耗游戏数据属性(统一接口)
* - 支持单个字段spendGameProperty('gold', 10)
* - 支持多个字段spendGameProperty({ gold: 10, exp: 5 })
* 只有当所有字段都满足扣除条件时,才会一次性扣减
* @param property 属性名或属性映射
* @param value 消耗的值(当 property 为字符串时有效)
* @param autoSave 是否自动保存 (默认true)
* @returns 是否成功消耗
*/
async spendGameProperty(property: string | Record<string, number>, value: any = undefined, autoSave: boolean = true): Promise<boolean> {
// 单字段扣除
if (typeof property === 'string') {
const currentValue = this.data[property] || 0;
if (currentValue < value) {
console.warn(`[SMC]: ${property} 不足,当前: ${currentValue}, 需要: ${value}`);
return false;
}
const newValue = currentValue - value;
this.data[property] = newValue;
this.vmdata.data[property] = newValue;
console.log(`[SMC]: 消耗游戏数据 ${property} = ${value}, 当前值: ${newValue}`);
return true;
}
// 多字段扣除(原子性:全部满足才扣)
const deductions = property as Record<string, number>;
// 1) 校验是否全部满足
for (const key in deductions) {
if (!Object.prototype.hasOwnProperty.call(deductions, key)) continue;
const need = deductions[key] ?? 0;
const current = this.data[key] || 0;
if (current < need) {
console.warn(`[SMC]: ${key} 不足,当前: ${current}, 需要: ${need}`);
return false;
}
}
// 2) 统一扣减
for (const key in deductions) {
if (!Object.prototype.hasOwnProperty.call(deductions, key)) continue;
const need = deductions[key] ?? 0;
const current = this.data[key] || 0;
const next = current - need;
this.data[key] = next;
this.vmdata.data[key] = next;
console.log(`[SMC]: 消耗游戏数据 ${key} = ${need}, 当前值: ${next}`);
}
return true;
}
addItem(item_uuid:number,count:number,autoSave:boolean=true){
if(this.isWxClient()){
this.gameDataSyncManager.addItem(item_uuid,count);
}
else{
this.items[item_uuid] = (this.items[item_uuid] || 0) + count;
}
}
}
export var smc: SingletonModuleComp = ecs.getSingleton(SingletonModuleComp);