|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\Ship\Lib\Movement\Component; |
|
6
|
|
|
|
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Stu\Component\Ship\ShipEnum; |
|
9
|
|
|
use Stu\Orm\Entity\MapInterface; |
|
10
|
|
|
use Stu\Orm\Entity\ShipInterface; |
|
11
|
|
|
use Stu\Orm\Entity\StarSystemMapInterface; |
|
12
|
|
|
|
|
13
|
|
|
final class UpdateFlightDirection implements UpdateFlightDirectionInterface |
|
14
|
|
|
{ |
|
15
|
5 |
|
public function updateWhenTraversing( |
|
16
|
|
|
MapInterface|StarSystemMapInterface $oldWaypoint, |
|
17
|
|
|
MapInterface|StarSystemMapInterface $waypoint, |
|
18
|
|
|
ShipInterface $ship |
|
19
|
|
|
): int { |
|
20
|
|
|
|
|
21
|
5 |
|
$startX = $oldWaypoint->getX(); |
|
22
|
5 |
|
$startY = $oldWaypoint->getY(); |
|
23
|
|
|
|
|
24
|
5 |
|
$destinationX = $waypoint->getX(); |
|
25
|
5 |
|
$destinationY = $waypoint->getY(); |
|
26
|
|
|
|
|
27
|
5 |
|
$flightDirection = null; |
|
28
|
|
|
|
|
29
|
5 |
|
if ($destinationX === $startX) { |
|
30
|
3 |
|
$oldy = $startY; |
|
31
|
3 |
|
if ($destinationY > $oldy) { |
|
32
|
1 |
|
$flightDirection = ShipEnum::DIRECTION_BOTTOM; |
|
33
|
2 |
|
} elseif ($destinationY < $oldy) { |
|
34
|
1 |
|
$flightDirection = ShipEnum::DIRECTION_TOP; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
5 |
|
if ($destinationY === $startY) { |
|
38
|
3 |
|
$oldx = $startX; |
|
39
|
3 |
|
if ($destinationX > $oldx) { |
|
40
|
1 |
|
$flightDirection = ShipEnum::DIRECTION_RIGHT; |
|
41
|
2 |
|
} elseif ($destinationX < $oldx) { |
|
42
|
1 |
|
$flightDirection = ShipEnum::DIRECTION_LEFT; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
if ($flightDirection === null) { |
|
47
|
1 |
|
throw new RuntimeException('this should not happen'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
$ship->setFlightDirection($flightDirection); |
|
51
|
|
|
|
|
52
|
4 |
|
return $flightDirection; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function updateWhenSystemExit(ShipInterface $ship, StarSystemMapInterface $starsystemMap): void |
|
56
|
|
|
{ |
|
57
|
|
|
$system = $starsystemMap->getSystem(); |
|
58
|
|
|
|
|
59
|
|
|
$shipX = $starsystemMap->getSx(); |
|
60
|
|
|
$shipY = $starsystemMap->getSy(); |
|
61
|
|
|
|
|
62
|
|
|
$rad12or34 = atan($shipY / $shipX); |
|
63
|
|
|
$rad14or23 = atan(($system->getMaxX() - $shipX) / $shipY); |
|
64
|
|
|
|
|
65
|
|
|
if ($rad12or34 < M_PI_4) { |
|
66
|
|
|
$flightDirection = $rad14or23 < M_PI_4 ? ShipEnum::DIRECTION_LEFT : ShipEnum::DIRECTION_BOTTOM; |
|
67
|
|
|
} elseif ($rad14or23 < M_PI_4) { |
|
68
|
|
|
$flightDirection = ShipEnum::DIRECTION_TOP; |
|
69
|
|
|
} else { |
|
70
|
|
|
$flightDirection = ShipEnum::DIRECTION_RIGHT; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$ship->setFlightDirection($flightDirection); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|