Przeglądaj źródła

add func to fill matrixes with tmx map

Bastien Sevajol 6 lat temu
rodzic
commit
e1dc5493d6
2 zmienionych plików z 54 dodań i 0 usunięć
  1. 17 0
      synergine2_xyz/tmx_utils.py
  2. 37 0
      tests/test_tmx.py

+ 17 - 0
synergine2_xyz/tmx_utils.py Wyświetl plik

@@ -1,8 +1,12 @@
1 1
 # coding: utf-8
2
+import typing
3
+
2 4
 from tmx import TileMap
3 5
 from tmx import Layer
4 6
 
5 7
 from synergine2.exceptions import ConfigurationError
8
+from synergine2_xyz.map import TMXMap
9
+from synergine2_xyz.physics import Matrixes
6 10
 
7 11
 
8 12
 def get_layer_by_name(map_: TileMap, layer_name: str) -> Layer:
@@ -11,3 +15,16 @@ def get_layer_by_name(map_: TileMap, layer_name: str) -> Layer:
11 15
             return layer
12 16
 
13 17
     raise ConfigurationError('No layer named "{}" in map')
18
+
19
+
20
+def fill_matrix(
21
+    tmx_map: TMXMap,
22
+    matrixes: Matrixes,
23
+    layer_name: str,
24
+    matrix_name: str,
25
+    properties: typing.List[str],
26
+) -> None:
27
+    for tile_xy, tile in tmx_map.tmx_layer_tiles[layer_name].items():
28
+        x, y = map(int, tile_xy.split('.'))
29
+        values = [tile.property(p_name) for p_name in properties]
30
+        matrixes.update_matrix(matrix_name, value=tuple(values), x=x, y=y)

+ 37 - 0
tests/test_tmx.py Wyświetl plik

@@ -0,0 +1,37 @@
1
+# coding: utf-8
2
+from synergine2_xyz.map import TMXMap
3
+from synergine2_xyz.physics import Matrixes
4
+from synergine2_xyz.tmx_utils import fill_matrix
5
+from tests import BaseTest
6
+
7
+
8
+class TestVisibilityMatrix(BaseTest):
9
+    def test_tmx_to_matrix(self):
10
+        tmx_map = TMXMap('tests/fixtures/map_001.tmx')
11
+        matrixes = Matrixes()
12
+
13
+        assert 5 == tmx_map.width
14
+        assert 5 == tmx_map.height
15
+
16
+        matrixes.initialize_empty_matrix(
17
+            'visibility',
18
+            matrix_height=5,
19
+            matrix_width=5,
20
+            value_structure=['height', 'opacity'],
21
+        )
22
+        assert [
23
+            [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), ],
24
+            [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), ],
25
+            [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), ],
26
+            [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), ],
27
+            [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), ],
28
+        ] == matrixes.get_matrix('visibility')
29
+
30
+        fill_matrix(tmx_map, matrixes, 'terrain', 'visibility', ['height', 'opacity'])
31
+        assert [
32
+            [(0.0, 0.0), (0.0, 0.0),   (0.0, 0.0),   (0.0, 0.0),   (0.0, 0.0), ],
33
+            [(0.0, 0.0), (2.0, 100.0), (2.0, 100.0), (2.0, 100.0), (0.0, 0.0), ],
34
+            [(0.0, 0.0), (2.0, 100.0), (0.0, 0.0),   (2.0, 100.0), (0.0, 0.0), ],
35
+            [(0.0, 0.0), (2.0, 100.0), (2.0, 100.0), (2.0, 100.0), (0.0, 0.0), ],
36
+            [(0.0, 0.0), (0.0, 0.0),   (0.0, 0.0),   (0.0, 0.0),   (0.0, 0.0), ],
37
+        ] == matrixes.get_matrix('visibility')