技能目标位置 完善

This commit is contained in:
2025-07-08 10:44:28 +08:00
parent aa31ffb0e8
commit b2c49d978a

View File

@@ -1,4 +1,4 @@
import { _decorator, Component, Node, Vec3 } from 'cc'; import { _decorator, Component, Node, v3, Vec3 } from 'cc';
import { HeroViewComp } from './HeroViewComp'; import { HeroViewComp } from './HeroViewComp';
import { SkillSet, TGroup, TType } from '../common/config/SkillSet'; import { SkillSet, TGroup, TType } from '../common/config/SkillSet';
import { Skill } from '../skills/Skill'; import { Skill } from '../skills/Skill';
@@ -75,16 +75,16 @@ export class SkillConComp extends CCComp {
if (!this.node || !this.node.isValid || !this.HeroView || !this.HeroView.node || !this.HeroView.node.isValid) { if (!this.node || !this.node.isValid || !this.HeroView || !this.HeroView.node || !this.HeroView.node.isValid) {
return; return;
} }
let targets:any=null let target:any=null
switch(config.TGroup){ switch(config.TGroup){
case TGroup.Enemy: //单个敌人 case TGroup.Enemy: //单个敌人
targets = this.filterFrontRow(); target = this.filterFrontRow()
break break
case TGroup.Ally: //所有敌人 case TGroup.Ally: //所有敌人
targets = this.selectAllyTargets(); target = this.selectAllyTargets()
break break
case TGroup.Self: //自身 case TGroup.Self: //自身
targets = this.HeroView.ent target = this.node.position
break break
case TGroup.Team: //所有友方 case TGroup.Team: //所有友方
@@ -96,7 +96,7 @@ export class SkillConComp extends CCComp {
this.HeroView.playSkillEffect(config.uuid) this.HeroView.playSkillEffect(config.uuid)
const skillEntity = ecs.getEntity<Skill>(Skill); const skillEntity = ecs.getEntity<Skill>(Skill);
if (targets.length === 0) return;
const timerId = setTimeout(() => { const timerId = setTimeout(() => {
// 再次检查节点有效性 // 再次检查节点有效性
@@ -104,17 +104,12 @@ export class SkillConComp extends CCComp {
return; return;
} }
// 检查目标有效性
if (targets.length <= 0 || !targets[0]) return;
const targetView = targets[0].get(HeroViewComp);
if (!targetView || !targetView.node || !targetView.node.isValid) return;
skillEntity.load( skillEntity.load(
new Vec3(this.HeroView.node.position.x + BoxSet.ATK_X * this.HeroView.scale, new Vec3(this.HeroView.node.position.x + BoxSet.ATK_X * this.HeroView.scale,
this.HeroView.node.position.y + BoxSet.ATK_Y, 0), this.HeroView.node.position.y + BoxSet.ATK_Y, 0),
this.node.parent, this.node.parent,
config.uuid, config.uuid,
new Vec3(targetView.node.position.x, targetView.node.position.y, 0), new Vec3(target.x, target.y, 0),
this.HeroView, this.HeroView,
0, 0,
dmg dmg
@@ -143,31 +138,53 @@ export class SkillConComp extends CCComp {
/** 筛选最前排单位 */ /** 筛选最前排单位 */
private filterFrontRow(): ecs.Entity[] { private filterFrontRow(): Vec3{
// 敌方最前排是x坐标最大的我方最前排是x坐标最小的 // 敌方最前排是x坐标最大的我方最前排是x坐标最小的,若目标不存在,敌人 取400,我方取-400
let entities:any=null let entities:any=null
let pos=v3(0,0)
if(this.HeroView.fac==FacSet.HERO){ if(this.HeroView.fac==FacSet.HERO){
entities = ecs.query(ecs.allOf(MonModelComp)) entities = ecs.query(ecs.allOf(MonModelComp))
pos=v3(400,0)
}else{ }else{
entities = ecs.query(ecs.allOf(HeroModelComp)) entities = ecs.query(ecs.allOf(HeroModelComp))
pos=v3(-400,0)
} }
if(entities.length==0){
console.log("filterFrontRow 目标不存在",pos.x,pos.y)
return pos
}
let keyPos = this.HeroView.fac==FacSet.HERO ? let keyPos = this.HeroView.fac==FacSet.HERO ?
Math.min(...entities.map(e => e.get(HeroViewComp).node.position.x)) : Math.min(...entities.map(e => e.get(HeroViewComp).node.position.x)) :
Math.max(...entities.map(e => e.get(HeroViewComp).node.position.x)); Math.max(...entities.map(e => e.get(HeroViewComp).node.position.x));
pos=v3(keyPos,0)
return entities.filter(e => console.log("filterFrontRow 目标存在",pos.x,pos.y)
Math.abs(e.get(HeroViewComp).node.position.x - keyPos) < 10 return pos
)
} }
private selectAllyTargets( ): ecs.Entity[] { private selectAllyTargets( ): Vec3 {
// 敌方最前排是x坐标最大的+50我方最前排是x坐标最小的+50,若目标不存在,敌人 取320/2,我方取-320/2
let entities:any=null
let kp=50
let pos=v3(0,0)
if(this.HeroView.fac==FacSet.HERO){ if(this.HeroView.fac==FacSet.HERO){
return ecs.query(ecs.allOf(MonModelComp)) entities = ecs.query(ecs.allOf(MonModelComp))
pos=v3(320/2+kp,0)
}else{ }else{
return ecs.query(ecs.allOf(MonModelComp)) entities = ecs.query(ecs.allOf(HeroModelComp))
pos=v3(-320/2-kp,0)
kp=-50
} }
if(entities.length==0){
console.log("selectAllyTargets 目标不存在",pos)
return pos
}
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));
pos=v3(keyPos+kp,0)
console.log("selectAllyTargets 目标存在",pos)
return pos
} }
/** 随机选择目标 */ /** 随机选择目标 */