Spaces:
Running
Running
File size: 2,571 Bytes
9e6ef9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
export function findNearestWithinRange(tower, enemies) {
let nearest = null;
let nearestDistSq = Infinity;
for (const e of enemies) {
const dSq = e.mesh.position.distanceToSquared(tower.position);
if (dSq <= tower.range * tower.range && dSq < nearestDistSq) {
nearest = e;
nearestDistSq = dSq;
}
}
return nearest;
}
export function findClosestToExitInRange(tower, enemies) {
const inRange = [];
const rangeSq = tower.range * tower.range;
for (const e of enemies) {
const dSq = e.mesh.position.distanceToSquared(tower.position);
if (dSq <= rangeSq) {
inRange.push(e);
}
}
if (inRange.length === 0) return null;
const towerPos = tower.position;
inRange.sort((a, b) => {
const segA = a.currentSeg ?? 0;
const segB = b.currentSeg ?? 0;
if (segA !== segB) return segB - segA;
const remA = a.target
? a.target.distanceTo(a.position ?? a.mesh.position)
: Infinity;
const remB = b.target
? b.target.distanceTo(b.position ?? b.mesh.position)
: Infinity;
if (remA !== remB) return remA - remB;
const da = (a.mesh?.position || a.position).distanceTo(towerPos);
const db = (b.mesh?.position || b.position).distanceTo(towerPos);
return da - db;
});
return inRange[0] || null;
}
export function findMultipleInRangeOrdered(tower, enemies, priority = "nearest") {
const rangeSq = tower.range * tower.range;
const inRange = [];
for (const e of enemies) {
const dSq = e.mesh.position.distanceToSquared(tower.position);
if (dSq <= rangeSq) inRange.push(e);
}
if (inRange.length === 0) return [];
if (priority === "closestToExit") {
const towerPos = tower.position;
inRange.sort((a, b) => {
const segA = a.currentSeg ?? 0;
const segB = b.currentSeg ?? 0;
if (segA !== segB) return segB - segA;
const remA = a.target
? a.target.distanceTo(a.position ?? a.mesh.position)
: Infinity;
const remB = b.target
? b.target.distanceTo(b.position ?? b.mesh.position)
: Infinity;
if (remA !== remB) return remA - remB;
const da = (a.mesh?.position || a.position).distanceTo(towerPos);
const db = (b.mesh?.position || b.position).distanceTo(towerPos);
return da - db;
});
} else {
const towerPos = tower.position;
inRange.sort((a, b) => {
const da = (a.mesh?.position || a.position).distanceTo(towerPos);
const db = (b.mesh?.position || b.position).distanceTo(towerPos);
return da - db;
});
}
return inRange;
} |