SysCastleSkill.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /** @format */
  2. import {ECSSystem} from '../lib/ECSSystem'
  3. import {FightWorld} from '../worlds/FightWorld'
  4. import {ComCocosNode} from '../components/ComCocosNode'
  5. import {ComTransform} from '../components/ComTransform'
  6. import {ComRole} from '../components/ComRole'
  7. import {FILTER_CAN_ATTACK_ENEMY} from './SysFindEnemy'
  8. import {Data} from '../../GameControl'
  9. import {CastleSkillConfig, ICastleSkillConfig} from '../../config/CastleSkillConfig'
  10. import {BUILDING} from '../../enums/Enum'
  11. export class SysCastleSkill extends ECSSystem {
  12. baseX: number = Data.game.fightSizeMinX
  13. baseY: number = (Data.game.fightSizeMaxY + Data.game.fightSizeMinY) / 2
  14. cd: number = 0
  15. baseBulletCfg: ICastleSkillConfig
  16. curLeftNum: number = 0
  17. rechargeTime: number = 0
  18. autoCastleSkill: boolean = false
  19. /** 连接 */
  20. public onAdd(world: FightWorld): void {}
  21. /** 断开连接 */
  22. public onRemove(world: FightWorld): void {}
  23. /** 添加实体 */
  24. public onEntityEnter(world: FightWorld, entity: number): void {}
  25. /** */
  26. public onEntityLeave(world: FightWorld, entity: number): void {}
  27. init() {
  28. this.baseBulletCfg = CastleSkillConfig[Data.user.useCastleSkillID]
  29. if (!this.baseBulletCfg) return
  30. this.curLeftNum = this.baseBulletCfg.rechargeNum
  31. this.rechargeTime = 0
  32. this.autoCastleSkill = Data.main.buildData?.find(value => value.id == BUILDING.main)?.autoExploreOption
  33. }
  34. reduceLeftNum() {
  35. this.curLeftNum -= 1
  36. if (this.curLeftNum <= 0) {
  37. this.rechargeTime = this.baseBulletCfg.rechargeCD
  38. }
  39. }
  40. /** 更新 */
  41. public onUpdate(world: FightWorld, dt: number): void {
  42. if (!this.baseBulletCfg) return
  43. //充能中
  44. if (this.rechargeTime > 0) {
  45. this.rechargeTime -= dt
  46. //充能结束
  47. if (this.rechargeTime <= 0) {
  48. this.curLeftNum = this.baseBulletCfg.rechargeNum
  49. this.rechargeTime = 0
  50. this.cd = 0
  51. }
  52. return
  53. }
  54. if (!this.autoCastleSkill) return
  55. if (this.cd > 0) {
  56. this.cd -= dt
  57. if (this.cd < 0) this.cd = 0
  58. return
  59. }
  60. let filter = world.getFilter(FILTER_CAN_ATTACK_ENEMY)
  61. let targetEntity = 0
  62. let minDistance = Infinity
  63. filter.walk((entity: number) => {
  64. //寻找最近的存活目标并且加载到场景的敌方单位
  65. let comTransform: ComTransform = world.getComponent(entity, ComTransform)
  66. let comCocosNode: ComCocosNode = world.getComponent(entity, ComCocosNode)
  67. let comRole: ComRole = world.getComponent(entity, ComRole)
  68. if (!comCocosNode?.loaded) return
  69. let dx = this.baseX - comTransform.x
  70. let dy = this.baseY - comTransform.y
  71. let distance = dx * dx + dy * dy
  72. if (distance <= minDistance && !world.isBase(comRole)) {
  73. minDistance = distance
  74. targetEntity = entity
  75. }
  76. return false
  77. })
  78. if (targetEntity) {
  79. let comTransform: ComTransform = world.getComponent(targetEntity, ComTransform)
  80. world.createBaseBulletEntity(comTransform)
  81. this.cd = this.baseBulletCfg.attackCD
  82. }
  83. }
  84. }