Browse Source

Update middleware.py

Bastien Sevajol 6 years ago
parent
commit
fd02270ed8
No account linked to committer's email
1 changed files with 15 additions and 8 deletions
  1. 15 8
      synergine2_cocos2d/middleware.py

+ 15 - 8
synergine2_cocos2d/middleware.py View File

1
 # coding: utf-8
1
 # coding: utf-8
2
 import os
2
 import os
3
+import typing
3
 
4
 
4
-import cocos
5
 from synergine2.config import Config
5
 from synergine2.config import Config
6
 from synergine2.log import get_logger
6
 from synergine2.log import get_logger
7
 from synergine2_cocos2d.util import get_map_file_path_from_dir
7
 from synergine2_cocos2d.util import get_map_file_path_from_dir
8
 
8
 
9
+if typing.TYPE_CHECKING:
10
+    import cocos
11
+
9
 
12
 
10
 class MapMiddleware(object):
13
 class MapMiddleware(object):
11
     def __init__(
14
     def __init__(
22
         return get_map_file_path_from_dir(self.map_dir_path)
25
         return get_map_file_path_from_dir(self.map_dir_path)
23
 
26
 
24
     def init(self) -> None:
27
     def init(self) -> None:
28
+        # import cocos here for prevent test crash when no X server is
29
+        # present
30
+        import cocos
31
+
25
         map_file_path = self.get_map_file_path()
32
         map_file_path = self.get_map_file_path()
26
         self.tmx = cocos.tiles.load(map_file_path)
33
         self.tmx = cocos.tiles.load(map_file_path)
27
 
34
 
28
-    def get_background_sprite(self) -> cocos.sprite.Sprite:
35
+    def get_background_sprite(self) -> 'cocos.sprite.Sprite':
29
         raise NotImplementedError()
36
         raise NotImplementedError()
30
 
37
 
31
-    def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
38
+    def get_ground_layer(self) -> 'cocos.tiles.RectMapLayer':
32
         raise NotImplementedError()
39
         raise NotImplementedError()
33
 
40
 
34
-    def get_top_layer(self) -> cocos.tiles.RectMapLayer:
41
+    def get_top_layer(self) -> 'cocos.tiles.RectMapLayer':
35
         raise NotImplementedError()
42
         raise NotImplementedError()
36
 
43
 
37
     def get_world_height(self) -> int:
44
     def get_world_height(self) -> int:
48
 
55
 
49
 
56
 
50
 class TMXMiddleware(MapMiddleware):
57
 class TMXMiddleware(MapMiddleware):
51
-    def get_background_sprite(self) -> cocos.sprite.Sprite:
58
+    def get_background_sprite(self) -> 'cocos.sprite.Sprite':
52
         # TODO: Extract it from tmx
59
         # TODO: Extract it from tmx
53
         return cocos.sprite.Sprite(os.path.join(
60
         return cocos.sprite.Sprite(os.path.join(
54
             self.map_dir_path,
61
             self.map_dir_path,
55
             'background.png',
62
             'background.png',
56
         ))
63
         ))
57
 
64
 
58
-    def get_interior_sprite(self) -> cocos.sprite.Sprite:
65
+    def get_interior_sprite(self) -> 'cocos.sprite.Sprite':
59
         # TODO: Extract it from tmx
66
         # TODO: Extract it from tmx
60
         return cocos.sprite.Sprite(os.path.join(
67
         return cocos.sprite.Sprite(os.path.join(
61
             self.map_dir_path,
68
             self.map_dir_path,
62
             'background_interiors.png',
69
             'background_interiors.png',
63
         ))
70
         ))
64
 
71
 
65
-    def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
72
+    def get_ground_layer(self) -> 'cocos.tiles.RectMapLayer':
66
         assert self.tmx
73
         assert self.tmx
67
         return self.tmx['ground']
74
         return self.tmx['ground']
68
 
75
 
69
-    def get_top_layer(self) -> cocos.tiles.RectMapLayer:
76
+    def get_top_layer(self) -> 'cocos.tiles.RectMapLayer':
70
         assert self.tmx
77
         assert self.tmx
71
         return self.tmx['top']
78
         return self.tmx['top']
72
 
79