1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Ship\Lib\Crew; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Stu\Component\Ship\ShipRumpEnum; |
9
|
|
|
use Stu\Orm\Entity\ShipInterface; |
10
|
|
|
use Stu\Orm\Repository\MapRepositoryInterface; |
11
|
|
|
use Stu\Orm\Repository\ShipRepositoryInterface; |
12
|
|
|
use Stu\Orm\Repository\ShipRumpRepositoryInterface; |
13
|
|
|
use Stu\Orm\Repository\StarSystemMapRepositoryInterface; |
14
|
|
|
use Stu\Orm\Repository\UserRepositoryInterface; |
15
|
|
|
|
16
|
|
|
//TODO unit tests |
17
|
|
|
final class LaunchEscapePods implements LaunchEscapePodsInterface |
18
|
|
|
{ |
19
|
|
|
private ShipRepositoryInterface $shipRepository; |
20
|
|
|
|
21
|
|
|
private UserRepositoryInterface $userRepository; |
22
|
|
|
|
23
|
|
|
private ShipRumpRepositoryInterface $shipRumpRepository; |
24
|
|
|
|
25
|
|
|
private StarSystemMapRepositoryInterface $starSystemMapRepository; |
26
|
|
|
|
27
|
|
|
private MapRepositoryInterface $mapRepository; |
28
|
|
|
|
29
|
|
|
private EntityManagerInterface $entityManager; |
30
|
|
|
|
31
|
1 |
|
public function __construct( |
32
|
|
|
ShipRepositoryInterface $shipRepository, |
33
|
|
|
UserRepositoryInterface $userRepository, |
34
|
|
|
ShipRumpRepositoryInterface $shipRumpRepository, |
35
|
|
|
StarSystemMapRepositoryInterface $starSystemMapRepository, |
36
|
|
|
MapRepositoryInterface $mapRepository, |
37
|
|
|
EntityManagerInterface $entityManager |
38
|
|
|
) { |
39
|
1 |
|
$this->shipRepository = $shipRepository; |
40
|
1 |
|
$this->userRepository = $userRepository; |
41
|
1 |
|
$this->shipRumpRepository = $shipRumpRepository; |
42
|
1 |
|
$this->starSystemMapRepository = $starSystemMapRepository; |
43
|
1 |
|
$this->mapRepository = $mapRepository; |
44
|
1 |
|
$this->entityManager = $entityManager; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function launch(ShipInterface $ship): ?ShipInterface |
48
|
|
|
{ |
49
|
|
|
$shipRump = $this->shipRumpRepository->find($ship->getUser()->getFactionId() + ShipRumpEnum::SHIP_RUMP_BASE_ID_ESCAPE_PODS); |
50
|
|
|
|
51
|
|
|
// faction does not have escape pods |
52
|
|
|
if ($shipRump == null) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$pods = $this->shipRepository->prototype(); |
57
|
|
|
$pods->setUser($this->userRepository->getFallbackUser()); |
58
|
|
|
$pods->setRump($shipRump); |
59
|
|
|
$pods->setName(sprintf(_('Rettungskapseln von (%d)'), $ship->getId())); |
60
|
|
|
$pods->setHuell(1); |
61
|
|
|
$pods->setMaxHuell(1); |
62
|
|
|
$pods->setAlertStateGreen(); |
63
|
|
|
|
64
|
|
|
$pods->updateLocation($ship->getMap(), $ship->getStarsystemMap()); |
65
|
|
|
|
66
|
|
|
//return to save place |
67
|
|
|
$this->returnToSafety($pods, $ship); |
68
|
|
|
|
69
|
|
|
$this->shipRepository->save($pods); |
70
|
|
|
$this->entityManager->flush(); |
71
|
|
|
return $pods; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function returnToSafety(ShipInterface $pods, ShipInterface $ship): void |
75
|
|
|
{ |
76
|
|
|
$field = $pods->getCurrentMapField(); |
77
|
|
|
|
78
|
|
|
if ($field->getFieldType()->getSpecialDamage()) { |
79
|
|
|
$met = 'fly' . $ship->getFlightDirection(); |
80
|
|
|
$newXY = $this->$met($pods); |
81
|
|
|
|
82
|
|
|
if ($pods->getSystem() !== null) { |
83
|
|
|
$map = $this->starSystemMapRepository->getByCoordinates( |
84
|
|
|
$pods->getSystem()->getId(), |
85
|
|
|
$newXY[0], |
86
|
|
|
$newXY[1] |
87
|
|
|
); |
88
|
|
|
$pods->setStarsystemMap($map); |
89
|
|
|
} else { |
90
|
|
|
$map = $this->mapRepository->getByCoordinates( |
91
|
|
|
$pods->getLayerId(), |
92
|
|
|
$newXY[0], |
93
|
|
|
$newXY[1] |
94
|
|
|
); |
95
|
|
|
$pods->setMap($map); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
//flee upwards |
101
|
|
|
/** |
102
|
|
|
* @return array<int> |
103
|
|
|
*/ |
104
|
|
|
private function fly2(ShipInterface $pods): array |
105
|
|
|
{ |
106
|
|
|
return [$pods->getPosX(), $pods->getPosY() - 1]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
//flee downwards |
110
|
|
|
/** |
111
|
|
|
* @return array<int> |
112
|
|
|
*/ |
113
|
|
|
private function fly4(ShipInterface $pods): array |
114
|
|
|
{ |
115
|
|
|
return [$pods->getPosX(), $pods->getPosY() + 1]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
//flee right |
119
|
|
|
/** |
120
|
|
|
* @return array<int> |
121
|
|
|
*/ |
122
|
|
|
private function fly3(ShipInterface $pods): array |
123
|
|
|
{ |
124
|
|
|
return [$pods->getPosX() - 1, $pods->getPosY()]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//flee left |
128
|
|
|
/** |
129
|
|
|
* @return array<int> |
130
|
|
|
*/ |
131
|
|
|
private function fly1(ShipInterface $pods): array |
132
|
|
|
{ |
133
|
|
|
return [$pods->getPosX() + 1, $pods->getPosY()]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|