SysBuff.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 {ComAttackable} from '../components/ComAttackable'
  8. import {Data} from '../../GameControl'
  9. import {EventRoleTip, EventSlowDown} from '../core/NodeEvent'
  10. import {ComFindEnemy} from '../components/ComFindEnemy'
  11. import {ComDie} from '../components/ComDie'
  12. import {
  13. ATTR_NAME,
  14. BUFF_ANI_TYPE,
  15. BUFF_EFFECT_TYPE,
  16. BUFF_TYPE,
  17. GAME_ROLE_TIP,
  18. MONSTER_TYPE,
  19. ROLE_TYPE,
  20. SKILL_EFFECT_TYPE,
  21. } from '../../enums/Enum'
  22. import {ComBehaviorTree} from '../components/ComBehaviorTree'
  23. import {ComFrameAni} from '../components/ComFrameAni'
  24. import {RoleConfig} from '../../config/RoleConfig'
  25. import {MonsterConfig} from '../../config/MonsterConfig'
  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. buff.countDown -= dt
  55. buff.countUp += dt
  56. let buffConfig = buff.buffCfg
  57. if (!buff.hurtFrameCompleted && buff.countUp >= buffConfig.hurtFrame * (buff.hurtTimes + 1)) {
  58. buff.hurtTimes += 1
  59. if (buffConfig.buffType == BUFF_TYPE.addHp) {
  60. let addHPNum = (comRole.HP * buffConfig.attrRate[0]) / Data.game.rateNum
  61. for (let i = 0; i < buff.buffCfg.effectType.length; i++) {
  62. let type = buff.buffCfg.effectType[i]
  63. if (type == BUFF_EFFECT_TYPE.addHpByAttack) {
  64. addHPNum +=
  65. (buff.buffCfg.effectParm[i][0] / Data.game.rateNum) *
  66. buff.createBaseAttr.spellAttack
  67. }
  68. }
  69. world.addHP(addHPNum, entity)
  70. comRole.HPDirty = true
  71. }
  72. }
  73. //buff刚挂上时 加上基础属性加成
  74. if (
  75. buff.curAttrChangeNum.length == 0 &&
  76. buffConfig.attr.length > 0 &&
  77. buffConfig.buffType == BUFF_TYPE.normal
  78. ) {
  79. let parentRoleCfg
  80. if (comRole.parentRoleID) {
  81. parentRoleCfg = RoleConfig[comRole.parentRoleID]
  82. if (!parentRoleCfg) parentRoleCfg = MonsterConfig[comRole.parentRoleID]
  83. }
  84. for (let j = 0; j < buffConfig.attr.length; j++) {
  85. let attrKey = buffConfig.attr[j]
  86. let cfgNum = parentRoleCfg
  87. ? (parentRoleCfg[attrKey] * comRole.roleCfg[attrKey]) / Data.game.rateNum
  88. : comRole.roleCfg[attrKey]
  89. let changeNum = buffConfig.attrNum[j]
  90. ? buffConfig.attrNum[j]
  91. : (cfgNum * buffConfig.attrRate[j]) / Data.game.rateNum
  92. let durChangeNum = comRole[attrKey] + changeNum
  93. let minV = 0
  94. let maxV = Infinity
  95. if (attrKey == ATTR_NAME.attackRange) {
  96. if (comRole.attackRange <= Data.game.meleeRange) {
  97. minV = Data.game.meleeRange
  98. maxV = Data.game.meleeRange
  99. } else {
  100. minV = comRole.roleCfg.attackRange
  101. maxV = Data.game.maxAttackRange
  102. }
  103. } else if (attrKey == ATTR_NAME.attackSpeed) {
  104. minV = Data.game.minAttackSpeed
  105. maxV = Data.game.maxAttackSpeed
  106. }
  107. if (durChangeNum < minV) {
  108. buff.curAttrChangeNum[j] = minV - comRole[attrKey]
  109. } else if (durChangeNum > maxV) {
  110. buff.curAttrChangeNum[j] = maxV - comRole[attrKey]
  111. } else {
  112. buff.curAttrChangeNum[j] = changeNum
  113. }
  114. //boss数值衰减
  115. for (let i = 0; i < buff.buffCfg.effectType.length; i++) {
  116. let type = buff.buffCfg.effectType[i]
  117. if (type == BUFF_EFFECT_TYPE.boss && comRole.roleCfg.type == MONSTER_TYPE.boss) {
  118. changeNum *= 1 - buff.buffCfg.effectParm[i][0] / Data.game.rateNum
  119. }
  120. }
  121. comRole[buffConfig.attr[j]] += buff.curAttrChangeNum[j]
  122. changeAttackRange(buff)
  123. }
  124. }
  125. //移除时间到的buff 减去buff的加成
  126. if (buff.countDown <= 0) {
  127. for (let j = 0; j < buffConfig.attr.length; j++) {
  128. if (buff.curAttrChangeNum[j]) {
  129. comRole[buffConfig.attr[j]] -= buff.curAttrChangeNum[j]
  130. changeAttackRange(buff)
  131. }
  132. }
  133. comRole.buffs.splice(i, 1)
  134. for (let aniEntity of buff.aniEntityArr) {
  135. let comFrameAni = world.getComponent(aniEntity, ComFrameAni)
  136. if (comFrameAni) comFrameAni.countDown = 0
  137. }
  138. buff.aniEntityArr.length = 0
  139. for (let aniID of buff.buffCfg.buffUrl) {
  140. if (!aniID) continue
  141. if (aniID == BUFF_ANI_TYPE.slow) {
  142. let comSpineNode = world.getComponent(comRole.spineEntity, ComCocosNode)
  143. comSpineNode?.events.push(new EventSlowDown(false))
  144. }
  145. }
  146. }
  147. }
  148. if (comRole.buffs.length != buffLength) {
  149. //comCocosNode.events.push(new EventBuffChange(comRole.buffs))
  150. }
  151. return false
  152. })
  153. }
  154. }