1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Stratadox\CardGame\Match; |
4
|
|
|
|
5
|
|
|
use Stratadox\CardGame\DomainEventRecorder; |
6
|
|
|
use Stratadox\CardGame\DomainEventRecording; |
7
|
|
|
|
8
|
|
|
final class Card implements DomainEventRecorder |
9
|
|
|
{ |
10
|
|
|
use DomainEventRecording; |
11
|
|
|
|
12
|
|
|
private $location; |
13
|
|
|
private $template; |
14
|
|
|
private $offset; |
15
|
|
|
|
16
|
|
|
private function __construct( |
17
|
|
|
Location $location, |
18
|
|
|
CardTemplate $template, |
19
|
|
|
int $offset |
20
|
|
|
) { |
21
|
|
|
$this->location = $location; |
22
|
|
|
$this->template = $template; |
23
|
|
|
$this->offset = $offset; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function inDeck(int $position, CardTemplate $template): self |
27
|
|
|
{ |
28
|
|
|
return new self(Location::inDeck($position), $template, $position); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function isInDeck(): bool |
32
|
|
|
{ |
33
|
|
|
return $this->location->isInDeck(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function isInHand(): bool |
37
|
|
|
{ |
38
|
|
|
return $this->location->isInHand(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function isInPlay(): bool |
42
|
|
|
{ |
43
|
|
|
return $this->location->isInPlay(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function isAttacking(): bool |
47
|
|
|
{ |
48
|
|
|
return $this->location->isAttacking(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isDefending(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->location->isDefending(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function isAttackingThe(Card $defender): bool |
57
|
|
|
{ |
58
|
|
|
return $this->location->isAttackingThe($defender->location); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function hasHigherPositionThan(Card $other): bool |
62
|
|
|
{ |
63
|
|
|
return $this->location->hasHigherPositionThan($other->location); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function draw(MatchId $match, int $position, int $player): void |
67
|
|
|
{ |
68
|
|
|
$this->location = $this->location->toHand($position); |
69
|
|
|
$this->happened( |
70
|
|
|
...$this->template->drawingEvents($match, $player, $this->offset) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function play(MatchId $match, int $position, int $player): void |
75
|
|
|
{ |
76
|
|
|
$this->location = $this->template->playingMove($position); |
77
|
|
|
$this->happened( |
78
|
|
|
...$this->template->playingEvents($match, $player, $this->offset) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function sendToAttack( |
83
|
|
|
MatchId $match, |
84
|
|
|
int $position, |
85
|
|
|
int $player |
86
|
|
|
): void { |
87
|
|
|
$this->location = $this->template->attackingMove($position); |
88
|
|
|
$this->happened( |
89
|
|
|
...$this->template->attackingEvents($match, $player, $this->offset) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function sendToDefendAgainst( |
94
|
|
|
MatchId $match, |
95
|
|
|
int $position, |
96
|
|
|
int $player |
97
|
|
|
): void { |
98
|
|
|
$this->location = $this->template->defendingMove($position); |
99
|
|
|
$this->happened( |
100
|
|
|
...$this->template->defendingEvents($match, $player, $this->offset) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function counterAttack( |
105
|
|
|
MatchId $match, |
106
|
|
|
Card $attacker, |
107
|
|
|
int $blockingPlayer, |
108
|
|
|
int $attackingPlayer |
109
|
|
|
): void { |
110
|
|
|
// @todo better combat mechanics |
111
|
|
|
if ($this->cost() > $attacker->cost()) { |
112
|
|
|
$attacker->location = Location::inVoid(); |
113
|
|
|
$this->happened(...$attacker->template->dyingEvents( |
114
|
|
|
$match, |
115
|
|
|
$attackingPlayer, |
116
|
|
|
$attacker->offset |
117
|
|
|
)); |
118
|
|
|
} else { |
119
|
|
|
$this->location = Location::inVoid(); |
120
|
|
|
$this->happened(...$this->template->dyingEvents( |
121
|
|
|
$match, |
122
|
|
|
$blockingPlayer, |
123
|
|
|
$this->offset |
124
|
|
|
)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function regroup(MatchId $match, int $position, int $player): void |
129
|
|
|
{ |
130
|
|
|
$this->location = $this->template->regroupingMove($position); |
131
|
|
|
$this->happened( |
132
|
|
|
...$this->template->regroupingEvents($match, $player, $this->offset) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function cost(): Mana |
137
|
|
|
{ |
138
|
|
|
return $this->template->cost(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|