瀏覽代碼

Matrixes: define value structure

Bastien Sevajol 7 年之前
父節點
當前提交
b12c4ed018
共有 2 個文件被更改,包括 36 次插入13 次删除
  1. 20 4
      synergine2_xyz/physics.py
  2. 16 9
      tests/test_physics.py

+ 20 - 4
synergine2_xyz/physics.py 查看文件

36
         return 1.0
36
         return 1.0
37
 
37
 
38
 
38
 
39
-class VisibilityMatrix(object):
39
+class Matrixes(object):
40
     _matrixes = shared.create('matrixes', value=lambda: {})  # type: typing.Dict[str, typing.List[typing.List[tuple]]]
40
     _matrixes = shared.create('matrixes', value=lambda: {})  # type: typing.Dict[str, typing.List[typing.List[tuple]]]
41
 
41
 
42
-    def initialize_empty_matrix(self, name: str, matrix_width: int, matrix_height: int) -> None:
42
+    def __init__(self):
43
+        self._value_structures = {}  # type: typing.List[str]
44
+
45
+    def initialize_empty_matrix(
46
+        self,
47
+        name: str,
48
+        matrix_width: int,
49
+        matrix_height: int,
50
+        value_structure: typing.List[str],
51
+    ) -> None:
43
         self._matrixes[name] = []
52
         self._matrixes[name] = []
53
+        self._value_structures[name] = value_structure
44
 
54
 
45
         for y in range(matrix_height):
55
         for y in range(matrix_height):
46
             x_list = []
56
             x_list = []
47
             for x in range(matrix_width):
57
             for x in range(matrix_width):
48
-                x_list.append((0.0,))
58
+                x_list.append(tuple([0.0] * len(value_structure)))
49
             self._matrixes[name].append(x_list)
59
             self._matrixes[name].append(x_list)
50
 
60
 
51
     def get_matrix(self, name: str) -> typing.List[typing.List[tuple]]:
61
     def get_matrix(self, name: str) -> typing.List[typing.List[tuple]]:
73
             values.append(matrix[y][x])
83
             values.append(matrix[y][x])
74
         return values
84
         return values
75
 
85
 
86
+    def get_value(self, matrix_name: str, x: int, y: int, value_name: str) -> float:
87
+        matrix = self.get_matrix(matrix_name)
88
+        values = matrix[y][x]
89
+        value_position = self._value_structures[matrix_name].index(value_name)
90
+        return values[value_position]
91
+
76
 
92
 
77
 class Physics(object):
93
 class Physics(object):
78
-    visibility_matrix = VisibilityMatrix
94
+    visibility_matrix = Matrixes
79
     move_cost_computer_class = MoveCostComputer
95
     move_cost_computer_class = MoveCostComputer
80
 
96
 
81
     def __init__(
97
     def __init__(

+ 16 - 9
tests/test_physics.py 查看文件

1
 # coding: utf-8
1
 # coding: utf-8
2
 import pytest
2
 import pytest
3
 
3
 
4
-from synergine2_xyz.physics import VisibilityMatrix
4
+from synergine2_xyz.physics import Matrixes
5
 from tests import BaseTest
5
 from tests import BaseTest
6
 
6
 
7
 
7
 
8
 class TestVisibilityMatrix(BaseTest):
8
 class TestVisibilityMatrix(BaseTest):
9
     def test_initialize_empty_matrix(self):
9
     def test_initialize_empty_matrix(self):
10
-        visibility = VisibilityMatrix()
11
-        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2)
10
+        visibility = Matrixes()
11
+        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2, value_structure=['opacity'])
12
         matrix = visibility.get_matrix('testing')
12
         matrix = visibility.get_matrix('testing')
13
 
13
 
14
         assert isinstance(matrix, list)
14
         assert isinstance(matrix, list)
17
         assert [(0.0,), (0.0,), (0.0,)] == matrix[1]
17
         assert [(0.0,), (0.0,), (0.0,)] == matrix[1]
18
 
18
 
19
     def test_update_matrix(self):
19
     def test_update_matrix(self):
20
-        visibility = VisibilityMatrix()
21
-        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2)
20
+        visibility = Matrixes()
21
+        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2, value_structure=['opacity'])
22
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
22
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
23
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
23
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
24
         matrix = visibility.get_matrix('testing')
24
         matrix = visibility.get_matrix('testing')
27
         assert [(0.0,), (0.0,), (0.5,)] == matrix[1]
27
         assert [(0.0,), (0.0,), (0.5,)] == matrix[1]
28
 
28
 
29
     def test_get_path_positions(self):
29
     def test_get_path_positions(self):
30
-        visibility = VisibilityMatrix()
31
-        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2)
30
+        visibility = Matrixes()
31
+        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2, value_structure=['opacity'])
32
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
32
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
33
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
33
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
34
 
34
 
36
         assert [(0, 0), (1, 0), (2, 1)] == path_positions
36
         assert [(0, 0), (1, 0), (2, 1)] == path_positions
37
 
37
 
38
     def test_get_path_values(self):
38
     def test_get_path_values(self):
39
-        visibility = VisibilityMatrix()
40
-        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2)
39
+        visibility = Matrixes()
40
+        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2, value_structure=['opacity'])
41
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
41
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
42
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
42
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
43
 
43
 
44
         path_positions = visibility.get_path_positions(from_=(0, 0), to=(2, 1))
44
         path_positions = visibility.get_path_positions(from_=(0, 0), to=(2, 1))
45
         path_values = visibility.get_values_for_path('testing', path_positions=path_positions)
45
         path_values = visibility.get_values_for_path('testing', path_positions=path_positions)
46
         assert [(0.7,), (0.0,), (0.5,)] == path_values
46
         assert [(0.7,), (0.0,), (0.5,)] == path_values
47
+
48
+    def test_get_value(self):
49
+        visibility = Matrixes()
50
+        visibility.initialize_empty_matrix('testing', matrix_width=3, matrix_height=2, value_structure=['opacity'])
51
+        visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
52
+        value = visibility.get_value('testing', x=2, y=1, value_name='opacity')
53
+        assert 0.5 == value