import { oops } from "db://oops-framework/core/Oops"; import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS"; import { BattleState } from "./BattleStateComp"; import { smc } from "../common/SingletonModuleComp"; import { GameEvent } from "../common/config/GameEvent"; import { DamageResult } from "../damage/DamageComp"; @ecs.register('BattleManager') export class BattleManager extends ecs.Entity { private static _instance: BattleManager; static get instance(): BattleManager { if (!this._instance) { this._instance = new BattleManager(); this._instance.add(BattleState); } return this._instance; } /** 外部调用入口:开始新战斗 */ startBattle(missionId: number) { this.get(BattleState).isEnded = false; // 加载关卡配置、生成敌人等 smc.mission.load(missionId); } /** 外部调用入口:结束战斗 */ endBattle() { this.get(BattleState).isEnded = true; } private initEvents() { oops.message.on("HeroDead", this.onHeroDead, this); oops.message.on("AllEnemyDead", this.onAllEnemyDead, this); oops.message.on(GameEvent.MissionEnd, this.endBattle, this); oops.message.on("BattleEndCheck", this.onBattleEndCheck, this); } private onHeroDead() { this.endBattle(); } private onAllEnemyDead() { this.endBattle(); } private onBattleEndCheck() { // 留空或添加实际需要的检测逻辑 } } @ecs.register('BattleManagerComp') export class BattleManagerComp extends ecs.Comp { reset() { // 初始化战斗状态 } } export class BattleManagerSystem extends ecs.ComblockSystem { filter(): ecs.IMatcher { return ecs.allOf(BattleManagerComp); } update(e: ecs.Entity) { const state = e.get(BattleState); if (state.isEnded) { // 处理战斗结束逻辑 this.onBattleEnd(e); } } private onBattleEnd(entity: ecs.Entity) { // 发放奖励、保存进度等 const state = entity.get(BattleState); // 清理所有残留伤害组件 ecs.query(ecs.allOf(DamageResult)).forEach(entity => { entity.remove(DamageResult); }); // 重置战斗状态 state.isEnded = false; entity.destroy(); } }