1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Lib\Map\VisualPanel\Layer\Render; |
6
|
|
|
|
7
|
|
|
use Stu\Lib\Map\VisualPanel\AbstractVisualPanel; |
8
|
|
|
use Stu\Lib\Map\VisualPanel\Layer\Data\CellDataInterface; |
9
|
|
|
use Stu\Lib\Map\VisualPanel\Layer\Data\ShipCountData; |
10
|
|
|
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerEnum; |
11
|
|
|
use Stu\Orm\Entity\ShipInterface; |
12
|
|
|
|
13
|
|
|
final class ShipCountLayerRenderer implements LayerRendererInterface |
14
|
|
|
{ |
15
|
|
|
private bool $showCloakedEverywhere; |
16
|
|
|
|
17
|
|
|
private ?ShipInterface $currentShip; |
18
|
|
|
|
19
|
40 |
|
public function __construct(bool $showCloakedEverywhere, ?ShipInterface $currentShip) |
20
|
|
|
{ |
21
|
40 |
|
$this->showCloakedEverywhere = $showCloakedEverywhere; |
22
|
40 |
|
$this->currentShip = $currentShip; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** @param ShipCountData $data */ |
26
|
40 |
|
public function render(CellDataInterface $data, AbstractVisualPanel $panel): string |
27
|
|
|
{ |
28
|
40 |
|
$displayCount = $this->getDisplayCount($data); |
29
|
40 |
|
if ($displayCount === null) { |
30
|
10 |
|
return ''; |
31
|
|
|
} |
32
|
|
|
|
33
|
30 |
|
return sprintf( |
34
|
30 |
|
'<div style="%s z-index: %d;" class="centered">%s</div>', |
35
|
30 |
|
$panel->getFontSize(), |
36
|
30 |
|
PanelLayerEnum::SHIP_COUNT->value, |
37
|
30 |
|
$displayCount |
38
|
30 |
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
40 |
|
private function getDisplayCount(ShipCountData $data): ?string |
42
|
|
|
{ |
43
|
40 |
|
if ($data->getShipCount() > 0) { |
44
|
1 |
|
return (string) $data->getShipCount(); |
45
|
|
|
} |
46
|
39 |
|
if ($data->hasCloakedShips()) { |
47
|
39 |
|
if ($this->showCloakedEverywhere) { |
48
|
1 |
|
return "?"; |
49
|
|
|
} |
50
|
|
|
|
51
|
38 |
|
$currentShip = $this->currentShip; |
52
|
|
|
|
53
|
|
|
if ( |
54
|
38 |
|
$currentShip !== null |
55
|
38 |
|
&& $currentShip->getTachyonState() |
56
|
38 |
|
&& abs($data->getPosX() - $currentShip->getPosX()) <= $this->getTachyonRange($currentShip) |
57
|
38 |
|
&& abs($data->getPosY() - $currentShip->getPosY()) <= $this->getTachyonRange($currentShip) |
58
|
|
|
) { |
59
|
28 |
|
return "?"; |
60
|
|
|
} |
61
|
|
|
} |
62
|
10 |
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
36 |
|
private function getTachyonRange(ShipInterface $ship): int |
66
|
|
|
{ |
67
|
36 |
|
return $ship->isBase() ? 7 : 3; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|