Browse Source

Merge branch 'master' of github.com:/buxx/synergine2

Bastien Sevajol 6 years ago
parent
commit
4eb24afb32
5 changed files with 35 additions and 14 deletions
  1. 1 1
      README.md
  2. 17 12
      synergine2_cocos2d/middleware.py
  3. 2 1
      synergine2_cocos2d/util.py
  4. 10 0
      tests/test_middleware.py
  5. 5 0
      tests/test_utils.py

+ 1 - 1
README.md View File

1
 # synergine2
1
 # synergine2
2
 
2
 
3
-[![Build Status](https://travis-ci.org/buxx/synergine2.svg?branch=master)](https://travis-ci.org/buxx/synergine2) [![Coverage Status](https://coveralls.io/repos/github/buxx/synergine2/badge.svg?branch=master)](https://coveralls.io/github/buxx/synergine2?branch=master)
3
+[![Build Status](https://travis-ci.org/buxx/synergine2.svg?branch=master)](https://travis-ci.org/buxx/synergine2) [![Coverage Status](https://coveralls.io/repos/github/buxx/synergine2/badge.svg?branch=master)](https://coveralls.io/github/buxx/synergine2?branch=master) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/3ebc962adf294ebdb5f6ac052ad5300e)](https://www.codacy.com/app/sevajol.bastien/synergine2?utm_source=github.com&utm_medium=referral&utm_content=buxx/synergine2&utm_campaign=Badge_Grade)
4
 
4
 
5
 A framework to build simulation with subject focused.
5
 A framework to build simulation with subject focused.
6
 
6
 

+ 17 - 12
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
8
+
9
+if typing.TYPE_CHECKING:
10
+    import cocos
7
 
11
 
8
 
12
 
9
 class MapMiddleware(object):
13
 class MapMiddleware(object):
18
         self.tmx = None
22
         self.tmx = None
19
 
23
 
20
     def get_map_file_path(self) -> str:
24
     def get_map_file_path(self) -> str:
21
-        return os.path.join(
22
-            self.map_dir_path,
23
-            '{}.tmx'.format(os.path.basename(self.map_dir_path)),
24
-        )
25
+        return get_map_file_path_from_dir(self.map_dir_path)
25
 
26
 
26
     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
+
27
         map_file_path = self.get_map_file_path()
32
         map_file_path = self.get_map_file_path()
28
         self.tmx = cocos.tiles.load(map_file_path)
33
         self.tmx = cocos.tiles.load(map_file_path)
29
 
34
 
30
-    def get_background_sprite(self) -> cocos.sprite.Sprite:
35
+    def get_background_sprite(self) -> 'cocos.sprite.Sprite':
31
         raise NotImplementedError()
36
         raise NotImplementedError()
32
 
37
 
33
-    def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
38
+    def get_ground_layer(self) -> 'cocos.tiles.RectMapLayer':
34
         raise NotImplementedError()
39
         raise NotImplementedError()
35
 
40
 
36
-    def get_top_layer(self) -> cocos.tiles.RectMapLayer:
41
+    def get_top_layer(self) -> 'cocos.tiles.RectMapLayer':
37
         raise NotImplementedError()
42
         raise NotImplementedError()
38
 
43
 
39
     def get_world_height(self) -> int:
44
     def get_world_height(self) -> int:
50
 
55
 
51
 
56
 
52
 class TMXMiddleware(MapMiddleware):
57
 class TMXMiddleware(MapMiddleware):
53
-    def get_background_sprite(self) -> cocos.sprite.Sprite:
58
+    def get_background_sprite(self) -> 'cocos.sprite.Sprite':
54
         # TODO: Extract it from tmx
59
         # TODO: Extract it from tmx
55
         return cocos.sprite.Sprite(os.path.join(
60
         return cocos.sprite.Sprite(os.path.join(
56
             self.map_dir_path,
61
             self.map_dir_path,
57
             'background.png',
62
             'background.png',
58
         ))
63
         ))
59
 
64
 
60
-    def get_interior_sprite(self) -> cocos.sprite.Sprite:
65
+    def get_interior_sprite(self) -> 'cocos.sprite.Sprite':
61
         # TODO: Extract it from tmx
66
         # TODO: Extract it from tmx
62
         return cocos.sprite.Sprite(os.path.join(
67
         return cocos.sprite.Sprite(os.path.join(
63
             self.map_dir_path,
68
             self.map_dir_path,
64
             'background_interiors.png',
69
             'background_interiors.png',
65
         ))
70
         ))
66
 
71
 
67
-    def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
72
+    def get_ground_layer(self) -> 'cocos.tiles.RectMapLayer':
68
         assert self.tmx
73
         assert self.tmx
69
         return self.tmx['ground']
74
         return self.tmx['ground']
70
 
75
 
71
-    def get_top_layer(self) -> cocos.tiles.RectMapLayer:
76
+    def get_top_layer(self) -> 'cocos.tiles.RectMapLayer':
72
         assert self.tmx
77
         assert self.tmx
73
         return self.tmx['top']
78
         return self.tmx['top']
74
 
79
 

+ 2 - 1
synergine2_cocos2d/util.py View File

10
 
10
 
11
 def get_map_file_path_from_dir(map_dir_path: str) -> str:
11
 def get_map_file_path_from_dir(map_dir_path: str) -> str:
12
     # TODO: path is temp here
12
     # TODO: path is temp here
13
-    return '{}.tmx'.format(os.path.join(map_dir_path, os.path.basename(map_dir_path)))
13
+    return '{}.tmx'.format(os.path.join(map_dir_path,
14
+        os.path.basename(map_dir_path.rstrip('/'))))
14
 
15
 
15
 
16
 
16
 class PathManager(object):
17
 class PathManager(object):

+ 10 - 0
tests/test_middleware.py View File

1
+from synergine2.config import Config
2
+from synergine2_cocos2d.middleware import MapMiddleware
3
+
4
+class TestMapMiddleware:
5
+    def test_get_map_file_path(self):
6
+        test = MapMiddleware(Config(), 'map/003')
7
+        assert test.get_map_file_path() == 'map/003/003.tmx'
8
+        test = MapMiddleware(Config(), 'map/003/')
9
+        assert test.get_map_file_path() == 'map/003/003.tmx'
10
+

+ 5 - 0
tests/test_utils.py View File

6
 from synergine2.utils import ChunkManager
6
 from synergine2.utils import ChunkManager
7
 from synergine2_cocos2d.exception import FileNotFound
7
 from synergine2_cocos2d.exception import FileNotFound
8
 from synergine2_cocos2d.util import PathManager
8
 from synergine2_cocos2d.util import PathManager
9
+from synergine2_cocos2d.util import get_map_file_path_from_dir
9
 from tests import BaseTest
10
 from tests import BaseTest
10
 
11
 
11
 
12
 
44
         path_manager.add_included_path('tests/fixtures/some_media2')
45
         path_manager.add_included_path('tests/fixtures/some_media2')
45
         # it is prior on path finding
46
         # it is prior on path finding
46
         assert 'tests/fixtures/some_media2/foo.txt' == path_manager.path('foo.txt')
47
         assert 'tests/fixtures/some_media2/foo.txt' == path_manager.path('foo.txt')
48
+
49
+    def test_get_map_file_path_from_dir(self):
50
+        assert get_map_file_path_from_dir('map/003') == 'map/003/003.tmx'
51
+        assert get_map_file_path_from_dir('map/003/') == 'map/003/003.tmx'