Highlight active item slot and activate when shortcut key is pressed
This commit is contained in:
@@ -51,3 +51,46 @@ export function traceProjectile(
|
||||
hitActorId
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the closest visible enemy to a given position.
|
||||
*/
|
||||
export function getClosestVisibleEnemy(
|
||||
world: World,
|
||||
origin: Vec2,
|
||||
seenTiles: Set<string> | boolean[] | Uint8Array, // Support various visibility structures
|
||||
width?: number // Required if seenTiles is a flat array
|
||||
): Vec2 | null {
|
||||
let closestDistSq = Infinity;
|
||||
let closestPos: Vec2 | null = null;
|
||||
|
||||
// Helper to check visibility
|
||||
const isVisible = (x: number, y: number) => {
|
||||
if (Array.isArray(seenTiles) || seenTiles instanceof Uint8Array || seenTiles instanceof Int8Array) {
|
||||
// Flat array
|
||||
if (!width) return false;
|
||||
return (seenTiles as any)[y * width + x];
|
||||
} else {
|
||||
// Set<string>
|
||||
return (seenTiles as Set<string>).has(`${x},${y}`);
|
||||
}
|
||||
};
|
||||
|
||||
for (const actor of world.actors.values()) {
|
||||
if (actor.category !== "combatant" || actor.isPlayer) continue;
|
||||
|
||||
// Check visibility
|
||||
if (!isVisible(actor.pos.x, actor.pos.y)) continue;
|
||||
|
||||
const dx = actor.pos.x - origin.x;
|
||||
const dy = actor.pos.y - origin.y;
|
||||
const distSq = dx*dx + dy*dy;
|
||||
|
||||
if (distSq < closestDistSq) {
|
||||
closestDistSq = distSq;
|
||||
closestPos = { x: actor.pos.x, y: actor.pos.y };
|
||||
}
|
||||
}
|
||||
|
||||
return closestPos;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user