import { _decorator, Animation, CCInteger, Collider2D, Contact2DType, UITransform, v3, Vec3 } from "cc"; import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS"; import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp"; import { HeroViewComp } from "../hero/HeroViewComp"; import { DTType, EType, RType, SkillSet } from "../common/config/SkillSet"; import { BezierMove } from "../BezierMove/BezierMove"; import { BoxSet } from "../common/config/BoxSet"; import { DmgDataCom, SDataCom } from "./SDataCom"; import { SMoveDataComp } from "./SMoveComp"; import { Attrs } from "../common/config/HeroAttrs"; import { MonMoveComp } from "../hero/MonMove"; import { HeroAttrsComp } from "../hero/HeroAttrsComp"; const { ccclass, property } = _decorator; /** 视图层对象 */ @ccclass('SkillViewComp') @ecs.register('SkillView', false) export class SkillView extends CCComp { /** 视图层逻辑代码分离演示 */ @property({ type: CCInteger }) atk_x: number = 0 @property({ type: CCInteger }) atk_y: number = 0 @property({ type: CCInteger }) runType: number = 0 //技能运行类型 0-线性 1-贝塞尔 2-开始位置固定 3-目标位置固定 anim:Animation=null; group:number=0; SConf:any=null; s_uuid:number=1001 start() { console.log("SkillView start scale",this.node.scale) this.SConf = SkillSet[this.s_uuid] this.anim=this.node.getComponent(Animation) this.node.active = true; let collider = this.getComponent(Collider2D); if(collider) { collider.group = this.group; collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); } const SMove=this.ent.get(SMoveDataComp) // 计算延长后的目标点坐标 switch(this.runType){ case RType.linear: SMove.rePos(v3(this.node.position.x + this.atk_x, this.node.position.y + this.atk_y)) this.do_linear(SMove.startPos,SMove.targetPos) break case RType.bezier: SMove.rePos(v3(this.node.position.x + this.atk_x, this.node.position.y + this.atk_y)) this.do_bezier(SMove.startPos,SMove.targetPos) break case RType.fixed: this.do_fixedStart(SMove.startPos,SMove.targetPos) break case RType.fixedEnd: this.do_fixedEnd(SMove.startPos,SMove.targetPos) break } } onBeginContact (seCol: Collider2D, oCol: Collider2D) { // console.log(this.scale+"碰撞开始 ",seCol,oCol); if(seCol.node.position.x-oCol.node.position.x > 100 ) return let target = oCol.getComponent(HeroViewComp) if(oCol.group!=this.group){ if(target == null) return; if (!this.SConf) return; } } do_bezier(startPos:Vec3,targetPos:Vec3){ console.log("do_bezier",startPos,targetPos) let bm=this.node.getComponent(BezierMove) this.node.angle +=10 // bm.speed=700 if(this.group==BoxSet.MONSTER) {bm.controlPointSide=-1 } bm.rotationSmoothness=0.6 bm.moveTo(targetPos) } do_linear(startPos:Vec3,targetPos:Vec3){ console.log("do_linear",startPos,targetPos) let bm=this.node.getComponent(BezierMove) let s_x=startPos.x let s_y=startPos.y let t_x=targetPos.x let t_y=targetPos.y // 设定目标x targetPos.x = 400; if(this.group == BoxSet.MONSTER) { bm.controlPointSide = -1; targetPos.x = -400; } // 计算斜率 const k = (t_y - s_y) / (t_x - s_x); // 按直线公式计算新的y targetPos.y = k * (targetPos.x - s_x) + s_y; bm.controlPointOffset=0 bm.rotationSmoothness=0.6 bm.moveTo(targetPos); } do_fixedEnd(startPos:Vec3,targetPos:Vec3){ console.log("do_fixedEnd",startPos,targetPos) this.node.setPosition(targetPos.x > 360?300:targetPos.x,this.node.position.y,0) this.do_anim() } do_fixedStart(startPos:Vec3,targetPos:Vec3){ console.log("do_fixedStart",startPos,targetPos) this.node.setPosition(startPos.x,this.node.position.y,0) this.do_anim() } do_anim(){ if(this.node.getComponent(Animation)){ let anim = this.node.getComponent(Animation); //console.log("[SkillCom]:has anim",anim) anim.on(Animation.EventType.FINISHED, this.onAnimationFinished, this); } } onAnimationFinished(){ } //动画帧事件 atk 触发 public atk(args:any){ let dis=this.node.getComponent(UITransform).width/2 let sData=this.ent.get(SDataCom) let fac=sData.fac let enemys:any=[] if(fac==BoxSet.HERO){ enemys=ecs.query(ecs.allOf(MonMoveComp)) }else{ enemys=ecs.query(ecs.allOf(HeroViewComp)) } let IRTargets: HeroViewComp[] = [] // 收集范围内所有敌方目标 enemys.some(e => { const view = e.get(HeroViewComp); const distance = Math.abs(this.node.position.x - view.node.position.x); if(distance <= dis) { IRTargets.push(view); } }); // 根据配置的hit_num决定攻击模式 const hitNum = SkillSet[this.s_uuid].hit_num || 0; if(hitNum > 0) { // 限制目标数量:按距离排序,选择最近的N个目标 if(IRTargets.length > 0) { // 按距离排序(从近到远) IRTargets.sort((a, b) => { const distanceA = Math.abs(this.node.position.x - a.node.position.x); const distanceB = Math.abs(this.node.position.x - b.node.position.x); return distanceA - distanceB; }); // 限制目标数量 const maxTargets = Math.min(hitNum, IRTargets.length); const sTargets = IRTargets.slice(0, maxTargets); sTargets.forEach(target => { this.apply_damage(target, false); }); } } else { // 范围伤害:对所有范围内目标造成伤害 if(IRTargets.length > 0) { IRTargets.forEach(target => { this.apply_damage(target, true); }); } } } //伤害应用 apply_damage(target:HeroViewComp,is_range:boolean=false){ if(target == null) return; if (!this.SConf) return; let sData=this.ent.get(SDataCom) //伤害处理 target.ent.add(DmgDataCom) let dmgData=target.ent.get(DmgDataCom) dmgData.Attrs=sData.Attrs dmgData.caster=sData.caster dmgData.s_uuid=sData.s_uuid console.log("[SkillCom]:apply_damage",target,sData,dmgData) sData.hit_count++ // console.log("[SkillCom]:碰撞次数:技能次数:穿刺次数",this.hit_count,this.Config.hit,this.puncture) if(sData.hit_count>=(this.SConf.hit+sData.Attrs[Attrs.PUNCTURE])&&(this.SConf.DTType!=DTType.range)&&(this.SConf.EType!=EType.animationEnd)&&(this.SConf.EType!=EType.timeEnd)) this.ent.destroy// 技能命中次数 } /** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */ reset() { this.node.destroy(); } }