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