import { _decorator, Component, Node, v3, Vec3 } from 'cc'; import { HeroViewComp } from './HeroViewComp'; import { SkillSet, TGroup, TType } from '../common/config/SkillSet'; import { Skill } from '../skills/Skill'; import { ecs } from 'db://oops-framework/libs/ecs/ECS'; import { oops } from 'db://oops-framework/core/Oops'; import { GameEvent } from '../common/config/GameEvent'; import { BoxSet, FacSet } from '../common/config/BoxSet'; import { smc } from '../common/SingletonModuleComp'; import { CCComp } from 'db://oops-framework/module/common/CCComp'; import { FightConComp } from '../map/FightConComp'; import { MonModelComp } from './MonModelComp'; import { HeroModelComp } from './HeroModelComp'; import { FightSet } from '../common/config/Mission'; import { MasterModelComp } from './MasterModel'; const { ccclass, property } = _decorator; @ccclass('SkillCon') @ecs.register('SkillCon') export class SkillConComp extends CCComp { FIGHTCON:FightConComp=null! HeroView:any=null; HeroEntity:any=null; private _timers: { [key: string]: number } = {}; private _damageQueue: Array<{ timer: number; callback: () => void }> = []; aoe_queues:any[]=[] // 范围伤害技能执行队列 private aoe_timers: Map = new Map(); // 每个技能的独立计时器 private readonly AOE_INTERVAL: number = 0.4; // 执行间隔,单位秒 private skill_id_counter: number = 0; // 技能ID计数器 aoe_target_pos:Vec3=new Vec3(180,40,0) init(): void { oops.message.on(GameEvent.FightEnd, this.clear_timer, this); } onLoad(){ this.HeroView=this.node.getComponent(HeroViewComp) this.FIGHTCON=this.node.parent.getComponent(FightConComp) // console.log(this.HeroView.uuid+"=>"+this.HeroView.hero_name+"=> SkillConComp onLoad") this.on(GameEvent.CastHeroSkill,this.cast_master_skill,this) oops.message.on(GameEvent.MaxSkill,this.use_max_skill,this) } start() { // console.log(this.HeroView.uuid+"=>"+this.HeroView.hero_name+"=> SkillConComp start") this.HeroEntity=this.HeroView.ent } update(dt: number) { if(!smc.mission.play||smc.mission.pause) return if(this.HeroView.DEBUFF_STUN <= 0&&this.HeroView.DEBUFF_FROST <= 0) this.HeroView.at += dt; let cd = this.get_cd(this.HeroView.cd,this.HeroView) if (this.HeroView.is_atking &&(this.HeroView.at > cd)) { if(this.HeroView.is_dead) return const config = SkillSet[this.HeroView.atk_skill]; if (!config) return; this.castSkill(config,this.check_wfuny()); this.HeroView.count_atk_count() this.HeroView.at = 0; } if (this.aoe_queues.length > 0) { console.log("[FightConComp]:aoe_queues:",this.aoe_queues) // 遍历所有必杀技技能,更新它们的计时器 for (let i = this.aoe_queues.length - 1; i >= 0; i--) { let skill = this.aoe_queues[i]; let timer = this.aoe_timers.get(skill.id) || 0; timer += dt; this.aoe_timers.set(skill.id, timer); // 检查是否到达执行间隔 if (timer >= this.AOE_INTERVAL) { // 重置计时器 this.aoe_timers.set(skill.id, 0); // 执行技能 this.aoe_skill_execute(skill); skill.count--; // 如果技能执行完毕,从队列中移除 if (skill.count <= 0) { this.aoe_queues.splice(i, 1); this.aoe_timers.delete(skill.id); } } } } } cast_master_skill(e:string,uuid:any){ if(!this.HeroView) return if(!this.HeroView.is_master) return console.log("hart cast_skill",uuid ,e) const config = SkillSet[uuid]; this.castSkill(config,false,this.FIGHTCON.hero_buff.SKILL_DMG) } /** 施放技能 */ castSkill(config: typeof SkillSet[keyof typeof SkillSet],is_wfuny:boolean=false,dmg:number=0) { // console.log(view.uuid+"=>"+view.hero_name+"施放技能:"+config.uuid); this.doSkill(config,is_wfuny,dmg); } use_max_skill(e:GameEvent,data:any){ if(!this.HeroView) return if(!this.HeroView.is_master) return console.log("[SkillConComp]:use_max_skill:",data) this.skill_id_counter++; this.aoe_queues.push({ id: this.skill_id_counter, s_uuid:data.uuid, count:SkillSet[data.uuid].maxC, damage:0}) // 初始化该技能的计时器 this.aoe_timers.set(this.skill_id_counter, 0); } private doSkill(config: typeof SkillSet[keyof typeof SkillSet],is_wfuny:boolean=false,dmg:number=0) { // 添加节点有效性检查 if (!this.node || !this.node.isValid || !this.HeroView || !this.HeroView.node || !this.HeroView.node.isValid) { return; } let target:any=null switch(config.TGroup){ case TGroup.Enemy: //单个敌人 target = this.filterFrontRow() break case TGroup.Ally: //所有敌人 target = this.selectAllyTargets() break case TGroup.Self: //自身 target = this.node.position break case TGroup.Team: //所有友方 target = this.node.position break case TGroup.All: //所有单位 break } this.HeroView.playSkillEffect(config.uuid) const skillEntity = ecs.getEntity(Skill); const timerId = setTimeout(() => { // 再次检查节点有效性 if (!this.node || !this.node.isValid || !this.HeroView || !this.HeroView.node || !this.HeroView.node.isValid) { return; } skillEntity.load( new Vec3(this.HeroView.node.position.x + BoxSet.ATK_X * this.HeroView.scale, this.HeroView.node.position.y + BoxSet.ATK_Y, 0), this.node.parent, config.uuid, new Vec3(target.x, target.y+BoxSet.ATK_Y, 0), this.HeroView, 0, dmg ); }, 300); if(is_wfuny){ this.scheduleOnce(()=>{ this.HeroView.ex_show("blue") this.doSkill(config,false,dmg) },0.05) } // 保存定时器ID this._timers[`skill_${config.uuid}`] = timerId; } check_wfuny(){ let buff=this.get_buff(this.HeroView) if(buff==null) return false let random = Math.random()*100 if(random < buff.WFUNY){ return true } return false } check_target(){ if(this.HeroView.fac==FacSet.HERO){ return ecs.query(ecs.allOf(MonModelComp)) }else{ return ecs.query(ecs.allOf(HeroModelComp)) } } get_front(entities:any){ let keyPos = this.HeroView.fac==FacSet.HERO ? Math.min(...entities.map(e => e.get(HeroViewComp).node.position.x)) : Math.max(...entities.map(e => e.get(HeroViewComp).node.position.x)); let keyEntity = entities.find(e => e.get(HeroViewComp).node.position.x === keyPos); return keyEntity.get(HeroViewComp).node.position; } /** 筛选最前排单位 */ private filterFrontRow(): Vec3{ // 敌方最前排是x坐标最大的,我方最前排是x坐标最小的,若目标不存在,敌人 取400,我方取-400 let pos=v3(0,0) let entities=this.check_target() if(entities.length==0){ if(this.HeroView.fac==FacSet.HERO){ return v3(400,0) }else{ return v3(-400,0) } } pos=v3(this.get_front(entities)) return pos } private selectAllyTargets( ): Vec3 { // 敌方最前排是x坐标最大的+50,我方最前排是x坐标最小的+50,若目标不存在,敌人 取320/2,我方取-320/2 let kp=50 if(this.HeroView.fac==FacSet.MON) kp=0 let pos=v3(0,0) let entities=this.check_target() if(entities.length==0){ if(this.HeroView.fac==FacSet.HERO){ return v3(320/2+kp,0) }else{ return v3(-320/2-kp,0) } } pos=v3(this.get_front(entities).x+kp,this.get_front(entities).y-BoxSet.ATK_Y,0) return pos } private aoe_skill_execute(data:any){ let skill=ecs.getEntity(Skill) let master = ecs.query(ecs.allOf(MasterModelComp)) // 检查必要参数 if (!master || master.length === 0) { console.error("[FightConComp] 未找到主角实体"); return; } let masterView = master[0].get(HeroViewComp); if (!masterView) { console.error("[FightConComp] 主角视图组件获取失败"); return; } let angle=0 let targets = this.pickRandomTarget(data.count) let target_pos= new Vec3(0,0,0) if(targets.length==0){ target_pos=this.aoe_target_pos }else{ target_pos= new Vec3(targets[0].get(HeroViewComp).node.position.x,0,0) } let start_pos=new Vec3(-280,50,0) // console.log("[Skill]:node=>",start_pos) skill.load( start_pos, this.node.parent, data.s_uuid, target_pos, masterView, angle, data.damage ); } /** 随机选择目标 */ private pickRandomTarget(count: number): ecs.Entity[] { let entities:any=null if(this.HeroView.fac==FacSet.HERO){ entities = ecs.query(ecs.allOf(MonModelComp)) }else{ entities = ecs.query(ecs.allOf(HeroModelComp)) } const shuffled = [...entities].sort(() => 0.5 - Math.random()); return shuffled.slice(0, count); } public clear_timer() { // console.log("clear_timer"); Object.values(this._timers).forEach(clearTimeout); } get_cd(cd:number,view:HeroViewComp){ let buff=this.get_buff(view) let buff_cd=0 if(buff!=null) buff_cd=buff.ATK_CD // 汇总DEBUFF_DECD并处理count值 let decd = 0; for (let i = view.DEBUFF_DECDS.length - 1; i >= 0; i--) { decd += view.DEBUFF_DECDS[i].value; view.DEBUFF_DECDS[i].count--; // 当count为0时移除该记录 if (view.DEBUFF_DECDS[i].count <= 0) { view.DEBUFF_DECDS.splice(i, 1); } } let bcd=0 for (let i = view.BUFF_CDS.length - 1; i >= 0; i--) { bcd += view.BUFF_CDS[i].value; view.BUFF_CDS[i].count--; if (view.BUFF_CDS[i].count <= 0) { view.BUFF_CDS.splice(i, 1); } } return cd*(100-bcd-buff_cd+decd)/100 } get_count(count:number,view:HeroViewComp){ let buff=this.get_buff(view) if(buff==null) return count let re=count+(buff.WFUNY) if(re<1) re=1 return re } get_buff(view:HeroViewComp){ if(view.is_master) return this.FIGHTCON.hero_buff if(view.is_boss||view.is_kalami) return this.FIGHTCON.enemy_buff return null } reset() { this.clear_timer(); this.aoe_queues = [] // 清空技能队列 this.aoe_timers = new Map(); // 重置计时器 this.skill_id_counter = 0; // 重置技能ID计数器 } onDestroy() { // 清理所有定时器 Object.values(this._timers).forEach(clearTimeout); this._timers = {}; // 移除事件监听 this.off(GameEvent.CastHeroSkill); } }