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\ICombatAction; |
9
|
|
|
use Nexendrie\Utils\Numbers; |
10
|
|
|
|
11
|
1 |
|
final class Attack implements ICombatAction { |
12
|
1 |
|
use \Nette\SmartObject; |
13
|
|
|
|
14
|
|
|
public const ACTION_NAME = "attack"; |
15
|
|
|
|
16
|
|
|
public function getName(): string { |
17
|
1 |
|
return static::ACTION_NAME; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getPriority(): int { |
21
|
1 |
|
return 0; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function shouldUse(CombatBase $combat, Character $character): bool { |
25
|
1 |
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Do an attack |
30
|
|
|
* Hit chance = Attacker's hit - Defender's dodge, but at least 15% |
31
|
|
|
* Damage = Attacker's damage - defender's defense |
32
|
|
|
*/ |
33
|
|
|
public function do(CombatBase $combat, Character $character): void { |
34
|
1 |
|
$result = []; |
35
|
1 |
|
$defender = $combat->selectAttackTarget($character); |
36
|
1 |
|
if(is_null($defender)) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
1 |
|
$result["result"] = $combat->successCalculator->hasHit($character, $defender); |
40
|
1 |
|
$result["amount"] = 0; |
41
|
1 |
|
if($result["result"]) { |
42
|
1 |
|
$amount = $character->damage - $defender->defense; |
43
|
1 |
|
$result["amount"] = Numbers::range($amount, 0, $defender->hitpoints); |
44
|
|
|
} |
45
|
1 |
|
if($result["amount"] > 0) { |
46
|
1 |
|
$defender->harm($result["amount"]); |
47
|
|
|
} |
48
|
1 |
|
$result["action"] = $this->getName(); |
49
|
1 |
|
$result["name"] = ""; |
50
|
1 |
|
$result["character1"] = $character; |
51
|
1 |
|
$result["character2"] = $defender; |
52
|
1 |
|
$combat->logDamage($character, $result["amount"]); |
53
|
1 |
|
$combat->log->log($result); |
54
|
1 |
|
} |
55
|
|
|
} |
56
|
|
|
?> |