Passed
Pull Request — master (#1928)
by Janko
12:21
created

NavPanel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 1
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Map\NavPanel;
6
7
use RuntimeException;
8
use Stu\Orm\Entity\ShipInterface;
9
10
class NavPanel
11
{
12
    public function __construct(private ShipInterface $ship) {}
13
14
    public function getShip(): ShipInterface
15
    {
16
        return $this->ship;
17
    }
18
19
    /** @return array{cx: int, cy: int} */
20
    public function getShipPosition(): array
21
    {
22
        return [
23
            "cx" => $this->getShip()->getPosX(),
24
            "cy" => $this->getShip()->getPosY()
25
        ];
26
    }
27
28
    /** @return array{mx: int, my: int} */
29
    public function getMapBorders(): array
30
    {
31
        $starSystem = $this->getShip()->getSystem();
32
33
        if ($starSystem !== null) {
34
            return [
35
                "mx" => $starSystem->getMaxX(),
36
                "my" => $starSystem->getMaxY()
37
            ];
38
        }
39
40
        $layer = $this->getShip()->getLayer();
41
        if ($layer === null) {
42
            throw new RuntimeException('this should not happen');
43
        }
44
        return [
45
            "mx" => $layer->getWidth(),
46
            "my" => $layer->getHeight()
47
        ];
48
    }
49
50
    public function getLeft(): NavPanelButtonInterface
51
    {
52
        $coords = $this->getShipPosition();
53
        if ($coords['cx'] - 1 < 1) {
54
            return new NavPanelButton("-", true);
55
        }
56
        return new NavPanelButton(($coords['cx'] - 1) . "|" . $coords['cy']);
57
    }
58
59
    public function getRight(): NavPanelButtonInterface
60
    {
61
        $coords = $this->getShipPosition();
62
        $borders = $this->getMapBorders();
63
        if ($coords['cx'] + 1 > $borders['mx']) {
64
            return new NavPanelButton("-", true);
65
        }
66
        return new NavPanelButton(($coords['cx'] + 1) . "|" . $coords['cy']);
67
    }
68
69
    public function getUp(): NavPanelButtonInterface
70
    {
71
        $coords = $this->getShipPosition();
72
        if ($coords['cy'] - 1 < 1) {
73
            return new NavPanelButton("-", true);
74
        }
75
        return new NavPanelButton($coords['cx'] . "|" . ($coords['cy'] - 1));
76
    }
77
78
    public function getDown(): NavPanelButtonInterface
79
    {
80
        $coords = $this->getShipPosition();
81
        $borders = $this->getMapBorders();
82
        if ($coords['cy'] + 1 > $borders['my']) {
83
            return new NavPanelButton("-", true);
84
        }
85
        return new NavPanelButton($coords['cx'] . "|" . ($coords['cy'] + 1));
86
    }
87
}
88