middleware.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 init(self) -> None:
  18. self.tmx = cocos.tiles.load(os.path.join(
  19. self.map_dir_path,
  20. '{}.tmx'.format(os.path.basename(self.map_dir_path)),
  21. ))
  22. def get_background_sprite(self) -> cocos.sprite.Sprite:
  23. raise NotImplementedError()
  24. def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
  25. raise NotImplementedError()
  26. def get_top_layer(self) -> cocos.tiles.RectMapLayer:
  27. raise NotImplementedError()
  28. def get_world_height(self) -> int:
  29. raise NotImplementedError()
  30. def get_world_width(self) -> int:
  31. raise NotImplementedError()
  32. def get_cell_height(self) -> int:
  33. raise NotImplementedError()
  34. def get_cell_width(self) -> int:
  35. raise NotImplementedError()
  36. class TMXMiddleware(MapMiddleware):
  37. def get_background_sprite(self) -> cocos.sprite.Sprite:
  38. return cocos.sprite.Sprite(os.path.join(
  39. self.map_dir_path,
  40. 'background.png',
  41. ))
  42. def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
  43. assert self.tmx
  44. return self.tmx['ground']
  45. def get_top_layer(self) -> cocos.tiles.RectMapLayer:
  46. assert self.tmx
  47. return self.tmx['top']
  48. def get_world_height(self) -> int:
  49. return len(self.tmx['ground'].cells[0])
  50. def get_world_width(self) -> int:
  51. return len(self.tmx['ground'].cells)
  52. def get_cell_height(self) -> int:
  53. return self.tmx['ground'].cells[0][0].size[1]
  54. def get_cell_width(self) -> int:
  55. return self.tmx['ground'].cells[0][0].size[0]