SysFrameAni.ts 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /** @format */
  2. import {ComRole} from '../components/ComRole'
  3. import {ECSSystem} from '../lib/ECSSystem'
  4. import {GenFilterKey} from '../lib/ECSComponent'
  5. import {FightWorld} from '../worlds/FightWorld'
  6. import {ComCocosNode} from '../components/ComCocosNode'
  7. import {ComTransform} from '../components/ComTransform'
  8. import {ComFrameAni} from '../components/ComFrameAni'
  9. import {ComBullet} from '../components/ComBullet'
  10. const FILTER_FRAME_ANI = GenFilterKey([ComFrameAni, ComTransform, ComCocosNode])
  11. export class SysFrameAni extends ECSSystem {
  12. /** 连接 */
  13. public onAdd(world: FightWorld): void {}
  14. /** 断开连接 */
  15. public onRemove(world: FightWorld): void {}
  16. /** 添加实体 */
  17. public onEntityEnter(world: FightWorld, entity: number): void {}
  18. /** */
  19. public onEntityLeave(world: FightWorld, entity: number): void {}
  20. /** 更新 */
  21. public onUpdate(world: FightWorld, dt: number): void {
  22. let filter = world.getFilter(FILTER_FRAME_ANI)
  23. //持续技能
  24. filter.walk((entity: number) => {
  25. let comCocosNode = world.getComponent(entity, ComCocosNode)
  26. if (!comCocosNode.loaded) return
  27. let comFrameAni = world.getComponent(entity, ComFrameAni)
  28. let targetRole = world.getComponent(comFrameAni.role, ComRole)
  29. let comTransform = world.getComponent(entity, ComTransform)
  30. if (!comFrameAni.dirty) return
  31. let removeFrameAniEntity = () => {
  32. //console.log('技能结束', entity)
  33. world.removeNodeEntity(entity)
  34. }
  35. if (comFrameAni.role && world.isDie(targetRole)) {
  36. removeFrameAniEntity()
  37. return
  38. }
  39. if (targetRole) {
  40. let targetComTransform = world.getComponent(comFrameAni.role, ComTransform)
  41. comTransform.x = targetComTransform.x + comFrameAni.aniConfig.offsetPos[1]
  42. let halfHeightScale = comFrameAni.aniConfig.offsetPos[0]
  43. if (halfHeightScale < 1) halfHeightScale = 1
  44. comTransform.y =
  45. targetComTransform.y +
  46. targetComTransform.halfHeight * (halfHeightScale - 1) +
  47. comFrameAni.aniConfig.offsetPos[2]
  48. }
  49. let lineEndRole = world.getComponent(comFrameAni.lineEndEntity, ComRole)
  50. let lineEndBullet = world.getComponent(comFrameAni.lineEndEntity, ComBullet)
  51. if (!world.isDie(lineEndRole) || lineEndBullet) {
  52. let startPos
  53. if (comFrameAni.lineStartPos) {
  54. startPos = comFrameAni.lineStartPos.clone()
  55. }
  56. let lineStartComTransform = world.getComponent(comFrameAni.lineStartEntity, ComTransform)
  57. let lineStartRole = world.getComponent(comFrameAni.lineStartEntity, ComRole)
  58. if (!world.isDie(lineStartRole) && lineStartComTransform) {
  59. startPos = cc.v2(lineStartComTransform.x, lineStartComTransform.y)
  60. }
  61. let lineEndComTransform = world.getComponent(comFrameAni.lineEndEntity, ComTransform)
  62. if (startPos && lineEndComTransform) {
  63. let endPos = cc.v2(
  64. lineEndComTransform.x,
  65. lineEndComTransform.y + (!world.isDie(lineEndRole) ? lineEndComTransform.halfHeight : 0),
  66. )
  67. comTransform.x = (startPos.x + endPos.x) / 2
  68. comTransform.y = (startPos.y + endPos.y) / 2
  69. let dirPosTmp = endPos.sub(startPos)
  70. //弧度转角度
  71. comCocosNode.node.angle = cc.misc.radiansToDegrees(Math.atan2(dirPosTmp.y, dirPosTmp.x))
  72. comCocosNode.node.scaleX = dirPosTmp.mag() / comCocosNode.node.width
  73. }
  74. }
  75. //弹射子弹弹射途中目标死亡,子弹被移除,当前特效也要移除
  76. if (comFrameAni.lineEndEntity && !lineEndRole && !lineEndBullet) {
  77. removeFrameAniEntity()
  78. }
  79. comFrameAni.countDown -= dt
  80. if (comFrameAni.countDown <= 0) {
  81. removeFrameAniEntity()
  82. }
  83. return false
  84. })
  85. }
  86. }