/** @format */ import {ECSSystem} from '../lib/ECSSystem' import {FightWorld} from '../worlds/FightWorld' import {ComCocosNode} from '../components/ComCocosNode' import {ComTransform} from '../components/ComTransform' import {ComRole} from '../components/ComRole' import {FILTER_CAN_ATTACK_ENEMY} from './SysFindEnemy' import {Data} from '../../GameControl' import {CastleSkillConfig, ICastleSkillConfig} from '../../config/CastleSkillConfig' import {BUILDING} from '../../enums/Enum' export class SysCastleSkill extends ECSSystem { baseX: number = Data.game.fightSizeMinX baseY: number = (Data.game.fightSizeMaxY + Data.game.fightSizeMinY) / 2 cd: number = 0 baseBulletCfg: ICastleSkillConfig curLeftNum: number = 0 rechargeTime: number = 0 autoCastleSkill: boolean = false /** 连接 */ public onAdd(world: FightWorld): void {} /** 断开连接 */ public onRemove(world: FightWorld): void {} /** 添加实体 */ public onEntityEnter(world: FightWorld, entity: number): void {} /** */ public onEntityLeave(world: FightWorld, entity: number): void {} init() { this.baseBulletCfg = CastleSkillConfig[Data.user.useCastleSkillID] if (!this.baseBulletCfg) return this.curLeftNum = this.baseBulletCfg.rechargeNum this.rechargeTime = 0 this.autoCastleSkill = Data.main.buildData?.find(value => value.id == BUILDING.main)?.autoExploreOption } reduceLeftNum() { this.curLeftNum -= 1 if (this.curLeftNum <= 0) { this.rechargeTime = this.baseBulletCfg.rechargeCD } } /** 更新 */ public onUpdate(world: FightWorld, dt: number): void { if (!this.baseBulletCfg) return //充能中 if (this.rechargeTime > 0) { this.rechargeTime -= dt //充能结束 if (this.rechargeTime <= 0) { this.curLeftNum = this.baseBulletCfg.rechargeNum this.rechargeTime = 0 this.cd = 0 } return } if (!this.autoCastleSkill) return if (this.cd > 0) { this.cd -= dt if (this.cd < 0) this.cd = 0 return } let filter = world.getFilter(FILTER_CAN_ATTACK_ENEMY) let targetEntity = 0 let minDistance = Infinity filter.walk((entity: number) => { //寻找最近的存活目标并且加载到场景的敌方单位 let comTransform: ComTransform = world.getComponent(entity, ComTransform) let comCocosNode: ComCocosNode = world.getComponent(entity, ComCocosNode) let comRole: ComRole = world.getComponent(entity, ComRole) if (!comCocosNode?.loaded) return let dx = this.baseX - comTransform.x let dy = this.baseY - comTransform.y let distance = dx * dx + dy * dy if (distance <= minDistance && !world.isBase(comRole)) { minDistance = distance targetEntity = entity } return false }) if (targetEntity) { let comTransform: ComTransform = world.getComponent(targetEntity, ComTransform) world.createBaseBulletEntity(comTransform) this.cd = this.baseBulletCfg.attackCD } } }