-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomainLogic.php
More file actions
57 lines (46 loc) · 1.97 KB
/
DomainLogic.php
File metadata and controls
57 lines (46 loc) · 1.97 KB
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
<?php
// класс для поиска пути Astar
class DomainLogic implements DomainLogicInterface
{
private static array $_tiles;
public function __construct()
{
}
// вообще PHP nr массив передает по ссылки и выделяет под него память если меняется значение только
public static function init(array &$tiles)
{
PHP::log('Инициализация матрицы поиска пути');
static::$_tiles = $tiles;
}
/**
* @param Coordinate $node
* @return Coordinate[]
*/
public function getAdjacentNodes(mixed $node): iterable
{
if(empty(static::$_tiles[$node]))
{
throw new Error('имеется ссылка на область '.$node.', но в матрице она отсутствует');
}
return array_keys(static::$_tiles[$node]);
}
// здесь можно влхвоащать сложность прохода по этому определенному маршруту из определеннйо клетки
public function calculateRealCost(mixed $node, mixed $destination): float | int
{
if (!isset(static::$_tiles[$node][$destination]))
{
throw new DomainException('из локации '.$node.' отсутствует путь в '.$destination);
}
// если есть кто то живой кроме нас сложность прохода этим путем увеличивается
return static::$_tiles[$node][$destination];
}
public function calculateEstimatedCost(mixed $fromNode, mixed $toNode): float | int
{
$from = explode(Position::DELIMETR, $fromNode);
$to = explode(Position::DELIMETR, $toNode);
$xFactor = ($from[0] - $to[0]) ** 2;
$yFactor = ($from[1] - $to[1]) ** 2;
$factor = sqrt($xFactor + $yFactor);
return $factor;
}
}