|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vehsamrak\Terraformator\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Vehsamrak\Terraformator\Exception\InvalidTypeException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Vehsamrak |
|
10
|
|
|
*/ |
|
11
|
|
|
class Map extends ArrayCollection |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritDoc} |
|
16
|
|
|
*/ |
|
17
|
16 |
|
public function __construct(array $locations = []) |
|
18
|
|
|
{ |
|
19
|
16 |
|
parent::__construct($locations); |
|
20
|
16 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** {@inheritDoc} */ |
|
23
|
12 |
|
public function add($element): bool |
|
24
|
|
|
{ |
|
25
|
12 |
|
if (!$element instanceof Location) { |
|
26
|
1 |
|
throw new InvalidTypeException(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
11 |
|
return parent::add($element); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
2 |
|
public function getPreviousLocations(int $x, int $y): PreviousLocationMap |
|
33
|
|
|
{ |
|
34
|
|
|
// filtering north, north-west, north-east and west locations by X and Y |
|
35
|
2 |
|
$previousLocations = $this->filter( |
|
36
|
2 |
|
function (Location $location) use ($x, $y) { |
|
37
|
2 |
|
$westLocationX = $x - 1; |
|
38
|
2 |
|
$westLocationY = $y; |
|
39
|
2 |
|
$northWestLocationX = $x - 1; |
|
40
|
2 |
|
$northWestLocationY = $y - 1; |
|
41
|
2 |
|
$northLocationX = $x; |
|
42
|
2 |
|
$northLocationY = $y - 1; |
|
43
|
2 |
|
$northEastLocationX = $x + 1; |
|
44
|
2 |
|
$northEastLocationY = $y - 1; |
|
45
|
|
|
|
|
46
|
2 |
|
$locationX = $location->getX(); |
|
47
|
2 |
|
$locationY = $location->getY(); |
|
48
|
|
|
|
|
49
|
2 |
|
return ($locationX === $westLocationX && $locationY === $westLocationY) || |
|
50
|
2 |
|
($locationX === $northWestLocationX && $locationY === $northWestLocationY) || |
|
51
|
2 |
|
($locationX === $northLocationX && $locationY === $northLocationY) || |
|
52
|
2 |
|
($locationX === $northEastLocationX && $locationY === $northEastLocationY); |
|
53
|
|
|
|
|
54
|
2 |
|
} |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
2 |
|
return new PreviousLocationMap($previousLocations->getValues()); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|