1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AardsGerds\Game\Fight; |
6
|
|
|
|
7
|
|
|
use AardsGerds\Game\Build\Attribute\Damage; |
8
|
|
|
use AardsGerds\Game\Player\PlayerAction; |
9
|
|
|
use AardsGerds\Game\Shared\IntegerValueException; |
10
|
|
|
|
11
|
|
|
final class AttackAction |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @throws IntegerValueException if target's health hits 0 |
15
|
|
|
*/ |
16
|
|
|
public static function invoke( |
17
|
|
|
Fighter $attacker, |
18
|
|
|
Fighter $target, |
19
|
|
|
Attack $attack, |
20
|
|
|
PlayerAction $playerAction, |
21
|
|
|
): void { |
22
|
|
|
if (self::blockOccurred($attacker, $target, $attack)) { |
23
|
|
|
$playerAction->tell("{$attacker->getName()} uses {$attack}, but {$target->getName()} has blocked this attack."); |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$damage = self::calculateDamage($attacker, $attack); |
28
|
|
|
|
29
|
|
|
try { |
30
|
|
|
$target->getHealth()->decreaseBy($damage); |
31
|
|
|
} catch (IntegerValueException $exception) { |
32
|
|
|
$playerAction->tell("{$attacker->getName()} uses {$attack} and deals {$damage} damage, which brings opponent to their knees!"); |
33
|
|
|
throw $exception; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$playerAction->tell("{$attacker->getName()} uses {$attack} and deals {$damage} damage."); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private static function calculateDamage( |
40
|
|
|
Fighter $attacker, |
41
|
|
|
Attack $attack, |
42
|
|
|
): Damage { |
43
|
|
|
$initialDamage = new Damage(0); |
44
|
|
|
|
45
|
|
|
return match (true) { |
46
|
|
|
$attack instanceof MeleeAttack => |
47
|
|
|
$initialDamage |
48
|
|
|
->increaseBy($attack->getDamage($attacker->getWeapon() |
|
|
|
|
49
|
|
|
?? throw FightException::weaponRequired())) |
50
|
|
|
->increaseBy($attacker->getStrength()), |
51
|
|
|
$attack instanceof EtherumAttack => $attack->getDamage(), |
52
|
|
|
default => $initialDamage, |
53
|
|
|
}; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private static function blockOccurred( |
57
|
|
|
Fighter $attacker, |
58
|
|
|
Fighter $target, |
59
|
|
|
Attack $attack, |
60
|
|
|
): bool { |
61
|
|
|
return self::occurred(Block::calculateChance($attacker, $target, $attack)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private static function occurred(float $chance): bool |
65
|
|
|
{ |
66
|
|
|
return mt_rand(1, 10000) <= $chance * 10000; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|