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\CharacterAttackSkill; |
||
9 | use HeroesofAbenez\Combat\ICombatAction; |
||
10 | use HeroesofAbenez\Combat\SkillAttack as Skill; |
||
11 | use HeroesofAbenez\Combat\NotImplementedException; |
||
12 | use Nexendrie\Utils\Numbers; |
||
13 | |||
14 | 1 | final class SkillAttack implements ICombatAction { |
|
15 | public const ACTION_NAME = "skill_attack"; |
||
16 | |||
17 | public function getName(): string { |
||
18 | 1 | return self::ACTION_NAME; |
|
19 | } |
||
20 | |||
21 | public function getPriority(): int { |
||
22 | 1 | return 1001; |
|
23 | } |
||
24 | |||
25 | public function shouldUse(CombatBase $combat, Character $character): bool { |
||
26 | $attackTarget = $combat->selectAttackTarget($character); |
||
27 | if($attackTarget === null) { |
||
28 | return false; |
||
29 | } |
||
30 | if(count($character->usableSkills) < 1) { |
||
31 | return false; |
||
32 | } |
||
33 | return ($character->usableSkills[0] instanceof CharacterAttackSkill); |
||
34 | } |
||
35 | |||
36 | protected function doSingleAttack(Character $attacker, Character $defender, CharacterAttackSkill $skill, CombatBase $combat): void { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
37 | 1 | $result = []; |
|
38 | 1 | $result["result"] = $combat->successCalculator->hasHit($attacker, $defender, $skill); |
|
39 | 1 | $result["amount"] = 0; |
|
40 | 1 | if($result["result"]) { |
|
41 | 1 | $amount = (int) (($attacker->damage - $defender->defense) / 100 * $skill->damage); |
|
42 | 1 | $result["amount"] = Numbers::range($amount, 0, $defender->hitpoints); |
|
43 | } |
||
44 | 1 | if($result["amount"] > 0) { |
|
45 | 1 | $defender->harm($result["amount"]); |
|
46 | } |
||
47 | 1 | $result["action"] = $this->getName(); |
|
48 | 1 | $result["name"] = $skill->skill->name; |
|
49 | 1 | $result["character1"] = $attacker; |
|
50 | 1 | $result["character2"] = $defender; |
|
51 | 1 | $combat->logDamage($attacker, $result["amount"]); |
|
52 | 1 | $combat->log->log($result); |
|
53 | 1 | $skill->resetCooldown(); |
|
54 | 1 | } |
|
55 | |||
56 | /** |
||
57 | * @throws NotImplementedException |
||
58 | */ |
||
59 | public function do(CombatBase $combat, Character $character): void { |
||
60 | /** @var CharacterAttackSkill $skill */ |
||
61 | 1 | $skill = $character->usableSkills[0]; |
|
62 | /** @var Character $primaryTarget */ |
||
63 | 1 | $primaryTarget = $combat->selectAttackTarget($character); |
|
64 | 1 | $targets = match($skill->skill->target) { |
|
65 | 1 | Skill::TARGET_SINGLE => [$primaryTarget], |
|
66 | Skill::TARGET_ROW => $combat->getTeam($primaryTarget)->getItems(["positionRow" => $primaryTarget->positionRow]), |
||
67 | Skill::TARGET_COLUMN => $combat->getTeam($primaryTarget)->getItems(["positionColumn" => $primaryTarget->positionColumn]), |
||
0 ignored issues
–
show
|
|||
68 | default => throw new NotImplementedException("Target {$skill->skill->target} for attack skills is not implemented."), |
||
0 ignored issues
–
show
|
|||
69 | }; |
||
70 | 1 | foreach($targets as $target) { |
|
71 | 1 | for($i = 1; $i <= $skill->skill->strikes; $i++) { |
|
72 | 1 | if($target->hitpoints > 0) { |
|
73 | 1 | $this->doSingleAttack($character, $target, $skill, $combat); |
|
74 | } |
||
75 | } |
||
76 | } |
||
77 | 1 | } |
|
78 | } |
||
79 | ?> |