update(dt: number) {
// 获取敌人和箭头的位置
// 检查敌人是否在屏幕外 // 计算交点位置
const enemyPos=this.enemy.getWorldPosition()
const intersection = this.CalculateIntersectionBetter(enemyPos.x, enemyPos.y,750,1334);
//console.log(375,667)
// 更新箭头指示的位置
this.node.position =v3(intersection.x+this.pos.x,intersection.y+this.pos.y) ;
}
CalculateIntersectionBetter(x: number, y: number, width: number, height: number)
{
let worldPosition = new Vec2();
let aspectRatio = height / width;
let relativeY = y - height / 2;
let relativeX = x - width / 2;
let k = Math.floor(relativeY /relativeX);//GetSafeFloatDivisor : return value = value == 0 ? 0.01f : value;
if (y > height / 2)
{
if (x < width / 2)
{
if (-aspectRatio < k) //1
{
worldPosition.x = 0;
worldPosition.y = height / 2 + (y - (height / 2)) * (width / 2) / (width / 2 - x);
}
else //2
{
worldPosition.x = width / 2 + (x - (width / 2)) * (height / 2) / (y - height / 2);
worldPosition.y = height;
}
}
else
{
if (aspectRatio < k) //3
{
worldPosition.x = width / 2 + (x - (width / 2)) * (height / 2) / (y - height / 2);
worldPosition.y = height;
}
else //4
{
worldPosition.x = width;
worldPosition.y = height / 2 + (y - (height / 2)) * (width / 2) / (x - width / 2);
}
}
}
else
{
if (x > width / 2)
{
if (-aspectRatio < k) //5
{
worldPosition.x = width;
worldPosition.y = height / 2 + (y - (height / 2)) * (width / 2) / (x - width / 2);
}
else //6
{
worldPosition.y = 0;
worldPosition.x = width / 2 + (x - (width / 2)) * (height / 2) / (height / 2 - y);
}
}
else
{
if (aspectRatio < k) //7
{
worldPosition.y = 0;
worldPosition.x = width / 2 + (x - (width / 2)) * (height / 2) / (height / 2 - y);
}
else //8
{
worldPosition.x = 0;
worldPosition.y = height / 2 + (y - (height / 2)) * (width / 2) / (width / 2 - x);
}
}
}
return worldPosition;
}
}