heroesofabenez /
combat
| 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 | use HeroesofAbenez\Combat\Team; |
||
| 11 | |||
| 12 | 1 | final class Heal implements ICombatAction |
|
| 13 | { |
||
| 14 | public const ACTION_NAME = "healing"; |
||
| 15 | |||
| 16 | public function getName(): string |
||
| 17 | { |
||
| 18 | 1 | return self::ACTION_NAME; |
|
| 19 | } |
||
| 20 | |||
| 21 | public function getPriority(): int |
||
| 22 | { |
||
| 23 | 1 | return 10000; |
|
| 24 | } |
||
| 25 | |||
| 26 | public function shouldUse(CombatBase $combat, Character $character): bool |
||
| 27 | { |
||
| 28 | 1 | return (in_array($character, $this->findHealers($combat)->toArray(), true) && $this->selectHealingTarget($character, $combat) !== null); |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 29 | } |
||
| 30 | |||
| 31 | public function do(CombatBase $combat, Character $character): void |
||
| 32 | { |
||
| 33 | 1 | $result = []; |
|
| 34 | /** @var Character $patient */ |
||
| 35 | 1 | $patient = $this->selectHealingTarget($character, $combat); |
|
| 36 | 1 | $result["result"] = $combat->successCalculator->hasHealed($character); |
|
| 37 | 1 | $amount = ($result["result"]) ? (int) ($character->intelligence / 2) : 0; |
|
| 38 | 1 | $result["amount"] = Numbers::range($amount, 0, $patient->maxHitpoints - $patient->hitpoints); |
|
| 39 | 1 | if ($result["amount"] > 0) { |
|
| 40 | 1 | $patient->heal($result["amount"]); |
|
| 41 | } |
||
| 42 | 1 | $result["action"] = $this->getName(); |
|
| 43 | 1 | $result["name"] = ""; |
|
| 44 | 1 | $result["character1"] = $character; |
|
| 45 | 1 | $result["character2"] = $patient; |
|
| 46 | 1 | $combat->log->log($result); |
|
| 47 | 1 | } |
|
| 48 | |||
| 49 | private function findHealers(CombatBase $combat): Team |
||
| 50 | { |
||
| 51 | 1 | $healers = call_user_func($combat->healers, $combat->team1, $combat->team2); |
|
| 52 | 1 | if ($healers instanceof Team) { |
|
| 53 | 1 | return $healers; |
|
| 54 | } |
||
| 55 | return new Team("healers"); |
||
| 56 | } |
||
| 57 | |||
| 58 | private function selectHealingTarget(Character $healer, CombatBase $combat): ?Character |
||
| 59 | { |
||
| 60 | 1 | return $combat->getTeam($healer)->getLowestHpCharacter(); |
|
| 61 | } |
||
| 62 | } |
||
| 63 |