-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.php
93 lines (74 loc) · 2.34 KB
/
Path.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
class Path
{
public const STATUS_DRIVING = 'driving';
public const STATUS_WALKING = 'walking';
public const STATUS_FINISHED = 'finished';
private int $xPos;
private int $yPos;
private string $direction;
private array $coords = [];
private array $instructions = [];
private string $status = self::STATUS_WALKING;
private array $firstCoords = [];
public function __construct(string $direction, int $x, int $y) {
$this->direction = $direction;
$this->xPos = $x;
$this->yPos = $y;
}
public function getDirection(): string
{
return $this->direction;
}
public function canMove(MovementOption $move): bool
{
return !in_array($this->toCoords($move->getTile()), $this->coords, true);
}
public function addMove(MovementOption $move): self
{
$newPath = clone $this;
$newPath->instructions[] = $move->convertToInstruction();
$newPath->direction = $move->getDirection();
$newPath->coords[] = $this->toCoords($move->getTile());
$newPath->xPos = $move->getTile()->getX();
$newPath->yPos = $move->getTile()->getY();
if ($move->getTile() instanceof Van) {
$newPath->firstCoords = $newPath->coords;
$newPath->coords = []; //Reset prev. path to prevent blocking
$newPath->status = self::STATUS_DRIVING;
}
if ($move->getTile() instanceof Gate) {
$newPath->status = self::STATUS_FINISHED;
}
return $newPath;
}
private function toCoords(TileInterface $tile): string
{
return sprintf('%s-%s', $tile->getY(), $tile->getX());
}
public function getInstructions(): string
{
return implode('', $this->instructions);
}
public function getX(): int
{
return $this->xPos;
}
public function getY(): int
{
return $this->yPos;
}
public function getStatus(): string
{
return $this->status;
}
public function contains($tile): bool
{
return in_array($this->toCoords($tile), $this->coords, true) || in_array($this->toCoords($tile), $this->firstCoords, true);
}
public function getChar($tile): string
{
return in_array($this->toCoords($tile), $this->coords, true) ? '~' : '*';
}
}