Explorar el Código

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

Bastien Sevajol hace 6 años
padre
commit
4eb24afb32
Se han modificado 5 ficheros con 35 adiciones y 14 borrados
  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 Ver fichero

@@ -1,6 +1,6 @@
1 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 5
 A framework to build simulation with subject focused.
6 6
 

+ 17 - 12
synergine2_cocos2d/middleware.py Ver fichero

@@ -1,9 +1,13 @@
1 1
 # coding: utf-8
2 2
 import os
3
+import typing
3 4
 
4
-import cocos
5 5
 from synergine2.config import Config
6 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 13
 class MapMiddleware(object):
@@ -18,22 +22,23 @@ class MapMiddleware(object):
18 22
         self.tmx = None
19 23
 
20 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 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 32
         map_file_path = self.get_map_file_path()
28 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 36
         raise NotImplementedError()
32 37
 
33
-    def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
38
+    def get_ground_layer(self) -> 'cocos.tiles.RectMapLayer':
34 39
         raise NotImplementedError()
35 40
 
36
-    def get_top_layer(self) -> cocos.tiles.RectMapLayer:
41
+    def get_top_layer(self) -> 'cocos.tiles.RectMapLayer':
37 42
         raise NotImplementedError()
38 43
 
39 44
     def get_world_height(self) -> int:
@@ -50,25 +55,25 @@ class MapMiddleware(object):
50 55
 
51 56
 
52 57
 class TMXMiddleware(MapMiddleware):
53
-    def get_background_sprite(self) -> cocos.sprite.Sprite:
58
+    def get_background_sprite(self) -> 'cocos.sprite.Sprite':
54 59
         # TODO: Extract it from tmx
55 60
         return cocos.sprite.Sprite(os.path.join(
56 61
             self.map_dir_path,
57 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 66
         # TODO: Extract it from tmx
62 67
         return cocos.sprite.Sprite(os.path.join(
63 68
             self.map_dir_path,
64 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 73
         assert self.tmx
69 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 77
         assert self.tmx
73 78
         return self.tmx['top']
74 79
 

+ 2 - 1
synergine2_cocos2d/util.py Ver fichero

@@ -10,7 +10,8 @@ from synergine2_cocos2d.exception import FileNotFound
10 10
 
11 11
 def get_map_file_path_from_dir(map_dir_path: str) -> str:
12 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 17
 class PathManager(object):

+ 10 - 0
tests/test_middleware.py Ver fichero

@@ -0,0 +1,10 @@
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 Ver fichero

@@ -6,6 +6,7 @@ import pytest
6 6
 from synergine2.utils import ChunkManager
7 7
 from synergine2_cocos2d.exception import FileNotFound
8 8
 from synergine2_cocos2d.util import PathManager
9
+from synergine2_cocos2d.util import get_map_file_path_from_dir
9 10
 from tests import BaseTest
10 11
 
11 12
 
@@ -44,3 +45,7 @@ class TestUtils(BaseTest):
44 45
         path_manager.add_included_path('tests/fixtures/some_media2')
45 46
         # it is prior on path finding
46 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'