|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Component\Colony\Shields; |
|
6
|
|
|
|
|
7
|
|
|
use Stu\Component\Building\BuildingEnum; |
|
8
|
|
|
use Stu\Component\Colony\ColonyFunctionManagerInterface; |
|
9
|
|
|
use Stu\Lib\Colony\PlanetFieldHostInterface; |
|
10
|
|
|
use Stu\Orm\Entity\ColonyInterface; |
|
11
|
|
|
use Stu\Orm\Repository\PlanetFieldRepositoryInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Provides shielding related methods |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ColonyShieldingManager implements ColonyShieldingManagerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private PlanetFieldRepositoryInterface $planetFieldRepository; |
|
19
|
|
|
|
|
20
|
|
|
private ColonyFunctionManagerInterface $colonyFunctionManager; |
|
21
|
|
|
|
|
22
|
|
|
private PlanetFieldHostInterface $host; |
|
23
|
|
|
|
|
24
|
5 |
|
public function __construct( |
|
25
|
|
|
PlanetFieldRepositoryInterface $planetFieldRepository, |
|
26
|
|
|
ColonyFunctionManagerInterface $colonyFunctionManager, |
|
27
|
|
|
PlanetFieldHostInterface $host |
|
28
|
|
|
) { |
|
29
|
5 |
|
$this->planetFieldRepository = $planetFieldRepository; |
|
30
|
5 |
|
$this->colonyFunctionManager = $colonyFunctionManager; |
|
31
|
5 |
|
$this->host = $host; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function updateActualShields(): void |
|
35
|
|
|
{ |
|
36
|
|
|
if (!$this->host instanceof ColonyInterface) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$shieldState = false; |
|
41
|
|
|
$shields = 0; |
|
42
|
|
|
|
|
43
|
|
|
foreach ($this->host->getPlanetFields() as $field) { |
|
44
|
|
|
$building = $field->getBuilding(); |
|
45
|
|
|
|
|
46
|
|
|
if ($building === null || !$field->isActive()) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$functions = $building->getFunctions(); |
|
51
|
|
|
|
|
52
|
|
|
if ($functions->containsKey(BuildingEnum::BUILDING_FUNCTION_SHIELD_GENERATOR)) { |
|
53
|
|
|
$shields += BuildingEnum::SHIELD_GENERATOR_CAPACITY; |
|
54
|
|
|
$shieldState = true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($functions->containsKey(BuildingEnum::BUILDING_FUNCTION_SHIELD_BATTERY)) { |
|
58
|
|
|
$shields += BuildingEnum::SHIELD_BATTERY_CAPACITY; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($shieldState) { |
|
63
|
|
|
$this->host->setShields(min($this->host->getShields(), $shields)); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function hasShielding(): bool |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->colonyFunctionManager->hasFunction( |
|
70
|
1 |
|
$this->host, |
|
71
|
1 |
|
BuildingEnum::BUILDING_FUNCTION_SHIELD_GENERATOR |
|
72
|
1 |
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function getMaxShielding(): int |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->planetFieldRepository->getMaxShieldsOfColony($this->host); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
public function isShieldingEnabled(): bool |
|
81
|
|
|
{ |
|
82
|
3 |
|
return $this->colonyFunctionManager->hasActiveFunction($this->host, BuildingEnum::BUILDING_FUNCTION_SHIELD_GENERATOR) |
|
83
|
3 |
|
&& ($this->host instanceof ColonyInterface ? $this->host->getShields() > 0 : true); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|