middleware.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. # TODO: Extract it from tmx
  42. return cocos.sprite.Sprite(os.path.join(
  43. self.map_dir_path,
  44. 'background.png',
  45. ))
  46. def get_interior_sprite(self) -> cocos.sprite.Sprite:
  47. # TODO: Extract it from tmx
  48. return cocos.sprite.Sprite(os.path.join(
  49. self.map_dir_path,
  50. 'background_interiors.png',
  51. ))
  52. def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
  53. assert self.tmx
  54. return self.tmx['ground']
  55. def get_top_layer(self) -> cocos.tiles.RectMapLayer:
  56. assert self.tmx
  57. return self.tmx['top']
  58. def get_world_height(self) -> int:
  59. return len(self.tmx['ground'].cells[0])
  60. def get_world_width(self) -> int:
  61. return len(self.tmx['ground'].cells)
  62. def get_cell_height(self) -> int:
  63. return self.tmx['ground'].cells[0][0].size[1]
  64. def get_cell_width(self) -> int:
  65. return self.tmx['ground'].cells[0][0].size[0]