Pārlūkot izejas kodu

Matrixes: define value structure

Bastien Sevajol 6 gadus atpakaļ
vecāks
revīzija
b12c4ed018
2 mainītis faili ar 36 papildinājumiem un 13 dzēšanām
  1. 20 4
      synergine2_xyz/physics.py
  2. 16 9
      tests/test_physics.py

+ 20 - 4
synergine2_xyz/physics.py Parādīt failu

@@ -36,16 +36,26 @@ class MoveCostComputer(object):
36 36
         return 1.0
37 37
 
38 38
 
39
-class VisibilityMatrix(object):
39
+class Matrixes(object):
40 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 52
         self._matrixes[name] = []
53
+        self._value_structures[name] = value_structure
44 54
 
45 55
         for y in range(matrix_height):
46 56
             x_list = []
47 57
             for x in range(matrix_width):
48
-                x_list.append((0.0,))
58
+                x_list.append(tuple([0.0] * len(value_structure)))
49 59
             self._matrixes[name].append(x_list)
50 60
 
51 61
     def get_matrix(self, name: str) -> typing.List[typing.List[tuple]]:
@@ -73,9 +83,15 @@ class VisibilityMatrix(object):
73 83
             values.append(matrix[y][x])
74 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 93
 class Physics(object):
78
-    visibility_matrix = VisibilityMatrix
94
+    visibility_matrix = Matrixes
79 95
     move_cost_computer_class = MoveCostComputer
80 96
 
81 97
     def __init__(

+ 16 - 9
tests/test_physics.py Parādīt failu

@@ -1,14 +1,14 @@
1 1
 # coding: utf-8
2 2
 import pytest
3 3
 
4
-from synergine2_xyz.physics import VisibilityMatrix
4
+from synergine2_xyz.physics import Matrixes
5 5
 from tests import BaseTest
6 6
 
7 7
 
8 8
 class TestVisibilityMatrix(BaseTest):
9 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 12
         matrix = visibility.get_matrix('testing')
13 13
 
14 14
         assert isinstance(matrix, list)
@@ -17,8 +17,8 @@ class TestVisibilityMatrix(BaseTest):
17 17
         assert [(0.0,), (0.0,), (0.0,)] == matrix[1]
18 18
 
19 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 22
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
23 23
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
24 24
         matrix = visibility.get_matrix('testing')
@@ -27,8 +27,8 @@ class TestVisibilityMatrix(BaseTest):
27 27
         assert [(0.0,), (0.0,), (0.5,)] == matrix[1]
28 28
 
29 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 32
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
33 33
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
34 34
 
@@ -36,11 +36,18 @@ class TestVisibilityMatrix(BaseTest):
36 36
         assert [(0, 0), (1, 0), (2, 1)] == path_positions
37 37
 
38 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 41
         visibility.update_matrix('testing', x=2, y=1, value=(0.5,))
42 42
         visibility.update_matrix('testing', x=0, y=0, value=(0.7,))
43 43
 
44 44
         path_positions = visibility.get_path_positions(from_=(0, 0), to=(2, 1))
45 45
         path_values = visibility.get_values_for_path('testing', path_positions=path_positions)
46 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