middleware.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # coding: utf-8
  2. import os
  3. import cocos
  4. from synergine2.config import Config
  5. from synergine2.log import SynergineLogger
  6. class MapMiddleware(object):
  7. def __init__(
  8. self,
  9. config: Config,
  10. logger: SynergineLogger,
  11. map_dir_path: str,
  12. ) -> None:
  13. self.config = config
  14. self.logger = logger
  15. self.map_dir_path = map_dir_path
  16. self.tmx = None
  17. def get_map_file_path(self) -> str:
  18. return os.path.join(
  19. self.map_dir_path,
  20. '{}.tmx'.format(os.path.basename(self.map_dir_path)),
  21. )
  22. def init(self) -> None:
  23. map_file_path = self.get_map_file_path()
  24. self.tmx = cocos.tiles.load(map_file_path)
  25. def get_background_sprite(self) -> cocos.sprite.Sprite:
  26. raise NotImplementedError()
  27. def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
  28. raise NotImplementedError()
  29. def get_top_layer(self) -> cocos.tiles.RectMapLayer:
  30. raise NotImplementedError()
  31. def get_world_height(self) -> int:
  32. raise NotImplementedError()
  33. def get_world_width(self) -> int:
  34. raise NotImplementedError()
  35. def get_cell_height(self) -> int:
  36. raise NotImplementedError()
  37. def get_cell_width(self) -> int:
  38. raise NotImplementedError()
  39. class TMXMiddleware(MapMiddleware):
  40. def get_background_sprite(self) -> cocos.sprite.Sprite:
  41. return cocos.sprite.Sprite(os.path.join(
  42. self.map_dir_path,
  43. 'background.png',
  44. ))
  45. def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
  46. assert self.tmx
  47. return self.tmx['ground']
  48. def get_top_layer(self) -> cocos.tiles.RectMapLayer:
  49. assert self.tmx
  50. return self.tmx['top']
  51. def get_world_height(self) -> int:
  52. return len(self.tmx['ground'].cells[0])
  53. def get_world_width(self) -> int:
  54. return len(self.tmx['ground'].cells)
  55. def get_cell_height(self) -> int:
  56. return self.tmx['ground'].cells[0][0].size[1]
  57. def get_cell_width(self) -> int:
  58. return self.tmx['ground'].cells[0][0].size[0]