技能目标位置 完善

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