refactor: reuse position distance for attacks
This commit is contained in:
@@ -53,8 +53,7 @@ final class CombatEngine
|
|||||||
throw new CombatException('Target must be an active enemy unit.');
|
throw new CombatException('Target must be an active enemy unit.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$distance = abs($attacker->position->x - $target->position->x)
|
$distance = $attacker->position->distanceTo($target->position);
|
||||||
+ abs($attacker->position->y - $target->position->y);
|
|
||||||
|
|
||||||
if ($distance !== 1) {
|
if ($distance !== 1) {
|
||||||
throw new CombatException('Target is outside attack range.');
|
throw new CombatException('Target is outside attack range.');
|
||||||
|
|||||||
@@ -418,6 +418,25 @@ final class CombatEngineTest extends TestCase
|
|||||||
yield 'defeated enemy' => ['bravo', 0];
|
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
|
public function testAttackAlwaysDealsAtLeastOneDamage(): void
|
||||||
{
|
{
|
||||||
$match = new MatchState(
|
$match = new MatchState(
|
||||||
|
|||||||
Reference in New Issue
Block a user