|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AardsGerds\Game\Dialog; |
|
6
|
|
|
|
|
7
|
|
|
use AardsGerds\Game\Entity\Entity; |
|
8
|
|
|
use AardsGerds\Game\Player\Player; |
|
9
|
|
|
use AardsGerds\Game\Player\PlayerAction; |
|
10
|
|
|
|
|
11
|
|
|
final class Dialog |
|
12
|
|
|
{ |
|
13
|
|
|
public function __construct( |
|
14
|
|
|
private Player $player, |
|
15
|
|
|
private Entity $npc, |
|
16
|
|
|
private NpcDialogOption $dialogOption, |
|
17
|
|
|
) {} |
|
18
|
|
|
|
|
19
|
|
|
public function __invoke(PlayerAction $playerAction): void |
|
20
|
|
|
{ |
|
21
|
|
|
$playerAction->tell("{$this->npc->getName()}: {$this->dialogOption}"); |
|
22
|
|
|
|
|
23
|
|
|
while (!$this->dialogOption->getResponses()->isEmpty()) { |
|
24
|
|
|
$this->playerDialog($playerAction, $this->dialogOption->getResponses()); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private function playerDialog(PlayerAction $playerAction, PlayerDialogOptionCollection $dialogOptions): void |
|
29
|
|
|
{ |
|
30
|
|
|
$playerDialogChoice = $playerAction->askForDialogChoice($dialogOptions); |
|
31
|
|
|
|
|
32
|
|
|
$playerAction->tell("{$this->player->getName()}: {$playerDialogChoice}"); |
|
33
|
|
|
$this->npcDialog($playerAction, $playerDialogChoice->getResponse()); |
|
34
|
|
|
|
|
35
|
|
|
if ($playerDialogChoice instanceof QuitDialogOption) { |
|
36
|
|
|
$this->dialogOption->getResponses()->clear(); |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->dialogOption->getResponses()->remove($playerDialogChoice); |
|
41
|
|
|
|
|
42
|
|
|
if ($playerDialogChoice instanceof FightDialogOption) { |
|
43
|
|
|
$playerAction->askForConfirmation('Continue?'); |
|
44
|
|
|
$playerDialogChoice->getEvent()($this->player, $playerAction); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function npcDialog(PlayerAction $playerAction, NpcDialogOption $dialogOption): void |
|
49
|
|
|
{ |
|
50
|
|
|
$playerAction->tell("{$this->npc->getName()}: {$dialogOption}"); |
|
51
|
|
|
|
|
52
|
|
|
if ($dialogOption->getResponses()->isEmpty()) { |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->playerDialog($playerAction, $dialogOption->getResponses()); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|