318 lines
9.8 KiB
TypeScript
318 lines
9.8 KiB
TypeScript
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 { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||
import { GameData, WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
|
||
import { gameDataSyncManager } from "./GameDataSyncManager";
|
||
import { Test } from "./Test";
|
||
import { GameEvent } from "./config/GameEvent";
|
||
|
||
|
||
// 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,
|
||
};
|
||
guides:any=[0,0,0,0,0]
|
||
current_guide:number=0
|
||
data:any={
|
||
score:0,
|
||
mission:1,
|
||
gold:100, //升级主要资源
|
||
diamond:100, //商店购买 及 双倍奖励资源
|
||
meat:0,
|
||
exp:0,
|
||
task:0,
|
||
}
|
||
shop:any={
|
||
daily:[1001,1004,1002,1005],
|
||
weekly:[],
|
||
monthly:[],
|
||
special:[],
|
||
goods_count:[1,1,3,3,10,10,10,10,10,10,10,10],
|
||
|
||
}
|
||
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},
|
||
};
|
||
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,
|
||
mission_data:{
|
||
mon_num:0,//怪物数量
|
||
hero_num:0,//英雄数量
|
||
wave_time_num:0,//波次时间
|
||
in_fight:false,
|
||
fight_time:0,//战斗时间
|
||
level:1,//关卡等级
|
||
max_mission:4,//最大关卡
|
||
},
|
||
};
|
||
vmAdd() {
|
||
VM.add(this.vmdata, "data");
|
||
}
|
||
reset() {
|
||
for (var key in this.vmdata) {
|
||
delete this.vmdata[key];
|
||
}
|
||
}
|
||
|
||
// ==================== 数据管理方法 ====================
|
||
|
||
/**
|
||
* 判断是否为微信客户端
|
||
*/
|
||
private isWxClient(): boolean {
|
||
// 检查是否存在微信API
|
||
return typeof wx !== 'undefined' && typeof (wx as any).getSystemInfoSync === 'function';
|
||
}
|
||
|
||
finishGuide(index:number){
|
||
smc.guides[index]=1
|
||
this.syncGuide()
|
||
}
|
||
|
||
syncGuide(){
|
||
//存储到远程服务器 后续再添加
|
||
}
|
||
//调试用
|
||
syncDataFromLocal(){
|
||
if(this.isWxClient()) return
|
||
const loginResult = new Test().load_data_from_local()
|
||
this.gameDataSyncManager.overrideLocalDataWithRemote(loginResult, "本地调试");
|
||
}
|
||
|
||
addHero(hero_uuid:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.addHero(hero_uuid)){
|
||
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
|
||
return true
|
||
}
|
||
|
||
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){
|
||
if(this.isWxClient()){
|
||
let result=this.gameDataSyncManager.levelUpHero(heroId);
|
||
if(result){
|
||
this.heros[heroId].lv++;
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
else{
|
||
this.heros[heroId].lv++;
|
||
return true
|
||
}
|
||
}
|
||
|
||
error(){
|
||
oops.gui.toast("数据处理异常,请重试或重新登录")
|
||
}
|
||
addExp(exp:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.addGameProperty("exp",exp)){
|
||
this.data.exp+=exp
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.data.exp+=exp
|
||
return true
|
||
}
|
||
addGold(gold:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.addGameProperty("gold",gold)){
|
||
this.data.gold+=gold
|
||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||
return true
|
||
}
|
||
this.error()
|
||
return false
|
||
}
|
||
this.data.gold+=gold
|
||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||
return true
|
||
}
|
||
|
||
addDiamond(diamond:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.addGameProperty("diamond",diamond)){
|
||
this.data.diamond+=diamond
|
||
oops.message.dispatchEvent(GameEvent.DIAMOND_UPDATE)
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.data.diamond+=diamond
|
||
oops.message.dispatchEvent(GameEvent.DIAMOND_UPDATE)
|
||
return true
|
||
}
|
||
|
||
addMission(mission:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.addGameProperty("mission",mission)){
|
||
this.data.mission+=mission
|
||
oops.message.dispatchEvent(GameEvent.MISSION_UPDATE)
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.data.mission+=mission
|
||
oops.message.dispatchEvent(GameEvent.MISSION_UPDATE)
|
||
return true
|
||
}
|
||
|
||
spendExp(exp:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.spendGameProperty("exp",exp)){
|
||
this.data.exp-=exp
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.data.exp-=exp
|
||
return true
|
||
}
|
||
spendGold(gold:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.spendGameProperty("gold",gold)){
|
||
this.data.gold-=gold
|
||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.data.gold-=gold
|
||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||
return true
|
||
}
|
||
spendDiamond(diamond:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.spendGameProperty("diamond",diamond)){
|
||
this.data.diamond-=diamond
|
||
oops.message.dispatchEvent(GameEvent.DIAMOND_UPDATE)
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.data.diamond-=diamond
|
||
oops.message.dispatchEvent(GameEvent.DIAMOND_UPDATE)
|
||
return true
|
||
}
|
||
|
||
/**
|
||
* 消耗游戏数据属性(统一接口)
|
||
* 处理多个字段:spendGameProperty({ gold: 10, exp: 5 })
|
||
*/
|
||
async spendGameProperty(property: Record<string, number>, autoSave: boolean = true): Promise<boolean> {
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.spendGameProperty(property)){
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
// 多字段扣除(原子性:全部满足才扣)
|
||
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}`);
|
||
oops.gui.toast(`${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;
|
||
// console.log(`[SMC]: 消耗游戏数据 ${key} = ${need}, 当前值: ${next}`);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
addItem(item_uuid:number,count:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.addItem(item_uuid,count)){
|
||
this.items[item_uuid] = (this.items[item_uuid] || 0) + count;
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.items[item_uuid] = (this.items[item_uuid] || 0) + count;
|
||
return true
|
||
}
|
||
spendItem(item_uuid:number,count:number,autoSave:boolean=true){
|
||
if(this.isWxClient()){
|
||
if(this.gameDataSyncManager.consumeItem(item_uuid,count)){
|
||
this.items[item_uuid] = (this.items[item_uuid] || 0) - count;
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
this.items[item_uuid] = (this.items[item_uuid] || 0) - count;
|
||
return true
|
||
}
|
||
|
||
|
||
}
|
||
|
||
export var smc: SingletonModuleComp = ecs.getSingleton(SingletonModuleComp); |