|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace HeroesofAbenez\Combat; |
|
5
|
|
|
|
|
6
|
|
|
use Nexendrie\Utils\Collection, |
|
7
|
|
|
Nexendrie\Utils\Numbers; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Structure for a team in combat |
|
11
|
|
|
* |
|
12
|
|
|
* @author Jakub Konečný |
|
13
|
|
|
* @property-read string $name |
|
14
|
|
|
* @property-read Character[] $aliveMembers |
|
15
|
|
|
* @property-read Character[] $usableMembers |
|
16
|
|
|
* @property int $maxRowSize |
|
17
|
|
|
*/ |
|
18
|
1 |
|
final class Team extends Collection { |
|
19
|
|
|
protected $class = Character::class; |
|
20
|
|
|
/** @var string Name of the team */ |
|
21
|
|
|
protected $name; |
|
22
|
|
|
/** @var int */ |
|
23
|
|
|
protected $maxRowSize = 5; |
|
24
|
|
|
|
|
25
|
1 |
|
use \Nette\SmartObject; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(string $name) { |
|
28
|
1 |
|
parent::__construct(); |
|
29
|
1 |
|
$this->name = $name; |
|
30
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getName(): string { |
|
33
|
1 |
|
return $this->name; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getMaxRowSize(): int { |
|
37
|
1 |
|
return $this->maxRowSize; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function setMaxRowSize(int $maxRowSize): void { |
|
41
|
1 |
|
$this->maxRowSize = Numbers::range($maxRowSize, 1, PHP_INT_MAX); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get alive members from the team |
|
46
|
|
|
* |
|
47
|
|
|
* @return Character[] |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getAliveMembers(): array { |
|
50
|
1 |
|
return $this->getItems(["hitpoints>" => 0,]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get members which can perform an action |
|
55
|
|
|
* |
|
56
|
|
|
* @return Character[] |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getUsableMembers(): array { |
|
59
|
1 |
|
return $this->getItems([ |
|
60
|
1 |
|
"stunned" => false, "hitpoints>" => 0, |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check whether the team has at least 1 alive member |
|
66
|
|
|
*/ |
|
67
|
|
|
public function hasAliveMembers(): bool { |
|
68
|
1 |
|
return count($this->getAliveMembers()) > 0; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set character's position in team |
|
73
|
|
|
* |
|
74
|
|
|
* @param string|int $id |
|
75
|
|
|
* @throws \OutOfBoundsException |
|
76
|
|
|
* @throws InvalidCharacterPositionException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setCharacterPosition($id, int $row, int $column): void { |
|
79
|
1 |
|
if(!$this->hasItems(["id" => $id])) { |
|
80
|
1 |
|
throw new \OutOfBoundsException("Character $id is not in the team"); |
|
81
|
1 |
|
} elseif(count($this->getItems(["positionRow" => $row])) >= $this->maxRowSize) { |
|
82
|
1 |
|
throw new InvalidCharacterPositionException("Row $row is full.", InvalidCharacterPositionException::ROW_FULL); |
|
83
|
1 |
|
} elseif($this->hasItems(["positionRow" => $row, "positionColumn" => $column])) { |
|
84
|
1 |
|
throw new InvalidCharacterPositionException("Row $row column $column is occupied.", InvalidCharacterPositionException::POSITION_OCCUPIED); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
1 |
|
$character = $this->getItems(["id" => $id])[0]; |
|
87
|
1 |
|
$character->positionRow = $row; |
|
88
|
1 |
|
$character->positionColumn = $column; |
|
89
|
1 |
|
} |
|
90
|
|
|
} |
|
91
|
|
|
?> |