refactor: reuse position distance for attacks

This commit is contained in:
Keith Solomon
2026-07-04 15:58:29 -05:00
parent 3c6359243a
commit 7588b304e1
2 changed files with 20 additions and 2 deletions
+1 -2
View File
@@ -53,8 +53,7 @@ final class CombatEngine
throw new CombatException('Target must be an active enemy unit.');
}
$distance = abs($attacker->position->x - $target->position->x)
+ abs($attacker->position->y - $target->position->y);
$distance = $attacker->position->distanceTo($target->position);
if ($distance !== 1) {
throw new CombatException('Target is outside attack range.');
+19
View File
@@ -418,6 +418,25 @@ final class CombatEngineTest extends TestCase
yield 'defeated enemy' => ['bravo', 0];
}
public function testAttackDealsCalculatedOpenTerrainDamage(): void
{
$target = $this->unit('bravo-1', 'bravo', new Position(1, 0), defense: 3);
$match = new MatchState(
new Battlefield(8, 8),
[
$this->unit('alpha-1', 'alpha', new Position(0, 0), attack: 8),
$target,
],
'alpha',
);
$next = (new CombatEngine())->attack($match, 'alpha-1', 'bravo-1');
self::assertSame(10, $target->health);
self::assertSame(5, $next->unit('bravo-1')->health);
self::assertSame(['alpha-1 attacked bravo-1 for 5 damage'], $next->actionLog);
}
public function testAttackAlwaysDealsAtLeastOneDamage(): void
{
$match = new MatchState(