|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\Spacecraft\Lib\Movement\Route; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Stu\Component\Map\DirectionEnum; |
|
9
|
|
|
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface; |
|
10
|
|
|
use Stu\Orm\Entity\StarSystem; |
|
11
|
|
|
use Stu\Orm\Entity\StarSystemMap; |
|
12
|
|
|
use Stu\Orm\Repository\StarSystemMapRepositoryInterface; |
|
13
|
|
|
|
|
14
|
|
|
class RandomSystemEntry implements RandomSystemEntryInterface |
|
15
|
|
|
{ |
|
16
|
1 |
|
public function __construct(private StarSystemMapRepositoryInterface $starSystemMapRepository) {} |
|
17
|
|
|
|
|
18
|
1 |
|
#[\Override] |
|
19
|
|
|
public function getRandomEntryPoint(SpacecraftWrapperInterface $wrapper, StarSystem $system): StarSystemMap |
|
20
|
|
|
{ |
|
21
|
1 |
|
[$posx, $posy] = $this->getDestinationCoordinates($wrapper, $system); |
|
22
|
|
|
|
|
23
|
|
|
// the destination starsystem map field |
|
24
|
1 |
|
$starsystemMap = $this->starSystemMapRepository->getByCoordinates($system->getId(), $posx, $posy); |
|
25
|
|
|
|
|
26
|
1 |
|
if ($starsystemMap === null) { |
|
27
|
|
|
throw new RuntimeException('starsystem map is missing'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
return $starsystemMap; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return array{0: int,1: int} |
|
35
|
|
|
*/ |
|
36
|
1 |
|
private function getDestinationCoordinates(SpacecraftWrapperInterface $wrapper, StarSystem $system): array |
|
37
|
|
|
{ |
|
38
|
1 |
|
$flightDirection = $wrapper->getComputerSystemDataMandatory()->getFlightDirection(); |
|
39
|
1 |
|
while ($flightDirection === DirectionEnum::NON) { |
|
40
|
1 |
|
$flightDirection = DirectionEnum::from(random_int(1, 4)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
switch ($flightDirection) { |
|
44
|
1 |
|
case DirectionEnum::BOTTOM: |
|
45
|
1 |
|
$posx = random_int(1, $system->getMaxX()); |
|
46
|
1 |
|
$posy = 1; |
|
47
|
1 |
|
break; |
|
48
|
|
|
case DirectionEnum::TOP: |
|
49
|
|
|
$posx = random_int(1, $system->getMaxX()); |
|
50
|
|
|
$posy = $system->getMaxY(); |
|
51
|
|
|
break; |
|
52
|
|
|
case DirectionEnum::RIGHT: |
|
53
|
|
|
$posx = 1; |
|
54
|
|
|
$posy = random_int(1, $system->getMaxY()); |
|
55
|
|
|
break; |
|
56
|
|
|
case DirectionEnum::LEFT: |
|
57
|
|
|
$posx = $system->getMaxX(); |
|
58
|
|
|
$posy = random_int(1, $system->getMaxY()); |
|
59
|
|
|
break; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return [$posx, $posy]; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|