1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Lib\Map\VisualPanel; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Stu\Orm\Entity\LayerInterface; |
9
|
|
|
use Stu\Orm\Entity\LocationInterface; |
10
|
|
|
use Stu\Orm\Entity\MapInterface; |
11
|
|
|
use Stu\Orm\Entity\StarSystemInterface; |
12
|
|
|
use Stu\Orm\Entity\StarSystemMapInterface; |
13
|
|
|
|
14
|
|
|
final class PanelBoundaries |
15
|
|
|
{ |
16
|
4 |
|
public function __construct(private int $minX, private int $maxX, private int $minY, private int $maxY, private LayerInterface|StarSystemInterface $parent) {} |
17
|
|
|
|
18
|
|
|
/** @return array<int> */ |
19
|
1 |
|
public function getColumnRange(): array |
20
|
|
|
{ |
21
|
1 |
|
return range($this->minX, $this->maxX); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @return array<int> */ |
25
|
1 |
|
public function getRowRange(): array |
26
|
|
|
{ |
27
|
1 |
|
return range($this->minY, $this->maxY); |
28
|
|
|
} |
29
|
|
|
|
30
|
4 |
|
public function getMinX(): int |
31
|
|
|
{ |
32
|
4 |
|
return $this->minX; |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
public function getMaxX(): int |
36
|
|
|
{ |
37
|
4 |
|
return $this->maxX; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
public function getMinY(): int |
41
|
|
|
{ |
42
|
4 |
|
return $this->minY; |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
public function getMaxY(): int |
46
|
|
|
{ |
47
|
4 |
|
return $this->maxY; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getParentId(): int |
51
|
|
|
{ |
52
|
|
|
return $this->parent->getId(); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
public function isOnMap(): bool |
56
|
|
|
{ |
57
|
4 |
|
return $this->parent instanceof LayerInterface; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array{minx: int, maxx: int, miny: int, maxy: int} $array |
62
|
|
|
*/ |
63
|
1 |
|
public static function fromArray(array $array, LayerInterface $layer): PanelBoundaries |
64
|
|
|
{ |
65
|
1 |
|
return new PanelBoundaries( |
66
|
1 |
|
$array['minx'], |
67
|
1 |
|
$array['maxx'], |
68
|
1 |
|
$array['miny'], |
69
|
1 |
|
$array['maxy'], |
70
|
1 |
|
$layer |
71
|
1 |
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public static function fromSystem(StarSystemInterface $system): PanelBoundaries |
75
|
|
|
{ |
76
|
1 |
|
return new PanelBoundaries( |
77
|
1 |
|
1, |
78
|
1 |
|
$system->getMaxX(), |
79
|
1 |
|
1, |
80
|
1 |
|
$system->getMaxY(), |
81
|
1 |
|
$system |
82
|
1 |
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
public static function fromLocation(LocationInterface $location, int $range): PanelBoundaries |
86
|
|
|
{ |
87
|
2 |
|
if ($location instanceof MapInterface) { |
88
|
1 |
|
return static::fromMap($location, $range); |
89
|
|
|
} |
90
|
1 |
|
if ($location instanceof StarSystemMapInterface) { |
91
|
1 |
|
return static::fromSystemMap($location, $range); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
throw new RuntimeException('unsupported location type'); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
private static function fromMap(MapInterface $map, int $range): PanelBoundaries |
98
|
|
|
{ |
99
|
1 |
|
$layer = $map->getLayer(); |
100
|
1 |
|
if ($layer === null) { |
101
|
|
|
throw new RuntimeException('this should not happen'); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return static::createLocationWithRange( |
105
|
1 |
|
$map, |
106
|
1 |
|
$layer->getWidth(), |
107
|
1 |
|
$layer->getHeight(), |
108
|
1 |
|
$layer, |
109
|
1 |
|
$range |
110
|
1 |
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
private static function fromSystemMap(StarSystemMapInterface $systemMap, int $range): PanelBoundaries |
114
|
|
|
{ |
115
|
1 |
|
return static::createLocationWithRange( |
116
|
1 |
|
$systemMap, |
117
|
1 |
|
$systemMap->getSystem()->getMaxX(), |
118
|
1 |
|
$systemMap->getSystem()->getMaxY(), |
119
|
1 |
|
$systemMap->getSystem(), |
120
|
1 |
|
$range |
121
|
1 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
private static function createLocationWithRange( |
125
|
|
|
LocationInterface $location, |
126
|
|
|
int $width, |
127
|
|
|
int $height, |
128
|
|
|
LayerInterface|StarSystemInterface $parent, |
129
|
|
|
int $range |
130
|
|
|
): PanelBoundaries { |
131
|
2 |
|
return new PanelBoundaries( |
132
|
2 |
|
max(1, $location->getX() - $range), |
133
|
2 |
|
min($width, $location->getX() + $range), |
134
|
2 |
|
max(1, $location->getY() - $range), |
135
|
2 |
|
min($height, $location->getY() + $range), |
136
|
2 |
|
$parent |
137
|
2 |
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|