actor.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_cocos2d.animation import AnimatedInterface
  8. from synergine2_cocos2d.layer import LayerManager
  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. position=(0, 0),
  16. rotation=0,
  17. scale=1,
  18. opacity=255,
  19. color=(255, 255, 255),
  20. anchor=None,
  21. **kwargs
  22. ):
  23. super().__init__(
  24. image,
  25. position,
  26. rotation,
  27. scale,
  28. opacity,
  29. color,
  30. anchor,
  31. **kwargs
  32. )
  33. self.cshape = None # type: collision_model.AARectShape
  34. self.update_cshape()
  35. self.build_animation_images()
  36. self.current_image = image
  37. self.need_update_cshape = False
  38. def update_cshape(self) -> None:
  39. self.cshape = collision_model.AARectShape(
  40. euclid.Vector2(self.position[0], self.position[1]),
  41. self.width // 2,
  42. self.height // 2,
  43. )
  44. def update_position(self, new_position: euclid.Vector2) -> None:
  45. self.position = new_position
  46. self.cshape.center = new_position
  47. def build_animation_images(self) -> None:
  48. """
  49. Fill self.animation_images with self.animation_image_paths
  50. :return: None
  51. """
  52. for animation_name, animation_image_paths in self.animation_image_paths.items():
  53. self.animation_images[animation_name] = []
  54. for animation_image_path in animation_image_paths:
  55. self.animation_images[animation_name].append(pyglet.resource.image(animation_image_path))
  56. def get_images_for_animation(self, animation_name: str) -> typing.List[pyglet.image.TextureRegion]:
  57. return self.animation_images.get(animation_name)
  58. def get_inanimate_image(self) -> pyglet.image.TextureRegion:
  59. return self.current_image
  60. def update_image(self, new_image: pyglet.image.TextureRegion):
  61. self.image = new_image
  62. self.image_anchor = new_image.width // 2, new_image.height // 2