actor.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # coding: utf-8
  2. import typing
  3. import pyglet
  4. import cocos
  5. from cocos import collision_model
  6. from cocos import euclid
  7. from synergine2.simulation import Subject
  8. from synergine2_cocos2d.animation import AnimatedInterface
  9. class Actor(AnimatedInterface, cocos.sprite.Sprite):
  10. animation_image_paths = {} # type: typing.Dict[str, typing.List[str]]
  11. animation_images = {} # type: typing.Dict[str, typing.List[pyglet.image.TextureRegion]]
  12. def __init__(
  13. self,
  14. image: pyglet.image.TextureRegion,
  15. subject: Subject,
  16. position=(0, 0),
  17. rotation=0,
  18. scale=1,
  19. opacity=255,
  20. color=(255, 255, 255),
  21. anchor=None,
  22. properties: dict=None,
  23. **kwargs
  24. ):
  25. super().__init__(
  26. image,
  27. position,
  28. rotation,
  29. scale,
  30. opacity,
  31. color,
  32. anchor,
  33. **kwargs
  34. )
  35. self.subject = subject
  36. self.cshape = None # type: collision_model.AARectShape
  37. self.update_cshape()
  38. self.build_animation_images()
  39. self.current_image = image
  40. self.need_update_cshape = False
  41. self.properties = properties or {}
  42. self._freeze = False
  43. def freeze(self) -> None:
  44. """
  45. Set object to freeze mode: No visual modification can be done anymore
  46. """
  47. self._freeze = True
  48. def stop_actions(self, action_types: typing.Tuple[typing.Type[cocos.actions.Action], ...]) -> None:
  49. for action in self.actions:
  50. if isinstance(action, action_types):
  51. self.remove_action(action)
  52. def update_cshape(self) -> None:
  53. self.cshape = collision_model.AARectShape(
  54. euclid.Vector2(self.position[0], self.position[1]),
  55. self.width // 2,
  56. self.height // 2,
  57. )
  58. self.need_update_cshape = False
  59. def update_position(self, new_position: euclid.Vector2) -> None:
  60. if self._freeze:
  61. return
  62. self.position = new_position
  63. self.cshape.center = new_position # Note: if remove: strange behaviour: drag change actor position with anomaly
  64. def build_animation_images(self) -> None:
  65. """
  66. Fill self.animation_images with self.animation_image_paths
  67. :return: None
  68. """
  69. for animation_name, animation_image_paths in self.animation_image_paths.items():
  70. self.animation_images[animation_name] = []
  71. for animation_image_path in animation_image_paths:
  72. self.animation_images[animation_name].append(pyglet.resource.image(animation_image_path))
  73. def get_images_for_animation(self, animation_name: str) -> typing.List[pyglet.image.TextureRegion]:
  74. return self.animation_images.get(animation_name)
  75. def get_inanimate_image(self) -> pyglet.image.TextureRegion:
  76. return self.current_image
  77. def update_image(self, new_image: pyglet.image.TextureRegion):
  78. if self._freeze:
  79. return
  80. self.image = new_image
  81. self.image_anchor = new_image.width // 2, new_image.height // 2