12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # coding: utf-8
- import typing
-
- import pyglet
-
- import cocos
- from cocos import collision_model
- from cocos import euclid
- from synergine2.simulation import Subject
- from synergine2_cocos2d.animation import AnimatedInterface
-
-
- class Actor(AnimatedInterface, cocos.sprite.Sprite):
- animation_image_paths = {} # type: typing.Dict[str, typing.List[str]]
- animation_images = {} # type: typing.Dict[str, typing.List[pyglet.image.TextureRegion]]
-
- def __init__(
- self,
- image: pyglet.image.TextureRegion,
- subject: Subject,
- position=(0, 0),
- rotation=0,
- scale=1,
- opacity=255,
- color=(255, 255, 255),
- anchor=None,
- properties: dict=None,
- **kwargs
- ):
- super().__init__(
- image,
- position,
- rotation,
- scale,
- opacity,
- color,
- anchor,
- **kwargs
- )
- self.subject = subject
- self.cshape = None # type: collision_model.AARectShape
- self.update_cshape()
- self.build_animation_images()
- self.current_image = image
- self.need_update_cshape = False
- self.properties = properties or {}
- self._freeze = False
-
- def freeze(self) -> None:
- """
- Set object to freeze mode: No visual modification can be done anymore
- """
- self._freeze = True
-
- def stop_actions(self, action_types: typing.Tuple[typing.Type[cocos.actions.Action], ...]) -> None:
- for action in self.actions:
- if isinstance(action, action_types):
- self.remove_action(action)
-
- def update_cshape(self) -> None:
- self.cshape = collision_model.AARectShape(
- euclid.Vector2(self.position[0], self.position[1]),
- self.width // 2,
- self.height // 2,
- )
- self.need_update_cshape = False
-
- def update_position(self, new_position: euclid.Vector2) -> None:
- if self._freeze:
- return
-
- self.position = new_position
- self.cshape.center = new_position # Note: if remove: strange behaviour: drag change actor position with anomaly
-
- def build_animation_images(self) -> None:
- """
- Fill self.animation_images with self.animation_image_paths
- :return: None
- """
- for animation_name, animation_image_paths in self.animation_image_paths.items():
- self.animation_images[animation_name] = []
- for animation_image_path in animation_image_paths:
- self.animation_images[animation_name].append(pyglet.resource.image(animation_image_path))
-
- def get_images_for_animation(self, animation_name: str) -> typing.List[pyglet.image.TextureRegion]:
- return self.animation_images.get(animation_name)
-
- def get_inanimate_image(self) -> pyglet.image.TextureRegion:
- return self.current_image
-
- def update_image(self, new_image: pyglet.image.TextureRegion):
- if self._freeze:
- return
-
- self.image = new_image
- self.image_anchor = new_image.width // 2, new_image.height // 2
|