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 $interlocutor, |
16
|
|
|
private DialogOption $dialogOption, |
17
|
|
|
private PlayerAction $playerAction, |
18
|
|
|
) {} |
19
|
|
|
|
20
|
|
|
public function __invoke(): void |
21
|
|
|
{ |
22
|
|
|
$this->playerAction->tell("{$this->interlocutor->getName()}: {$this->dialogOption}"); |
23
|
|
|
|
24
|
|
|
while (!$this->dialogOption->getResponses()->isEmpty()) { |
25
|
|
|
$this->playerDialog($this->dialogOption->getResponses()); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function playerDialog(PlayerDialogOptionCollection $dialogOptions): void |
30
|
|
|
{ |
31
|
|
|
$response = $this->playerAction->askForResponse($dialogOptions); |
32
|
|
|
|
33
|
|
|
$this->playerAction->tell("{$this->player->getName()}: {$response}"); |
34
|
|
|
$this->interlocutorDialog($response->getResponse()); |
35
|
|
|
|
36
|
|
|
if ($response instanceof QuitDialogOption) { |
37
|
|
|
$this->dialogOption->getResponses()->clear(); |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->dialogOption->getResponses()->remove($response); |
42
|
|
|
|
43
|
|
|
if ($response instanceof FightDialogOption) { |
44
|
|
|
$this->playerAction->askForConfirmation('Continue?'); |
45
|
|
|
$response->getEvent()($this->player, $this->playerAction); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function interlocutorDialog(DialogOption $dialogOption): void |
50
|
|
|
{ |
51
|
|
|
$this->playerAction->tell("{$this->interlocutor->getName()}: {$dialogOption}"); |
52
|
|
|
|
53
|
|
|
if ($dialogOption->getResponses()->isEmpty()) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->playerDialog($dialogOption->getResponses()); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|