|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace HeroesofAbenez\Combat\CombatActions; |
|
5
|
|
|
|
|
6
|
|
|
use HeroesofAbenez\Combat\Character; |
|
7
|
|
|
use HeroesofAbenez\Combat\CombatBase; |
|
8
|
|
|
use HeroesofAbenez\Combat\CombatLogEntry; |
|
9
|
|
|
use Nexendrie\Utils\Numbers; |
|
10
|
|
|
|
|
11
|
1 |
|
final class Attack implements ICombatAction { |
|
12
|
|
|
public function getName(): string { |
|
13
|
1 |
|
return CombatLogEntry::ACTION_ATTACK; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function getPriority(): int { |
|
17
|
1 |
|
return 0; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function shouldUse(CombatBase $combat, Character $character): bool { |
|
21
|
1 |
|
return true; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Do an attack |
|
26
|
|
|
* Hit chance = Attacker's hit - Defender's dodge, but at least 15% |
|
27
|
|
|
* Damage = Attacker's damage - defender's defense |
|
28
|
|
|
*/ |
|
29
|
|
|
public function do(CombatBase $combat, Character $character): void { |
|
30
|
1 |
|
$result = []; |
|
31
|
|
|
/** @var Character $defender */ |
|
32
|
1 |
|
$defender = $combat->selectAttackTarget($character); |
|
33
|
1 |
|
$result["result"] = $combat->successCalculator->hasHit($character, $defender); |
|
34
|
1 |
|
$result["amount"] = 0; |
|
35
|
1 |
|
if($result["result"]) { |
|
36
|
1 |
|
$amount = $character->damage - $defender->defense; |
|
37
|
1 |
|
$result["amount"] = Numbers::range($amount, 0, $defender->hitpoints); |
|
38
|
|
|
} |
|
39
|
1 |
|
if($result["amount"] > 0) { |
|
40
|
1 |
|
$defender->harm($result["amount"]); |
|
41
|
|
|
} |
|
42
|
1 |
|
$result["action"] = CombatLogEntry::ACTION_ATTACK; |
|
43
|
1 |
|
$result["name"] = ""; |
|
44
|
1 |
|
$result["character1"] = $character; |
|
45
|
1 |
|
$result["character2"] = $defender; |
|
46
|
1 |
|
$combat->logDamage($character, $result["amount"]); |
|
47
|
1 |
|
$combat->log->log($result); |
|
48
|
1 |
|
} |
|
49
|
|
|
} |
|
50
|
|
|
?> |