heroesofabenez /
combat
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace HeroesofAbenez\Combat; |
||
| 5 | |||
| 6 | use Nette\Bridges\ApplicationLatte\LatteFactory; |
||
| 7 | use Nette\Localization\Translator; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * TextCombatLogRender |
||
| 11 | * |
||
| 12 | * @property-write string $template |
||
| 13 | */ |
||
| 14 | 1 | final class TextCombatLogRender implements ICombatLogRender |
|
| 15 | { |
||
| 16 | use \Nette\SmartObject; |
||
| 17 | |||
| 18 | private \Latte\Engine $latte; |
||
| 19 | private string $template = __DIR__ . "/CombatLog.latte"; |
||
| 20 | |||
| 21 | 1 | public function __construct(LatteFactory $latteFactory, private readonly Translator $translator) |
|
| 22 | { |
||
| 23 | 1 | $this->latte = $latteFactory->create(); |
|
| 24 | 1 | } |
|
| 25 | |||
| 26 | /** |
||
| 27 | * @throws \RuntimeException |
||
| 28 | */ |
||
| 29 | protected function setTemplate(string $template): void |
||
| 30 | { |
||
| 31 | if (!is_file($template)) { |
||
| 32 | throw new \RuntimeException("File $template does not exist."); |
||
| 33 | } |
||
| 34 | $this->template = $template; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function render(array $params): string |
||
| 38 | { |
||
| 39 | 1 | $params["render"] = $this; |
|
| 40 | 1 | return $this->latte->renderToString($this->template, $params); |
|
| 41 | } |
||
| 42 | |||
| 43 | public function renderItem(CombatLogEntry|string $item): string |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 44 | { |
||
| 45 | 1 | if (!$item instanceof CombatLogEntry) { |
|
| 46 | 1 | return $item; |
|
| 47 | } |
||
| 48 | 1 | $character1 = $item->character1->name; |
|
| 49 | 1 | $character2 = $item->character2->name; |
|
| 50 | 1 | switch ($item->action) { |
|
| 51 | 1 | case CombatActions\Attack::ACTION_NAME: |
|
| 52 | 1 | $message = ($item->result) ? "combat.log.attackHits" : "combat.log.attackFails"; |
|
| 53 | 1 | $text = $this->translator->translate( |
|
| 54 | 1 | $message, |
|
| 55 | 1 | $item->amount, |
|
| 56 | 1 | ["character1" => $character1, "character2" => $character2,] |
|
| 57 | ); |
||
| 58 | 1 | if ($item->result && $item->character2->hitpoints < 1) { |
|
| 59 | $text .= $this->translator->translate("combat.log.characterFalls"); |
||
| 60 | } |
||
| 61 | 1 | return $text; |
|
| 62 | 1 | case CombatActions\SkillAttack::ACTION_NAME: |
|
| 63 | 1 | $message = ($item->result) ? "combat.log.specialAttackHits" : "combat.log.specialAttackFails"; |
|
| 64 | 1 | $text = $this->translator->translate( |
|
| 65 | 1 | $message, |
|
| 66 | 1 | $item->amount, |
|
| 67 | 1 | ["character1" => $character1, "character2" => $character2, "name" => $item->name,] |
|
| 68 | ); |
||
| 69 | 1 | if ($item->result && $item->character2->hitpoints < 1) { |
|
| 70 | $text .= $this->translator->translate("combat.log.characterFalls"); |
||
| 71 | } |
||
| 72 | 1 | return $text; |
|
| 73 | 1 | case CombatActions\SkillSpecial::ACTION_NAME: |
|
| 74 | 1 | $message = ($item->result) ? "combat.log.specialSkillSuccess" : "combat.log.specialSKillFailure"; |
|
| 75 | 1 | return $this->translator->translate( |
|
| 76 | 1 | $message, |
|
| 77 | 1 | 0, |
|
| 78 | 1 | ["character1" => $character1, "character2" => $character2, "name" => $item->name,] |
|
| 79 | ); |
||
| 80 | 1 | case CombatActions\Heal::ACTION_NAME: |
|
| 81 | 1 | $message = ($item->result) ? "combat.log.healingSuccess" : "combat.log.healingFailure"; |
|
| 82 | 1 | return $this->translator->translate( |
|
| 83 | 1 | $message, |
|
| 84 | 1 | $item->amount, |
|
| 85 | 1 | ["character1" => $character1, "character2" => $character2,] |
|
| 86 | ); |
||
| 87 | 1 | case CombatLogEntry::ACTION_POISON: |
|
| 88 | 1 | return $this->translator->translate("combat.log.poison", $item->amount, ["character1" => $character1]); |
|
| 89 | } |
||
| 90 | return ""; |
||
| 91 | } |
||
| 92 | } |
||
| 93 |