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!");
@@ -105,12 +111,12 @@ export class TargetingSystem {
// Only remove if it's a consumable throwable
if (item.type === "Consumable" && item.throwable) {
// Handle stack decrement if applicable, or remove
if (item.quantity && item.quantity > 1) {
item.quantity--;
} else {
player.inventory.items.splice(itemIdx, 1);
}
// Handle stack decrement if applicable, or remove
if (item.quantity && item.quantity > 1) {
item.quantity--;
} else {
player.inventory.items.splice(itemIdx, 1);
}
}
const start = player.pos;
@@ -235,14 +241,14 @@ export class TargetingSystem {
let currentDist = 0;
while (currentDist < distance) {
const len = Math.min(dashLen, distance - currentDist);
const sx = x1 + currentDist * cos;
const sy = y1 + currentDist * sin;
const ex = sx + len * cos;
const ey = sy + len * sin;
const len = Math.min(dashLen, distance - currentDist);
const sx = x1 + currentDist * cos;
const sy = y1 + currentDist * sin;
const ex = sx + len * cos;
const ey = sy + len * sin;
this.graphics.lineBetween(sx, sy, ex, ey);
currentDist += dashLen + gapLen;
this.graphics.lineBetween(sx, sy, ex, ey);
currentDist += dashLen + gapLen;
}
}
}

View File

@@ -3,34 +3,34 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
// Mock Phaser
vi.mock('phaser', () => {
const mockGraphics = {
setDepth: vi.fn().mockReturnThis(),
clear: vi.fn().mockReturnThis(),
lineStyle: vi.fn().mockReturnThis(),
lineBetween: vi.fn().mockReturnThis(),
};
const mockGraphics = {
setDepth: vi.fn().mockReturnThis(),
clear: vi.fn().mockReturnThis(),
lineStyle: vi.fn().mockReturnThis(),
lineBetween: vi.fn().mockReturnThis(),
};
const mockSprite = {
setDepth: vi.fn().mockReturnThis(),
setVisible: vi.fn().mockReturnThis(),
setAlpha: vi.fn().mockReturnThis(),
setPosition: vi.fn().mockReturnThis(),
};
const mockSprite = {
setDepth: vi.fn().mockReturnThis(),
setVisible: vi.fn().mockReturnThis(),
setAlpha: vi.fn().mockReturnThis(),
setPosition: vi.fn().mockReturnThis(),
};
return {
default: {
GameObjects: {
Sprite: vi.fn(() => mockSprite),
Graphics: vi.fn(() => mockGraphics),
},
Scene: class {
add = {
graphics: vi.fn(() => mockGraphics),
sprite: vi.fn(() => mockSprite),
};
}
}
};
return {
default: {
GameObjects: {
Sprite: vi.fn(() => mockSprite),
Graphics: vi.fn(() => mockGraphics),
},
Scene: class {
add = {
graphics: vi.fn(() => mockGraphics),
sprite: vi.fn(() => mockSprite),
};
}
}
};
});
// Mock CombatLogic
@@ -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();
});
});