- 删除 SingletonModuleComp 中的 meat、exp、monsters 等未使用字段 - 移除 MissionComp 中的 chou_gold 和 lvup_gold 初始化 - 保留核心游戏状态字段,减少内存占用和维护复杂度
418 lines
14 KiB
TypeScript
418 lines
14 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 { WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
|
||
import { GameEvent } from "./config/GameEvent";
|
||
import * as exp from "constants";
|
||
import { HeroAttrsComp } from "../hero/HeroAttrsComp";
|
||
import { Attrs, GameScoreStats } from "./config/HeroAttrs";
|
||
import { count, time } from "console";
|
||
import { getLevelExp } from "../map/RogueConfig";
|
||
import { FightSet } from "./config/GameSet";
|
||
import { mLogger } from "./Logger";
|
||
/**
|
||
* 用远程数据覆盖本地数据(统一方法)
|
||
* @param remoteData 远程数据(云端或本地调试)
|
||
*/
|
||
interface GameDate{
|
||
gold:number,
|
||
heros:any,
|
||
fight_hero:number,
|
||
collection?: {
|
||
talents: Record<number, number>,
|
||
skills: {uuid:0,count:0},
|
||
friend:{uuid:0,count:0},
|
||
}
|
||
}
|
||
interface CloudData {
|
||
openid: string;
|
||
data: GameDate;
|
||
}
|
||
/** 游戏模块 */
|
||
@ecs.register('SingletonModule')
|
||
export class SingletonModuleComp extends ecs.Comp {
|
||
private debugMode: boolean = false;
|
||
|
||
/** 游戏初始化模块 */
|
||
initialize: Initialize = null!;
|
||
/** 游戏地图 */
|
||
map: GameMap = null!;
|
||
openid:string=''
|
||
mission:any={
|
||
status:0, //0:未开始 1:进行中 2:胜利 3:失败
|
||
play:false,
|
||
pause:false,
|
||
in_select:false,
|
||
in_fight:false,
|
||
stop_mon_action:false,
|
||
};
|
||
data:any={
|
||
score:0,
|
||
mission:1,
|
||
diamond:100, //商店购买 及 双倍奖励资源
|
||
task:0,
|
||
noStop:false,
|
||
showInfo:true,
|
||
}
|
||
guides:any=[0,0,0,0,0]
|
||
current_guide:number=0
|
||
fight_hero: number = 5001; // 单个出战英雄
|
||
heros:any= [5001]
|
||
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,//最大关卡
|
||
coin:0,
|
||
time:15*60,//游戏时间
|
||
unlockCoin: 20,
|
||
},
|
||
scores: {
|
||
score: 0, // 基础得分
|
||
// 战斗统计
|
||
crt_count: 0, // 暴击次数
|
||
wf_count: 0, // 风怒次数
|
||
dod_count: 0, // 闪避次数
|
||
back_count: 0, // 击退次数
|
||
stun_count: 0, // 击晕次数
|
||
freeze_count: 0, // 冰冻次数
|
||
|
||
// 伤害统计
|
||
total_dmg: 0, // 总伤害
|
||
atk_count: 0, // 攻击次数
|
||
avg_dmg: 0, // 平均伤害
|
||
thorns_dmg: 0, // 反伤伤害
|
||
crit_dmg_total: 0, // 暴击伤害总额
|
||
|
||
// 生存统计
|
||
heal_total: 0, // 治疗总量
|
||
lifesteal_total: 0, // 吸血总量
|
||
|
||
// 资源统计
|
||
exp_total: 0, // 经验总数
|
||
gold_total: 0, // 金币总数
|
||
|
||
// 击杀统计
|
||
melee_kill_count: 0, // 近战怪击杀数量
|
||
remote_kill_count: 0, // 远程怪击杀数量
|
||
elite_kill_count: 0, // 精英怪击杀数量
|
||
boss_kill_count: 0, // Boss击杀数
|
||
} as GameScoreStats,
|
||
hero:{
|
||
name:'',
|
||
path:'',
|
||
as:0,
|
||
type:0,
|
||
lv:1,
|
||
exp:0,
|
||
exp_max:100,
|
||
exp_pre:0,
|
||
hp:50,
|
||
hp_max:100,
|
||
mp:50,
|
||
mp_max:100,
|
||
def:0,
|
||
ap:0,
|
||
crt:0,
|
||
speed:0,
|
||
skills:[],
|
||
buff:[],
|
||
tal:[],
|
||
info:'',
|
||
},
|
||
// 收集记录
|
||
collection: {
|
||
talents: {} as Record<number, number>,
|
||
skill: {uuid:0,count:0},
|
||
friend:{uuid:0,count:0},
|
||
},
|
||
gold: 0, // 金币数据(MVVM绑定字段)
|
||
};
|
||
|
||
|
||
|
||
// 全局属性加成 {attrIndex: [value, count]}
|
||
global_attrs: Record<number, [number, number]> = {
|
||
[Attrs.AP]: [1, 0], // 攻击力
|
||
[Attrs.HP_MAX]: [100, 100], // 生命上限
|
||
[Attrs.DEF]: [1, 0], // 防御
|
||
[Attrs.SPEED]: [1, 0], // 速度
|
||
[Attrs.CRITICAL]: [1, 0], // 暴击率
|
||
[Attrs.STUN_CHANCE]: [1, 0], // 眩晕率
|
||
[Attrs.WFUNY]: [1, 0], // 风怒率
|
||
};
|
||
|
||
/** 主角实体引用 */
|
||
// role: ecs.Entity | null = null;
|
||
|
||
/**
|
||
* 记录天赋获取
|
||
* @param id 天赋ID
|
||
*/
|
||
addTalentRecord(id: number) {
|
||
if (!this.vmdata.collection.talents[id]) {
|
||
this.vmdata.collection.talents[id] = 0;
|
||
}
|
||
this.vmdata.collection.talents[id]++;
|
||
mLogger.log(this.debugMode, 'SMC', `[SMC] 记录天赋获取: ID=${id}, 次数=${this.vmdata.collection.talents[id]}`);
|
||
oops.message.dispatchEvent(GameEvent.UpdateCollection);
|
||
}
|
||
|
||
/**
|
||
* 记录技能获取
|
||
* @param id 技能ID
|
||
*/
|
||
addSkillRecord(id: number) {
|
||
if (!this.vmdata.collection.skill.uuid) {
|
||
this.vmdata.collection.skill.uuid = id;
|
||
}
|
||
this.vmdata.collection.skill.count++;
|
||
mLogger.log(this.debugMode, 'SMC', `[SMC] 记录技能获取: ID=${id}, 次数=${this.vmdata.collection.skill.count}`);
|
||
oops.message.dispatchEvent(GameEvent.UpdateCollection);
|
||
}
|
||
/**
|
||
* 记录好友获取
|
||
* @param id 好友ID
|
||
*/
|
||
addFriendHero(id: number) {
|
||
if (!this.vmdata.collection.friend.uuid) {
|
||
this.vmdata.collection.friend.uuid = id;
|
||
}
|
||
this.vmdata.collection.friend.count++;
|
||
mLogger.log(this.debugMode, 'SMC', `[SMC] 记录好友获取: ID=${id}, 次数=${this.vmdata.collection.friend.count}`);
|
||
oops.message.dispatchEvent(GameEvent.UpdateCollection);
|
||
}
|
||
|
||
/**
|
||
* 增加经验并处理升级逻辑
|
||
* @param exp 获得的经验值
|
||
*/
|
||
addExp(exp: number) {
|
||
if (exp <= 0) return;
|
||
|
||
const h = this.vmdata.hero;
|
||
// 确保等级至少为1
|
||
if (h.lv < 1) h.lv = 1;
|
||
// 确保经验上限正确
|
||
if (h.exp_max <= 0) h.exp_max = getLevelExp(h.lv);
|
||
|
||
h.exp += exp;
|
||
|
||
// 检查升级
|
||
let isLevelUp = false;
|
||
while (h.exp >= h.exp_max) {
|
||
h.exp -= h.exp_max;
|
||
h.lv++;
|
||
isLevelUp = true;
|
||
|
||
// 更新下一级所需经验
|
||
h.exp_max = getLevelExp(h.lv);
|
||
|
||
mLogger.log(this.debugMode, 'SMC', `[SMC] 升级! Lv.${h.lv - 1} -> Lv.${h.lv}, 下级所需: ${h.exp_max}`);
|
||
}
|
||
h.exp_pre=Math.round(h.exp/h.exp_max*100)
|
||
if (isLevelUp) {
|
||
// 发送升级事件
|
||
oops.message.dispatchEvent(GameEvent.CanUpdateLv, { lv: h.lv });
|
||
}
|
||
}
|
||
|
||
vmAdd() {
|
||
VM.add(this.vmdata, "data");
|
||
// mLogger.log(this.debugMode, 'SMC', "[MissionComp]局内数据初始化",smc.vmdata.mission_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
|
||
//存储到远程服务器 后续再添加
|
||
}
|
||
|
||
updateCloudData(){
|
||
|
||
let gemeDate=this.getGameDate()
|
||
WxCloudApi.save(gemeDate).then((result) => {
|
||
mLogger.log(this.debugMode, 'SMC', '云端保存')
|
||
if(result.result.code === 200) {
|
||
mLogger.log(this.debugMode, 'SMC', "保存成功",result.result)
|
||
return true
|
||
} else {
|
||
mLogger.warn(this.debugMode, 'SMC', `[SMC]: 游戏数据增加失败: ${result.result.msg}`);
|
||
return false
|
||
}
|
||
}).catch((error) => {
|
||
mLogger.error(this.debugMode, 'SMC', `[SMC]: 增加游戏数据异常:`, error);
|
||
return false
|
||
});
|
||
return true
|
||
}
|
||
getCloudData(){
|
||
WxCloudApi.get().then(async (result) => {
|
||
if(result.result.code === 200) {
|
||
let data=result.result.data
|
||
mLogger.log(this.debugMode, 'SMC', `[SMC]: 获取游戏数据成功:`, result.result);
|
||
this.overrideLocalDataWithRemote(data)
|
||
return true
|
||
} else {
|
||
mLogger.warn(this.debugMode, 'SMC', `[SMC]: 游戏数据增加失败`);
|
||
return false
|
||
}
|
||
}).catch((error) => {
|
||
mLogger.error(this.debugMode, 'SMC', `[SMC]: 获取游戏数据异常:`, error);
|
||
});
|
||
}
|
||
public async overrideLocalDataWithRemote(CloudData) {
|
||
try {
|
||
// 直接覆盖基础游戏数据
|
||
if (CloudData.openid) {
|
||
this.openid=CloudData.openid
|
||
}
|
||
// 直接覆盖出战英雄配置
|
||
if (CloudData.data) {
|
||
if(CloudData.data.gold) this.vmdata.gold=CloudData.data.gold
|
||
if(CloudData.data.heros) this.heros=CloudData.data.heros
|
||
if(CloudData.data.fight_hero) this.fight_hero=CloudData.data.fight_hero
|
||
// 恢复收集记录
|
||
if(CloudData.data.collection) {
|
||
this.vmdata.collection = CloudData.data.collection;
|
||
}
|
||
}
|
||
|
||
} catch (error) {
|
||
mLogger.error(this.debugMode, 'SMC', `[SMC]: 数据覆盖失败:`, error);
|
||
}
|
||
}
|
||
getGameDate(){
|
||
return {
|
||
gold:this.vmdata.gold,
|
||
heros:this.heros,
|
||
fight_hero:this.fight_hero,
|
||
collection: this.vmdata.collection
|
||
}
|
||
}
|
||
addHero(hero_uuid:number){
|
||
if(this.heros.indexOf(hero_uuid)==-1){
|
||
this.heros.push(hero_uuid)
|
||
}
|
||
if(this.isWxClient()){
|
||
let res = this.updateCloudData()
|
||
if (res){
|
||
return true
|
||
}else{
|
||
// 同步不成功,删除uuid
|
||
this.heros.splice(this.heros.indexOf(hero_uuid), 1);
|
||
oops.gui.toast("数据同步失败,已回滚操作");
|
||
return false
|
||
}
|
||
}
|
||
|
||
return true
|
||
}
|
||
// 设置单个出战英雄
|
||
updateFihgtHero(heroId: number) {
|
||
this.fight_hero = heroId;
|
||
if(this.isWxClient()){
|
||
let res = this.updateCloudData()
|
||
if (res){
|
||
return true
|
||
}else{
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
updateGold(gold:number, is_sync: boolean = true){
|
||
this.vmdata.gold += gold;
|
||
if(this.isWxClient() && is_sync){
|
||
let res = this.updateCloudData()
|
||
if (res){
|
||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||
return true
|
||
}else{
|
||
this.vmdata.gold -= gold
|
||
return false
|
||
}
|
||
}
|
||
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
|
||
return true
|
||
}
|
||
|
||
/**
|
||
* 更新主角英雄数据到 VM
|
||
* @param heroAttrs 英雄属性组件
|
||
*/
|
||
updateHeroInfo(heroAttrs: HeroAttrsComp) {
|
||
if (!heroAttrs || !heroAttrs.is_master) return;
|
||
|
||
const h = this.vmdata.hero;
|
||
|
||
// 基础信息
|
||
h.name = heroAttrs.hero_name;
|
||
h.type = heroAttrs.type;
|
||
|
||
// 防止 ECS 旧数据覆盖 VM 新数据 (如果 VM 里的等级更高,说明刚升级还没同步到 ECS)
|
||
if (heroAttrs.lv > h.lv) {
|
||
h.lv = heroAttrs.lv;
|
||
} else if (h.lv > heroAttrs.lv) {
|
||
// 此时应该反向同步?或者等待 CanUpdateLv 事件处理
|
||
// 这里暂时保持 VM 的高等级,不被 ECS 覆盖
|
||
} else {
|
||
h.lv = heroAttrs.lv;
|
||
}
|
||
|
||
// 动态属性
|
||
h.hp = Math.floor(heroAttrs.hp);
|
||
h.mp = Math.floor(heroAttrs.mp);
|
||
|
||
// 计算属性
|
||
h.hp_max = Math.floor(heroAttrs.Attrs[Attrs.HP_MAX] || 0);
|
||
h.mp_max = Math.floor(heroAttrs.Attrs[Attrs.MP_MAX] || 0);
|
||
h.def = Math.floor(heroAttrs.Attrs[Attrs.DEF] || 0);
|
||
h.ap = Math.floor(heroAttrs.Attrs[Attrs.AP] || 0);
|
||
h.speed = Math.floor(heroAttrs.Attrs[Attrs.SPEED] || 0);
|
||
h.crt = Math.floor(heroAttrs.Attrs[Attrs.CRITICAL] || 0);
|
||
h.as = Math.floor(heroAttrs.Attrs[Attrs.AS] || 0);
|
||
|
||
// 强制触发 VM 更新
|
||
// 如果 VM 监听的是 smc.vmdata.hero 的属性变化,上面的赋值应该有效。
|
||
// 但如果 UI 绑定的是 hero 整体对象,或者因为深层监听问题,可能需要手动通知。
|
||
// 为了保险,我们可以重新赋值一次(如果是对象引用可能不会触发),或者使用 VM 提供的 set 方法
|
||
// 这里尝试直接重新赋值整个对象属性来触发更新,或者假设 VM 已经处理好了深层监听。
|
||
// 如果 UI 没变,可能是 VM 没有检测到深层属性变化。
|
||
|
||
// 尝试手动通知或重新赋值关键路径
|
||
// 注意:Oops Framework 的 VM 通常支持对象属性修改的监听,前提是初始化时已经建立了监听。
|
||
// 这里我们尝试显式调用 VM.modify 来通知更新(如果有这个 API),或者重新赋值给 vmdata
|
||
|
||
// 方案:重新设置 vmdata.hero 来触发根节点的更新通知
|
||
this.vmdata.hero = h;
|
||
}
|
||
error(){
|
||
oops.gui.toast("数据处理异常,请重试或重新登录")
|
||
}
|
||
|
||
}
|
||
|
||
export var smc: SingletonModuleComp = ecs.getSingleton(SingletonModuleComp); |