SysBullet.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /** @format */
  2. import {ECSSystem} from '../lib/ECSSystem'
  3. import {GenFilterKey} from '../lib/ECSComponent'
  4. import {FightWorld} from '../worlds/FightWorld'
  5. import {ComMovable} from '../components/ComMovable'
  6. import {ComTransform} from '../components/ComTransform'
  7. import {ComBullet} from '../components/ComBullet'
  8. import {ComCocosNode} from '../components/ComCocosNode'
  9. import {ComRole} from '../components/ComRole'
  10. import {ComAttackable} from '../components/ComAttackable'
  11. import {ComDie} from '../components/ComDie'
  12. import {ComEnemy} from '../components/ComEnemy'
  13. import {ComFriend} from '../components/ComFriend'
  14. import {Data, Mgr} from '../../GameControl'
  15. import {EntityIndex} from '../lib/Const'
  16. import {SkillConfig} from '../../config/SkillConfig'
  17. import {EventBulletEjection, EventRoleTip} from '../core/NodeEvent'
  18. import {
  19. BUFF_EFFECT_TYPE,
  20. BUFF_TYPE,
  21. BULLET_EFFECT_TYPE,
  22. BULLET_TYPE,
  23. ENTRY,
  24. FRAME_ANI_NAME,
  25. GAME_ROLE_TIP,
  26. ROLE_TYPE,
  27. SKILL_EFFECT_TYPE,
  28. } from '../../enums/Enum'
  29. import {IAttackEffect} from '../core/GameInterface'
  30. import {IBulletConfig} from '../../config/BulletConfig'
  31. import {ComFrameAni} from '../components/ComFrameAni'
  32. const FILTER_BULLET = GenFilterKey([ComBullet, ComMovable, ComCocosNode, ComTransform])
  33. const FILTER_FRIEND_ROLE = GenFilterKey([ComRole, ComCocosNode, ComTransform, ComFriend], [ComDie])
  34. const FILTER_ENEMY_ROLE = GenFilterKey([ComRole, ComCocosNode, ComTransform, ComEnemy], [ComDie])
  35. export class SysBullet extends ECSSystem {
  36. /** 连接 */
  37. public onAdd(world: FightWorld): void {}
  38. /** 断开连接 */
  39. public onRemove(world: FightWorld): void {}
  40. /** 添加实体 */
  41. public onEntityEnter(world: FightWorld, entity: number): void {}
  42. /** */
  43. public onEntityLeave(world: FightWorld, entity: number): void {}
  44. /** 更新 */
  45. public onUpdate(world: FightWorld, dt: number): void {
  46. let filter = world.getFilter(FILTER_BULLET)
  47. let friendsFilter = world.getFilter(FILTER_FRIEND_ROLE)
  48. let enemiesFilter = world.getFilter(FILTER_ENEMY_ROLE)
  49. let changeHpByAttack = (comBullet: ComBullet, roleEntity) => {
  50. let bulletCfg = comBullet.bulletCfg
  51. Mgr.audio.playSFX(bulletCfg.sound)
  52. if (comBullet.bulletCfg.type == BULLET_TYPE.normal) {
  53. //目标敌人掉血
  54. let comRoleEnemy = world.getComponent(roleEntity, ComRole)
  55. let comEnemyCocosNode = world.getComponent(roleEntity, ComCocosNode)
  56. //基地免疫
  57. if (comRoleEnemy.roleCfg.type == ROLE_TYPE.base) {
  58. let effectParmIndex = bulletCfg.effectType.findIndex(
  59. value => value == BULLET_EFFECT_TYPE.castleVoid,
  60. )
  61. if (effectParmIndex >= 0) return
  62. }
  63. //是否闪避
  64. let isDodge =
  65. comRoleEnemy &&
  66. Math.getProb(
  67. comBullet.fightData.hit - comRoleEnemy.dodge > 0
  68. ? 0
  69. : (comRoleEnemy.dodge - comBullet.fightData.hit) / Data.game.rateNum,
  70. )
  71. if (!isDodge) {
  72. let attackEffect: IAttackEffect = {isBack: false, isCrit: comBullet.isCrit}
  73. let hurt = world.getHurtByAttackRole(comBullet.fightData, roleEntity, attackEffect)
  74. let decrease = 0
  75. let effectParmIndex = bulletCfg.effectType.findIndex(value => value == BULLET_EFFECT_TYPE.ejection)
  76. if (effectParmIndex >= 0) decrease = bulletCfg.effectParm[effectParmIndex][0]
  77. hurt *= Math.pow(1 - decrease / Data.game.rateNum, comBullet.ejectionEnemies.length)
  78. if (hurt < 0) hurt = 0
  79. //远程伤害免疫为1点
  80. if (hurt > 1 && comRoleEnemy.buffs.some(buff => buff.buffCfg.buffType == BUFF_TYPE.rangedImmune)) {
  81. hurt = 1
  82. }
  83. world.changeHpByHurt(roleEntity, hurt, comBullet.isCrit, comBullet.bulletCfg.hitID)
  84. //暂时屏蔽子弹特效
  85. //world.changeHpByHurt(roleEntity, hurt, comBullet.isCrit, 0)
  86. if (!world.isDie(comBullet.fightData.roleEntity)) {
  87. let createComRole = world.getComponent(comBullet.fightData.roleEntity, ComRole)
  88. for (let buff of createComRole.buffs) {
  89. if (buff.buffCfg.buffType == BUFF_TYPE.attackAddBabyHP) {
  90. //攻击让宝宝加血
  91. let addHPNum = (hurt * buff.buffCfg.attrRate[0]) / Data.game.rateNum
  92. createComRole.babys.forEach(baby => {
  93. world.addHP(addHPNum, baby)
  94. })
  95. }
  96. }
  97. }
  98. //吸血
  99. if (comBullet.fightData.entryMap.get(ENTRY.hurt2HP)?.num > 0) {
  100. let addHPNum = (comBullet.fightData.entryMap.get(ENTRY.hurt2HP).num * hurt) / Data.game.rateNum
  101. world.addHP(addHPNum, comBullet.fightData.roleEntity)
  102. }
  103. } else {
  104. comEnemyCocosNode.events.push(new EventRoleTip(GAME_ROLE_TIP.missTip, 0, 'miss'))
  105. }
  106. } else if (comBullet.bulletCfg.type == BULLET_TYPE.skill) {
  107. world.createCardBulletSkill(comBullet, roleEntity)
  108. }
  109. }
  110. filter.walk((bulletEntity: number) => {
  111. let comCocosNode = world.getComponent(bulletEntity, ComCocosNode)
  112. if (!comCocosNode.loaded) return
  113. let comBullet = world.getComponent(bulletEntity, ComBullet)
  114. let comMovable = world.getComponent(bulletEntity, ComMovable)
  115. let bulletTrans = world.getComponent(bulletEntity, ComTransform)
  116. let bulletCfg = comBullet.bulletCfg
  117. //产生碰撞
  118. if (comBullet.cObject.containsBody.size > 0) {
  119. let containsArr = comBullet.cObject.containsBody.keysArr()
  120. for (let i = 0; i < containsArr.length; i++) {
  121. let roleEntity = containsArr[i]
  122. if (
  123. bulletCfg.isEjection &&
  124. roleEntity == comBullet.ejectionEnemy &&
  125. comMovable.pointIdx >= comMovable.points.length
  126. ) {
  127. changeHpByAttack(comBullet, roleEntity)
  128. //推入弹中数组
  129. comBullet.ejectionEnemies.push(roleEntity)
  130. //连线的终点改为角色
  131. if (bulletCfg.effectType.includes(BULLET_EFFECT_TYPE.line)) {
  132. let lastEjectionAni = comBullet.ejectionAnis[comBullet.ejectionAnis.length - 1]
  133. let lastComFrameAni = world.getComponent(lastEjectionAni, ComFrameAni)
  134. if (lastComFrameAni) {
  135. lastComFrameAni.lineEndEntity = roleEntity
  136. }
  137. }
  138. //子弹移除
  139. if (comBullet.ejectionEnemies.length >= bulletCfg.maxHitNum) {
  140. world.removeBulletEntity(bulletEntity)
  141. } else {
  142. //子弹弹射
  143. let targetFilter =
  144. comBullet.group == Data.game.gFriendBullet ? enemiesFilter : friendsFilter
  145. let minDistance = Infinity
  146. let nearestTrans: ComTransform = null
  147. let nearestEntity: EntityIndex = null
  148. let ejectionParam: number[] = null
  149. for (let i = 0; i < bulletCfg.effectType.length; i++) {
  150. let type = bulletCfg.effectType[i]
  151. if (type == BULLET_EFFECT_TYPE.ejection) {
  152. ejectionParam = bulletCfg.effectParm[i]
  153. }
  154. }
  155. let maxAttackRange = ejectionParam
  156. ? ejectionParam[1]
  157. : Data.game.fightSizeMaxX - Data.game.fightSizeMinX
  158. let decrease = ejectionParam ? ejectionParam[0] : 0
  159. let moreTimesHurt = ejectionParam ? ejectionParam[2] : true
  160. targetFilter.walk((enemyEntity: number) => {
  161. let otherTrans = world.getComponent(enemyEntity, ComTransform)
  162. let dx = otherTrans.x - bulletTrans.x
  163. let dy = otherTrans.y - bulletTrans.y
  164. let distance = Math.sqrt(dx * dx + dy * dy)
  165. //不等于弹射目标不能弹基地
  166. if (
  167. distance < minDistance &&
  168. enemyEntity != comBullet.ejectionEnemy &&
  169. !world.isBase(enemyEntity) &&
  170. (moreTimesHurt ||
  171. (!moreTimesHurt && !comBullet.ejectionEnemies.includes(enemyEntity)))
  172. ) {
  173. minDistance = distance
  174. nearestTrans = otherTrans
  175. nearestEntity = enemyEntity
  176. }
  177. return false
  178. })
  179. if (nearestTrans && minDistance < maxAttackRange) {
  180. comBullet.ejectionEnemy = nearestEntity
  181. world.initBulletDir(bulletTrans, nearestTrans, comMovable, comBullet, comCocosNode)
  182. comMovable.speed = bulletCfg.moveSpeed
  183. //comCocosNode.events.push(new EventBulletEjection(bulletTrans.x, bulletTrans.y))
  184. //创建连线特效
  185. if (bulletCfg.effectType.includes(BULLET_EFFECT_TYPE.line)) {
  186. let aniEntity = world.createAni(FRAME_ANI_NAME.lightLine, 0)
  187. let comFrameAni = world.getComponent(aniEntity, ComFrameAni)
  188. let startComTrans = world.getComponent(roleEntity, ComTransform)
  189. comFrameAni.lineStartPos = cc.v2(startComTrans.x, startComTrans.y)
  190. if (!world.isDie(roleEntity)) comFrameAni.lineStartEntity = roleEntity
  191. comFrameAni.lineEndEntity = bulletEntity
  192. comBullet.ejectionAnis.push(aniEntity)
  193. }
  194. return true
  195. } else {
  196. world.removeBulletEntity(bulletEntity)
  197. }
  198. }
  199. } else if (!bulletCfg.isEjection && !comBullet.hitEnemies.includes(roleEntity)) {
  200. if (comBullet.bulletCfg.type == BULLET_TYPE.normal) {
  201. comBullet.hitEnemies.push(roleEntity)
  202. if (comBullet.hitEnemies.length <= bulletCfg.maxHitNum) {
  203. changeHpByAttack(comBullet, roleEntity)
  204. }
  205. //子弹移除
  206. if (comBullet.hitEnemies.length >= bulletCfg.maxHitNum) {
  207. world.removeBulletEntity(bulletEntity)
  208. }
  209. }
  210. }
  211. }
  212. }
  213. return false
  214. })
  215. }
  216. }