import { HeroViewComp } from "../../../hero/HeroViewComp"; import { BattleMoveComp } from "./BattleMoveComp"; import { ecs } from "../../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS"; import { smc } from "../../SingletonModuleComp"; @ecs.register('BattleMoveSystem') export class BattleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate { filter(): ecs.IMatcher { return ecs.allOf(BattleMoveComp, HeroViewComp); } update(e: ecs.Entity) { if(!smc.mission.play||smc.mission.pause) return const move = e.get(BattleMoveComp); const view = e.get(HeroViewComp); if (!move.moving) return; // 检测攻击范围内是否有敌人 const shouldStop = this.checkEnemiesInRange(e, view.dis); view.is_atking = shouldStop; // 同步攻击状态 if (!shouldStop) { if(view.is_stop||view.is_dead) return //停止移动或者死亡不移动 // 计算移动量 const delta = view.speed * this.dt * move.direction; const newX = view.node.position.x + delta; view.status_change("move") // 限制移动范围 if (this.validatePosition(newX, move)) { view.node.setPosition(newX, view.node.position.y, 0); } } else{ view.status_change("idle") } // console.log(`[${view.hero_name}] 类型:${view.type} 是否停止:${shouldStop} 方向:${move.direction} 位置:${view.node.position.x.toFixed(1)}`); } /** 验证目标位置有效性 */ private validatePosition(newX: number, move: BattleMoveComp): boolean { // 我方不能超过右边界,敌方不能超过左边界 return move.direction === 1 ? newX <= move.targetX : newX >= move.targetX; } /** 检测攻击范围内敌人 */ private checkEnemiesInRange(entity: ecs.Entity, range: number): boolean { const currentPos = entity.get(HeroViewComp).node.position; const team = entity.get(HeroViewComp).fac; const view = entity.get(HeroViewComp); const isMelee = view.type === 0; return ecs.query(ecs.allOf(HeroViewComp)).some(e => { const view = e.get(HeroViewComp); const distance = Math.abs(currentPos.x - view.node.position.x); if (isMelee) { // 近战需要满足:在攻击范围内且距离<=75才停止 const inAttackRange = distance <= range; const closeEnough = distance <= 75; // 近战停止距离 return view.fac !== team && inAttackRange && closeEnough; } else { // 远程/辅助:在攻击范围内即停止 return view.fac !== team && distance <= range; } }); } }