SysBuff.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /** @format */
  2. import {ComRole} from '../components/ComRole'
  3. import {ECSSystem} from '../lib/ECSSystem'
  4. import {GenFilterKey} from '../lib/ECSComponent'
  5. import {ComCocosNode} from '../components/ComCocosNode'
  6. import {ComAttackable} from '../components/ComAttackable'
  7. import {Data} from '../../GameControl'
  8. import {EventRoleTip, EventSlowDown} from '../core/NodeEvent'
  9. import {ComFindEnemy} from '../components/ComFindEnemy'
  10. import {ComDie} from '../components/ComDie'
  11. import {
  12. ATTR_NAME,
  13. BUFF_ANI_TYPE,
  14. BUFF_EFFECT_TYPE,
  15. BUFF_TYPE,
  16. GAME_ROLE_TIP,
  17. MONSTER_TYPE,
  18. ROLE_TYPE,
  19. SKILL_EFFECT_TYPE,
  20. } from '../../enums/Enum'
  21. import {ComBehaviorTree} from '../components/ComBehaviorTree'
  22. import {ComFrameAni} from '../components/ComFrameAni'
  23. import {RoleConfig} from '../../config/RoleConfig'
  24. import {MonsterConfig} from '../../config/MonsterConfig'
  25. import {FightWorld} from '../worlds/FightWorld'
  26. const FILTER_BUFF = GenFilterKey([ComAttackable, ComRole, ComCocosNode, ComBehaviorTree], [ComDie])
  27. export class SysBuff extends ECSSystem {
  28. /** 连接 */
  29. public onAdd(world: FightWorld): void {}
  30. /** 断开连接 */
  31. public onRemove(world: FightWorld): void {}
  32. /** 添加实体 */
  33. public onEntityEnter(world: FightWorld, entity: number): void {}
  34. /** */
  35. public onEntityLeave(world: FightWorld, entity: number): void {}
  36. /** 更新 */
  37. public onUpdate(world: FightWorld, dt: number): void {
  38. let filterBuff = world.getFilter(FILTER_BUFF)
  39. filterBuff.walk((entity: number) => {
  40. let comRole = world.getComponent(entity, ComRole)
  41. let comCocosNode = world.getComponent(entity, ComCocosNode)
  42. //修改射程
  43. let changeAttackRange = attr => {
  44. if (attr == ATTR_NAME.attackRange) {
  45. let comFindEnemy = world.getComponent(entity, ComFindEnemy)
  46. if (comFindEnemy.attackCObject.radius != comRole.attackRange) {
  47. comFindEnemy.attackCObject.updateSphereShape(comRole.attackRange)
  48. }
  49. }
  50. }
  51. let buffLength = comRole.buffs.length
  52. for (let i = comRole.buffs.length - 1; i >= 0; i--) {
  53. let buff = comRole.buffs[i]
  54. let buffConfig = buff.buffCfg
  55. //buff刚挂上时
  56. if (buff.countUp == 0) {
  57. }
  58. if (!buff.hurtFrameCompleted && buff.countUp >= buffConfig.hurtFrame * (buff.hurtTimes + 1)) {
  59. buff.hurtTimes += 1
  60. if (buffConfig.buffType == BUFF_TYPE.addHp) {
  61. let addHPNum = (comRole.HP * buffConfig.attrRate[0]) / Data.game.rateNum
  62. for (let i = 0; i < buff.buffCfg.effectType.length; i++) {
  63. let type = buff.buffCfg.effectType[i]
  64. if (type == BUFF_EFFECT_TYPE.addHpByAttack) {
  65. addHPNum +=
  66. (buff.buffCfg.effectParm[i][0] / Data.game.rateNum) *
  67. buff.createBaseAttr.spellAttack
  68. }
  69. }
  70. world.addHP(addHPNum, entity)
  71. comRole.HPDirty = true
  72. }
  73. }
  74. //buff刚挂上时 加上基础属性加成
  75. if (
  76. buff.curAttrChangeNum.length == 0 &&
  77. buffConfig.attr.length > 0 &&
  78. buffConfig.buffType == BUFF_TYPE.normal
  79. ) {
  80. let parentRoleCfg
  81. if (comRole.parentRoleID) {
  82. parentRoleCfg = RoleConfig[comRole.parentRoleID]
  83. if (!parentRoleCfg) parentRoleCfg = MonsterConfig[comRole.parentRoleID]
  84. }
  85. for (let j = 0; j < buffConfig.attr.length; j++) {
  86. let attrKey = buffConfig.attr[j]
  87. let cfgNum = parentRoleCfg
  88. ? (parentRoleCfg[attrKey] * comRole.roleCfg[attrKey]) / Data.game.rateNum
  89. : comRole.roleCfg[attrKey]
  90. let changeNum = buffConfig.attrNum[j]
  91. ? buffConfig.attrNum[j]
  92. : (cfgNum * buffConfig.attrRate[j]) / Data.game.rateNum
  93. let durChangeNum = comRole[attrKey] + changeNum
  94. let minV = 0
  95. let maxV = Infinity
  96. if (attrKey == ATTR_NAME.attackRange) {
  97. if (comRole.attackRange <= Data.game.meleeRange) {
  98. minV = Data.game.meleeRange
  99. maxV = Data.game.meleeRange
  100. } else {
  101. minV = comRole.roleCfg.attackRange
  102. maxV = Data.game.maxAttackRange
  103. }
  104. } else if (attrKey == ATTR_NAME.attackSpeed) {
  105. minV = Data.game.minAttackSpeed
  106. maxV = Data.game.maxAttackSpeed
  107. }
  108. if (durChangeNum < minV) {
  109. buff.curAttrChangeNum[j] = minV - comRole[attrKey]
  110. } else if (durChangeNum > maxV) {
  111. buff.curAttrChangeNum[j] = maxV - comRole[attrKey]
  112. } else {
  113. buff.curAttrChangeNum[j] = changeNum
  114. }
  115. //boss数值衰减
  116. for (let i = 0; i < buff.buffCfg.effectType.length; i++) {
  117. let type = buff.buffCfg.effectType[i]
  118. if (type == BUFF_EFFECT_TYPE.boss && comRole.roleCfg.type == MONSTER_TYPE.boss) {
  119. changeNum *= 1 - buff.buffCfg.effectParm[i][0] / Data.game.rateNum
  120. }
  121. }
  122. comRole[buffConfig.attr[j]] += buff.curAttrChangeNum[j]
  123. changeAttackRange(buff)
  124. }
  125. }
  126. buff.countDown -= dt
  127. buff.countUp += dt
  128. if (buff.countDown <= 0) {
  129. //移除时间到的buff 减去buff的加成
  130. for (let j = 0; j < buffConfig.attr.length; j++) {
  131. if (buff.curAttrChangeNum[j]) {
  132. comRole[buffConfig.attr[j]] -= buff.curAttrChangeNum[j]
  133. changeAttackRange(buff)
  134. }
  135. }
  136. comRole.buffs.splice(i, 1)
  137. for (let aniEntity of buff.aniEntityArr) {
  138. let comFrameAni = world.getComponent(aniEntity, ComFrameAni)
  139. if (comFrameAni) comFrameAni.countDown = 0
  140. }
  141. buff.aniEntityArr.length = 0
  142. for (let aniID of buff.buffCfg.buffUrl) {
  143. if (!aniID) continue
  144. if (aniID == BUFF_ANI_TYPE.slow) {
  145. let comSpineNode = world.getComponent(comRole.spineEntity, ComCocosNode)
  146. comSpineNode?.events.push(new EventSlowDown(false))
  147. }
  148. }
  149. }
  150. }
  151. if (comRole.buffs.length != buffLength) {
  152. //comCocosNode.events.push(new EventBuffChange(comRole.buffs))
  153. }
  154. return false
  155. })
  156. }
  157. }