Ensure projectiles dont get embedded in walls or blocking tiles

This commit is contained in:
2026-01-18 13:44:48 +11:00
parent 6447f01c77
commit 064952f254
2 changed files with 59 additions and 9 deletions

View File

@@ -0,0 +1,50 @@
import { describe, it, expect } from 'vitest';
import { traceProjectile } from '../gameplay/CombatLogic';
import { EntityManager } from '../EntityManager';
import { type World, type Actor, type EntityId } from '../../core/types';
const createTestWorld = (actors: Map<EntityId, Actor>): World => {
return {
width: 10,
height: 10,
tiles: new Array(100).fill(0), // 0 = Floor
actors,
exit: { x: 9, y: 9 }
};
};
describe('Throwing Mechanics', () => {
it('should land ON the wall currently (demonstrating the bug)', () => {
const actors = new Map<EntityId, Actor>();
const world = createTestWorld(actors);
const entityManager = new EntityManager(world);
// Wall at (5, 0)
world.tiles[5] = 4; // Wall
const start = { x: 0, y: 0 };
const target = { x: 5, y: 0 }; // Target the wall directly
const result = traceProjectile(world, start, target, entityManager);
// NEW BEHAVIOR: blockedPos is the tile BEFORE the wall (4, 0)
expect(result.blockedPos).toEqual({ x: 4, y: 0 });
});
it('should land ON the wall when throwing PAST a wall (demonstrating the bug)', () => {
const actors = new Map<EntityId, Actor>();
const world = createTestWorld(actors);
const entityManager = new EntityManager(world);
// Wall at (3, 0)
world.tiles[3] = 4; // Wall
const start = { x: 0, y: 0 };
const target = { x: 5, y: 0 }; // Target past the wall
const result = traceProjectile(world, start, target, entityManager);
// NEW BEHAVIOR: Hits the wall at 3,0, stops at 2,0
expect(result.blockedPos).toEqual({ x: 2, y: 0 });
});
});

View File

@@ -38,7 +38,7 @@ export function traceProjectile(
blockedPos = p;
} else {
// Hit wall or other obstacle
blockedPos = p;
blockedPos = points[i - 1];
}
break;
}
@@ -84,7 +84,7 @@ export function getClosestVisibleEnemy(
const dx = actor.pos.x - origin.x;
const dy = actor.pos.y - origin.y;
const distSq = dx*dx + dy*dy;
const distSq = dx * dx + dy * dy;
if (distSq < closestDistSq) {
closestDistSq = distSq;