|
@@ -1,22 +1,49 @@
|
1
|
1
|
|
2
|
2
|
import typing
|
3
|
3
|
|
4
|
|
-import tmx
|
5
|
4
|
from dijkstar import Graph
|
6
|
5
|
from dijkstar import find_path
|
7
|
6
|
|
8
|
7
|
from synergine2.config import Config
|
9
|
|
-from synergine2_xyz.map import TMXMap
|
10
|
|
-from synergine2_xyz.tmx_utils import get_layer_by_name
|
|
8
|
+from synergine2_xyz.map import TMXMap, XYZTile
|
|
9
|
+from synergine2_xyz.subjects import XYZSubject
|
|
10
|
+from synergine2_xyz.xyz import get_neighbor_positions
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+class MoveCostComputer(object):
|
|
14
|
+ def __init__(
|
|
15
|
+ self,
|
|
16
|
+ config: Config,
|
|
17
|
+ ) -> None:
|
|
18
|
+ self.config = config
|
|
19
|
+
|
|
20
|
+ def for_subject(self, subject: XYZSubject):
|
|
21
|
+
|
|
22
|
+ def move_cost_func(previous_node: str, next_node: str, tile: XYZTile, unknown):
|
|
23
|
+ return self.compute_move_cost(subject, tile, previous_node, next_node, unknown)
|
|
24
|
+ return move_cost_func
|
|
25
|
+
|
|
26
|
+ def compute_move_cost(
|
|
27
|
+ self,
|
|
28
|
+ subject: XYZSubject,
|
|
29
|
+ tile: XYZTile,
|
|
30
|
+ previous_node: str,
|
|
31
|
+ next_node: str,
|
|
32
|
+ unknown,
|
|
33
|
+ ) -> float:
|
|
34
|
+ return 1.0
|
11
|
35
|
|
12
|
36
|
|
13
|
37
|
class Physics(object):
|
|
38
|
+ move_cost_computer_class = MoveCostComputer
|
|
39
|
+
|
14
|
40
|
def __init__(
|
15
|
41
|
self,
|
16
|
42
|
config: Config,
|
17
|
43
|
) -> None:
|
18
|
44
|
self.config = config
|
19
|
45
|
self.graph = Graph()
|
|
46
|
+ self.move_cost_computer = self.move_cost_computer_class(config)
|
20
|
47
|
|
21
|
48
|
def load(self) -> None:
|
22
|
49
|
raise NotImplementedError()
|
|
@@ -28,11 +55,12 @@ class Physics(object):
|
28
|
55
|
self,
|
29
|
56
|
start: typing.Tuple[int, int],
|
30
|
57
|
end: typing.Tuple[int, int],
|
|
58
|
+ subject: XYZSubject,
|
31
|
59
|
) -> typing.List[typing.Tuple[int, int]]:
|
32
|
60
|
start_key = self.position_to_key(start)
|
33
|
61
|
end_key = self.position_to_key(end)
|
34
|
62
|
|
35
|
|
- found_path = find_path(self.graph, start_key, end_key)
|
|
63
|
+ found_path = find_path(self.graph, start_key, end_key, cost_func=self.move_cost_computer.for_subject(subject))
|
36
|
64
|
regular_path = []
|
37
|
65
|
for position in found_path[0][1:]:
|
38
|
66
|
x, y = map(int, position.split('.'))
|
|
@@ -42,6 +70,8 @@ class Physics(object):
|
42
|
70
|
|
43
|
71
|
|
44
|
72
|
class TMXPhysics(Physics):
|
|
73
|
+ tmx_map_class = TMXMap
|
|
74
|
+
|
45
|
75
|
def __init__(
|
46
|
76
|
self,
|
47
|
77
|
config: Config,
|
|
@@ -49,42 +79,28 @@ class TMXPhysics(Physics):
|
49
|
79
|
) -> None:
|
50
|
80
|
super().__init__(config)
|
51
|
81
|
self.map_file_path = map_file_path
|
|
82
|
+ self.tmx_map = self.tmx_map_class(map_file_path)
|
52
|
83
|
|
53
|
84
|
def load(self) -> None:
|
54
|
85
|
self.load_graph_from_map(self.map_file_path)
|
55
|
86
|
|
56
|
87
|
def load_graph_from_map(self, map_file_path: str) -> None:
|
57
|
|
- tmx_map = TMXMap(map_file_path)
|
58
|
|
-
|
59
|
88
|
|
60
|
|
- for y in range(tmx_map.height):
|
61
|
|
- for x in range(tmx_map.width):
|
|
89
|
+ for y in range(self.tmx_map.height):
|
|
90
|
+ for x in range(self.tmx_map.width):
|
62
|
91
|
position = self.position_to_key((x, y))
|
63
|
|
- neighbors = []
|
64
|
|
-
|
65
|
|
- for modifier_x, modifier_y in (
|
66
|
|
- (+1, +1),
|
67
|
|
- (+1, +0),
|
68
|
|
- (+1, -1),
|
69
|
|
- (+0, -1),
|
70
|
|
- (-1, -1),
|
71
|
|
- (-1, +0),
|
72
|
|
- (-1, -1),
|
73
|
|
- (+0, +1),
|
74
|
|
- ):
|
75
|
|
- try:
|
76
|
|
- neighbors.append('{}.{}'.format(x + modifier_x, y + modifier_y))
|
77
|
|
- except ValueError:
|
78
|
|
- pass
|
79
|
|
-
|
80
|
|
- for neighbor in neighbors:
|
81
|
|
- neighbor_x, neighbor_y = map(int, neighbor.split('.'))
|
82
|
|
-
|
83
|
|
- if neighbor_x > 69 or neighbor_x < 0:
|
|
92
|
+
|
|
93
|
+ for neighbor_position in get_neighbor_positions((x, y)):
|
|
94
|
+ neighbor = '{}.{}'.format(*neighbor_position)
|
|
95
|
+ neighbor_x, neighbor_y = neighbor_position
|
|
96
|
+
|
|
97
|
+ if neighbor_x > self.tmx_map.width-1 or neighbor_x < 0:
|
84
|
98
|
continue
|
85
|
99
|
|
86
|
|
- if neighbor_y > 69 or neighbor_y < 0:
|
|
100
|
+ if neighbor_y > self.tmx_map.height-1 or neighbor_y < 0:
|
87
|
101
|
continue
|
88
|
102
|
|
89
|
|
-
|
90
|
|
- self.graph.add_edge(position, neighbor, 1)
|
|
103
|
+
|
|
104
|
+ to_tile = self.tmx_map.layer_tiles('terrain')[neighbor]
|
|
105
|
+
|
|
106
|
+ self.graph.add_edge(position, neighbor, to_tile)
|