weapon.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # coding: utf-8
  2. import typing
  3. import pyglet
  4. from PIL import Image
  5. from pyglet.image import ImageData
  6. from synergine2.config import Config
  7. from synergine2_cocos2d.util import PathManager
  8. from opencombat.exception import UnknownWeapon
  9. from opencombat.gui.const import POSITION_MAN_STAND_UP
  10. from opencombat.gui.const import POSITION_MAN_CRAWLING
  11. if typing.TYPE_CHECKING:
  12. from opencombat.gui.actor import BaseActor
  13. RIFFLE = 'RIFFLE'
  14. class ImageApplier(object):
  15. pass # FIXME: refact here
  16. class WeaponImageApplier(ImageApplier):
  17. def __init__(
  18. self,
  19. config: Config,
  20. actor: 'BaseActor',
  21. ) -> None:
  22. self.actor = actor
  23. self._images_scheme = self.get_rest_images_scheme()
  24. self._firing_images_scheme = self.get_firing_images_scheme()
  25. self.path_manager = PathManager(config.resolve('global.include_path.graphics'))
  26. self._cache = {} # type: typing.Dict[str, Image.Image]
  27. self._firing_cache = {} # type: typing.Dict[str, Image.Image]
  28. def get_rest_images_scheme(self) -> typing.Dict[str, typing.Dict[str, typing.List[str]]]:
  29. return {
  30. POSITION_MAN_STAND_UP: {
  31. RIFFLE: [
  32. 'actors/man_weap1.png'
  33. ],
  34. },
  35. POSITION_MAN_CRAWLING: {
  36. RIFFLE: [
  37. 'actors/man_c1_weap1.png',
  38. 'actors/man_c2_weap1.png',
  39. 'actors/man_c3_weap1.png',
  40. 'actors/man_c4_weap1.png',
  41. ],
  42. }
  43. }
  44. def get_firing_images_scheme(self) -> typing.Dict[str, typing.Dict[str, typing.List[str]]]:
  45. return {
  46. POSITION_MAN_STAND_UP: {
  47. RIFFLE: [
  48. 'actors/man_weap1_firing1.png',
  49. 'actors/man_weap1_firing2.png',
  50. 'actors/man_weap1_firing3.png',
  51. ],
  52. },
  53. }
  54. def get_image_for_weapon(self, mode: str, weapon_type: str) -> Image.Image:
  55. try:
  56. image_file_path = self.path_manager.path(
  57. self._images_scheme[mode][weapon_type][0],
  58. )
  59. try:
  60. return self._cache[image_file_path]
  61. except KeyError:
  62. self._cache[image_file_path] = Image.open(image_file_path)
  63. return self._cache[image_file_path]
  64. except KeyError:
  65. raise UnknownWeapon(
  66. 'Unknown weapon "{}" for mode "{}"'.format(weapon_type, mode),
  67. )
  68. def get_firing_image(
  69. self, mode: str,
  70. weapon_type: str,
  71. ) -> typing.List[Image.Image]:
  72. images = []
  73. try:
  74. image_file_paths = self._firing_images_scheme[mode][weapon_type]
  75. for image_file_path in image_file_paths:
  76. final_path = self.path_manager.path(image_file_path)
  77. try:
  78. images.append(self._firing_cache[final_path])
  79. except KeyError:
  80. self._firing_cache[image_file_path] = Image.open(final_path)
  81. images.append(self._firing_cache[image_file_path])
  82. return images
  83. except KeyError:
  84. raise UnknownWeapon(
  85. 'Unknown weapon "{}" for mode "{}"'.format(weapon_type, mode),
  86. )
  87. def get_animation_image_for_weapon(
  88. self,
  89. mode: str,
  90. weapon_type: str,
  91. animation_position: int,
  92. ) -> Image.Image:
  93. try:
  94. image_file_path = self.path_manager.path(
  95. self._images_scheme[mode][weapon_type][animation_position],
  96. )
  97. try:
  98. return self._cache[image_file_path]
  99. except KeyError:
  100. self._cache[image_file_path] = Image.open(image_file_path)
  101. return self._cache[image_file_path]
  102. except KeyError:
  103. raise UnknownWeapon(
  104. 'Unknown weapon "{}" for mode "{}"'.format(weapon_type, mode),
  105. )
  106. except IndexError:
  107. raise UnknownWeapon(
  108. 'Unknown weapon "{}" for mode "{}"'.format(weapon_type, mode),
  109. )