/** @format */ import {ComRole} from '../components/ComRole' import {ECSSystem} from '../lib/ECSSystem' import {GenFilterKey} from '../lib/ECSComponent' import {FightWorld} from '../worlds/FightWorld' import {ComCocosNode} from '../components/ComCocosNode' import {ComTransform} from '../components/ComTransform' import {ComFrameAni} from '../components/ComFrameAni' import {ComBullet} from '../components/ComBullet' const FILTER_FRAME_ANI = GenFilterKey([ComFrameAni, ComTransform, ComCocosNode]) export class SysFrameAni extends ECSSystem { /** 连接 */ public onAdd(world: FightWorld): void {} /** 断开连接 */ public onRemove(world: FightWorld): void {} /** 添加实体 */ public onEntityEnter(world: FightWorld, entity: number): void {} /** */ public onEntityLeave(world: FightWorld, entity: number): void {} /** 更新 */ public onUpdate(world: FightWorld, dt: number): void { let filter = world.getFilter(FILTER_FRAME_ANI) //持续技能 filter.walk((entity: number) => { let comCocosNode = world.getComponent(entity, ComCocosNode) if (!comCocosNode.loaded) return let comFrameAni = world.getComponent(entity, ComFrameAni) let targetRole = world.getComponent(comFrameAni.role, ComRole) let comTransform = world.getComponent(entity, ComTransform) if (!comFrameAni.dirty) return let removeFrameAniEntity = () => { //console.log('技能结束', entity) world.removeNodeEntity(entity) } if (comFrameAni.role && world.isDie(targetRole)) { removeFrameAniEntity() return } if (targetRole) { let targetComTransform = world.getComponent(comFrameAni.role, ComTransform) comTransform.x = targetComTransform.x + comFrameAni.aniConfig.offsetPos[1] let halfHeightScale = comFrameAni.aniConfig.offsetPos[0] if (halfHeightScale < 1) halfHeightScale = 1 comTransform.y = targetComTransform.y + targetComTransform.halfHeight * (halfHeightScale - 1) + comFrameAni.aniConfig.offsetPos[2] } let lineEndRole = world.getComponent(comFrameAni.lineEndEntity, ComRole) let lineEndBullet = world.getComponent(comFrameAni.lineEndEntity, ComBullet) if (!world.isDie(lineEndRole) || lineEndBullet) { let startPos if (comFrameAni.lineStartPos) { startPos = comFrameAni.lineStartPos.clone() } let lineStartComTransform = world.getComponent(comFrameAni.lineStartEntity, ComTransform) let lineStartRole = world.getComponent(comFrameAni.lineStartEntity, ComRole) if (!world.isDie(lineStartRole) && lineStartComTransform) { startPos = cc.v2(lineStartComTransform.x, lineStartComTransform.y) } let lineEndComTransform = world.getComponent(comFrameAni.lineEndEntity, ComTransform) if (startPos && lineEndComTransform) { let endPos = cc.v2( lineEndComTransform.x, lineEndComTransform.y + (!world.isDie(lineEndRole) ? lineEndComTransform.halfHeight : 0), ) comTransform.x = (startPos.x + endPos.x) / 2 comTransform.y = (startPos.y + endPos.y) / 2 let dirPosTmp = endPos.sub(startPos) //弧度转角度 comCocosNode.node.angle = cc.misc.radiansToDegrees(Math.atan2(dirPosTmp.y, dirPosTmp.x)) comCocosNode.node.scaleX = dirPosTmp.mag() / comCocosNode.node.width } } //弹射子弹弹射途中目标死亡,子弹被移除,当前特效也要移除 if (comFrameAni.lineEndEntity && !lineEndRole && !lineEndBullet) { removeFrameAniEntity() } comFrameAni.countDown -= dt if (comFrameAni.countDown <= 0) { removeFrameAniEntity() } return false }) } }