weapon.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # coding: utf-8
  2. import typing
  3. from PIL import Image
  4. from synergine2.config import Config
  5. from synergine2_cocos2d.util import PathManager
  6. from opencombat.exception import UnknownWeapon
  7. if typing.TYPE_CHECKING:
  8. from opencombat.gui.actor import BaseActor
  9. RIFFLE = 'RIFFLE'
  10. class ImageApplier(object):
  11. pass # FIXME: refact here
  12. class WeaponImageApplier(ImageApplier):
  13. def __init__(
  14. self,
  15. config: Config,
  16. actor: 'BaseActor',
  17. ) -> None:
  18. self.actor = actor
  19. self._images_scheme = self.get_images_scheme()
  20. self.path_manager = PathManager(config.resolve('global.include_path.graphics'))
  21. def get_images_scheme(self) -> typing.Dict[str, typing.Dict[str, str]]:
  22. from opencombat.gui.actor import MAN_STAND_UP
  23. return {
  24. MAN_STAND_UP: {
  25. RIFFLE: 'actors/man_weap1.png',
  26. }
  27. }
  28. def get_default_image_for_weapon(self, mode: str, weapon_type: str) -> Image.Image:
  29. try:
  30. # FIXME Cache
  31. image_file_path = self.path_manager.path(
  32. self._images_scheme[mode][weapon_type],
  33. )
  34. return Image.open(image_file_path)
  35. except KeyError:
  36. raise UnknownWeapon(
  37. 'Unknown weapon "{}" for mode "{}"'.format(weapon_type, mode),
  38. )