Passed
Push — master ( 0577a9...ac39e5 )
by Paweł
03:28
created

Dialog::npcDialog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 2
crap 6
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