/** @format */ import {ComRole} from '../components/ComRole' import {ECSSystem} from '../lib/ECSSystem' import {GenFilterKey} from '../lib/ECSComponent' import {FightWorld} from '../worlds/FightWorld' import {ComCocosNode} from '../components/ComCocosNode' import {ComAttackable} from '../components/ComAttackable' import {Data} from '../../GameControl' import {EventRoleTip, EventSlowDown} from '../core/NodeEvent' import {ComFindEnemy} from '../components/ComFindEnemy' import {ComDie} from '../components/ComDie' import { ATTR_NAME, BUFF_ANI_TYPE, BUFF_EFFECT_TYPE, BUFF_TYPE, GAME_ROLE_TIP, MONSTER_TYPE, ROLE_TYPE, SKILL_EFFECT_TYPE, } from '../../enums/Enum' import {ComBehaviorTree} from '../components/ComBehaviorTree' import {ComFrameAni} from '../components/ComFrameAni' import {RoleConfig} from '../../config/RoleConfig' import {MonsterConfig} from '../../config/MonsterConfig' const FILTER_BUFF = GenFilterKey([ComAttackable, ComRole, ComCocosNode, ComBehaviorTree], [ComDie]) export class SysBuff extends ECSSystem { /** 连接 */ public onAdd(world: FightWorld): void {} /** 断开连接 */ public onRemove(world: FightWorld): void {} /** 添加实体 */ public onEntityEnter(world: FightWorld, entity: number): void {} /** */ public onEntityLeave(world: FightWorld, entity: number): void {} /** 更新 */ public onUpdate(world: FightWorld, dt: number): void { let filterBuff = world.getFilter(FILTER_BUFF) filterBuff.walk((entity: number) => { let comRole = world.getComponent(entity, ComRole) let comCocosNode = world.getComponent(entity, ComCocosNode) //修改射程 let changeAttackRange = attr => { if (attr == ATTR_NAME.attackRange) { let comFindEnemy = world.getComponent(entity, ComFindEnemy) if (comFindEnemy.attackCObject.radius != comRole.attackRange) { comFindEnemy.attackCObject.updateSphereShape(comRole.attackRange) } } } let buffLength = comRole.buffs.length for (let i = comRole.buffs.length - 1; i >= 0; i--) { let buff = comRole.buffs[i] buff.countDown -= dt buff.countUp += dt let buffConfig = buff.buffCfg if (!buff.hurtFrameCompleted && buff.countUp >= buffConfig.hurtFrame * (buff.hurtTimes + 1)) { buff.hurtTimes += 1 if (buffConfig.buffType == BUFF_TYPE.addHp) { let addHPNum = (comRole.HP * buffConfig.attrRate[0]) / Data.game.rateNum for (let i = 0; i < buff.buffCfg.effectType.length; i++) { let type = buff.buffCfg.effectType[i] if (type == BUFF_EFFECT_TYPE.addHpByAttack) { addHPNum += (buff.buffCfg.effectParm[i][0] / Data.game.rateNum) * buff.createBaseAttr.spellAttack } } world.addHP(addHPNum, entity) comRole.HPDirty = true } } //buff刚挂上时 加上基础属性加成 if ( buff.curAttrChangeNum.length == 0 && buffConfig.attr.length > 0 && buffConfig.buffType == BUFF_TYPE.normal ) { let parentRoleCfg if (comRole.parentRoleID) { parentRoleCfg = RoleConfig[comRole.parentRoleID] if (!parentRoleCfg) parentRoleCfg = MonsterConfig[comRole.parentRoleID] } for (let j = 0; j < buffConfig.attr.length; j++) { let attrKey = buffConfig.attr[j] let cfgNum = parentRoleCfg ? (parentRoleCfg[attrKey] * comRole.roleCfg[attrKey]) / Data.game.rateNum : comRole.roleCfg[attrKey] let changeNum = buffConfig.attrNum[j] ? buffConfig.attrNum[j] : (cfgNum * buffConfig.attrRate[j]) / Data.game.rateNum let durChangeNum = comRole[attrKey] + changeNum let minV = 0 let maxV = Infinity if (attrKey == ATTR_NAME.attackRange) { if (comRole.attackRange <= Data.game.meleeRange) { minV = Data.game.meleeRange maxV = Data.game.meleeRange } else { minV = comRole.roleCfg.attackRange maxV = Data.game.maxAttackRange } } else if (attrKey == ATTR_NAME.attackSpeed) { minV = Data.game.minAttackSpeed maxV = Data.game.maxAttackSpeed } if (durChangeNum < minV) { buff.curAttrChangeNum[j] = minV - comRole[attrKey] } else if (durChangeNum > maxV) { buff.curAttrChangeNum[j] = maxV - comRole[attrKey] } else { buff.curAttrChangeNum[j] = changeNum } //boss数值衰减 for (let i = 0; i < buff.buffCfg.effectType.length; i++) { let type = buff.buffCfg.effectType[i] if (type == BUFF_EFFECT_TYPE.boss && comRole.roleCfg.type == MONSTER_TYPE.boss) { changeNum *= 1 - buff.buffCfg.effectParm[i][0] / Data.game.rateNum } } comRole[buffConfig.attr[j]] += buff.curAttrChangeNum[j] changeAttackRange(buff) } } //移除时间到的buff 减去buff的加成 if (buff.countDown <= 0) { for (let j = 0; j < buffConfig.attr.length; j++) { if (buff.curAttrChangeNum[j]) { comRole[buffConfig.attr[j]] -= buff.curAttrChangeNum[j] changeAttackRange(buff) } } comRole.buffs.splice(i, 1) for (let aniEntity of buff.aniEntityArr) { let comFrameAni = world.getComponent(aniEntity, ComFrameAni) if (comFrameAni) comFrameAni.countDown = 0 } buff.aniEntityArr.length = 0 for (let aniID of buff.buffCfg.buffUrl) { if (!aniID) continue if (aniID == BUFF_ANI_TYPE.slow) { let comSpineNode = world.getComponent(comRole.spineEntity, ComCocosNode) comSpineNode?.events.push(new EventSlowDown(false)) } } } } if (comRole.buffs.length != buffLength) { //comCocosNode.events.push(new EventBuffChange(comRole.buffs)) } return false }) } }