Make it so you cant shoot yourself

This commit is contained in:
2026-01-28 18:19:46 +11:00
parent 5d33d0e660
commit 90aebc892a
2 changed files with 84 additions and 49 deletions

View File

@@ -94,6 +94,12 @@ export class TargetingSystem {
const player = accessor.getCombatant(playerId);
if (!player || !player.inventory) return false;
// Prevent targeting self
if (this.cursor.x === player.pos.x && this.cursor.y === player.pos.y) {
console.log("Cannot target self!");
return false;
}
const itemIdx = player.inventory.items.findIndex(it => it.id === this.targetingItemId);
if (itemIdx === -1) {
console.log("Item not found!");

View File

@@ -171,4 +171,33 @@ describe('TargetingSystem', () => {
expect(mockGraphics.clear).toHaveBeenCalled();
expect(mockSprite.setVisible).toHaveBeenCalledWith(false);
});
it('should prevent targeting self', () => {
const playerPos = { x: 1, y: 1 };
// Setup targeting
targetingSystem.startTargeting(
'item-1',
playerPos,
mockWorld,
{ getCombatant: vi.fn().mockReturnValue({ pos: playerPos, inventory: { items: [{ id: 'item-1' }] } }) } as any,
1 as EntityId,
new Uint8Array(100),
10
);
// Manually set cursor to player pos (startTargeting might do it, but we ensure it)
targetingSystem.updateCursor(playerPos, playerPos);
const callback = vi.fn();
const result = targetingSystem.executeThrow(
mockWorld,
1 as EntityId,
{ getCombatant: vi.fn().mockReturnValue({ pos: playerPos, inventory: { items: [{ id: 'item-1' }] } }) } as any,
callback
);
expect(result).toBe(false);
expect(callback).not.toHaveBeenCalled();
});
});