|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AardsGerds\Game\Fight; |
|
6
|
|
|
|
|
7
|
|
|
use AardsGerds\Game\Build\Attribute\Damage; |
|
8
|
|
|
use AardsGerds\Game\Build\Attribute\Health; |
|
9
|
|
|
use AardsGerds\Game\Inventory\Usable; |
|
10
|
|
|
use AardsGerds\Game\Player\Player; |
|
11
|
|
|
use AardsGerds\Game\Player\PlayerAction; |
|
12
|
|
|
use AardsGerds\Game\Player\PlayerException; |
|
13
|
|
|
use AardsGerds\Game\Shared\IntegerValue; |
|
14
|
|
|
use AardsGerds\Game\Shared\IntegerValueException; |
|
15
|
|
|
use function Lambdish\Phunctional\map; |
|
16
|
|
|
use function Lambdish\Phunctional\not; |
|
17
|
|
|
|
|
18
|
|
|
final class Fight |
|
19
|
|
|
{ |
|
20
|
|
|
private IntegerValue $round; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
private Player $player, |
|
24
|
|
|
private FighterCollection $opponents, |
|
25
|
|
|
private PlayerAction $playerAction, |
|
26
|
|
|
) { |
|
27
|
|
|
$this->round = new IntegerValue(1); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __invoke(): void |
|
31
|
|
|
{ |
|
32
|
|
|
while ($this->opponents->count() > 0) { |
|
33
|
|
|
$fighters = new FighterCollection( |
|
34
|
|
|
array_merge([$this->player], $this->opponents->getItems()), |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
$this->playerAction->newRound("Round {$this->round}:"); |
|
38
|
|
|
$this->playerAction->list(map( |
|
39
|
|
|
static fn(Fighter $fighter) => [$fighter->getName() => "{$fighter->getHealth()} health"], |
|
40
|
|
|
$fighters, |
|
41
|
|
|
)); |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
|
|
foreach ($fighters->order() as $fighter) { |
|
45
|
|
|
$this->action($fighter); |
|
46
|
|
|
} |
|
47
|
|
|
} catch (IntegerValueException $exception) { |
|
48
|
|
|
$isDead = static fn(Fighter $fighter): bool => $fighter->getHealth()->isLowerThan(new Health(1)); |
|
49
|
|
|
|
|
50
|
|
|
if ($isDead($this->player)) { |
|
51
|
|
|
throw PlayerException::death(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->opponents = $this->opponents->filter(not($isDead)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->round->increment(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function action(Fighter $fighter): void |
|
62
|
|
|
{ |
|
63
|
|
|
if ($fighter instanceof Player) { |
|
64
|
|
|
$action = $this->askForAction(); |
|
65
|
|
|
|
|
66
|
|
|
if ($action instanceof Usable) { |
|
67
|
|
|
$action->use($fighter, $this->playerAction); |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
} else { |
|
71
|
|
|
$action = $fighter->getTalents()->filterAttacks()->getIterator()->current(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
AttackAction::invoke($fighter, $this->findOpponent($fighter), $action, $this->playerAction); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function askForAction(): Attack|Usable |
|
78
|
|
|
{ |
|
79
|
|
|
$action = $this->playerAction->askForChoice( |
|
80
|
|
|
'Select action', |
|
81
|
|
|
array_merge($this->player->getTalents()->filterAttacks()->getItems(), ['Go to inventory']), |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
if ($action === 'Go to inventory') { |
|
85
|
|
|
$selectedItem = $this->playerAction->askForChoice( |
|
86
|
|
|
'Select item to use', |
|
87
|
|
|
array_merge($this->player->getInventory()->filterUsable()->getItems(), ['Back']), |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
if ($selectedItem === 'Back') { |
|
91
|
|
|
return $this->askForAction(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $selectedItem; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($action instanceof MeleeAttack && $this->player->getWeapon() === null) { |
|
98
|
|
|
$this->playerAction->note('This attack requires weapon equipped.'); |
|
99
|
|
|
return $this->askForAction(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($action instanceof EtherumAttack) { |
|
103
|
|
|
if ($this->player->getEtherum()->isLowerThan($action::getEtherumCost())) { |
|
104
|
|
|
$this->playerAction->note('You do not posses enough Etherum.'); |
|
105
|
|
|
return $this->askForAction(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->player->getEtherum()->decreaseBy($action::getEtherumCost()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $action; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function findOpponent(Fighter $fighter): Fighter |
|
115
|
|
|
{ |
|
116
|
|
|
if ($fighter instanceof Player) { |
|
117
|
|
|
return $this->opponents->count() > 1 |
|
118
|
|
|
? $this->playerAction->askForChoice('Select opponent to attack', $this->opponents->getItems()) |
|
119
|
|
|
: $this->opponents->getIterator()->current(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $this->player; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|