actor.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. class Actor(AnimatedInterface, cocos.sprite.Sprite):
  9. animation_image_paths = {} # type: typing.Dict[str, typing.List[str]]
  10. animation_images = {} # type: typing.Dict[str, typing.List[pyglet.image.TextureRegion]]
  11. def __init__(
  12. self,
  13. image: pyglet.image.TextureRegion,
  14. position=(0, 0),
  15. rotation=0,
  16. scale=1,
  17. opacity=255,
  18. color=(255, 255, 255),
  19. anchor=None,
  20. **kwargs
  21. ):
  22. super().__init__(
  23. image,
  24. position,
  25. rotation,
  26. scale,
  27. opacity,
  28. color,
  29. anchor,
  30. **kwargs
  31. )
  32. self.cshape = collision_model.AARectShape(
  33. euclid.Vector2(0.0, 0.0),
  34. self.width,
  35. self.height,
  36. )
  37. self.build_animation_images()
  38. self.current_image = image
  39. def update_position(self, new_position: euclid.Vector2) -> None:
  40. self.position = new_position
  41. self.cshape.center = new_position
  42. def build_animation_images(self) -> None:
  43. """
  44. Fill self.animation_images with self.animation_image_paths
  45. :return: None
  46. """
  47. for animation_name, animation_image_paths in self.animation_image_paths.items():
  48. self.animation_images[animation_name] = []
  49. for animation_image_path in animation_image_paths:
  50. self.animation_images[animation_name].append(pyglet.resource.image(animation_image_path))
  51. def get_images_for_animation(self, animation_name: str) -> typing.List[pyglet.image.TextureRegion]:
  52. return self.animation_images.get(animation_name)
  53. def get_inanimate_image(self) -> pyglet.image.TextureRegion:
  54. return self.current_image
  55. def update_image(self, new_image: pyglet.image.TextureRegion):
  56. self.image = new_image
  57. self.image_anchor = new_image.width // 2, new_image.height // 2