diff --git a/src/gameUtils/__init__.py b/src/gameUtils/__init__.py index 6c64ec6..9c07602 100644 --- a/src/gameUtils/__init__.py +++ b/src/gameUtils/__init__.py @@ -2,3 +2,5 @@ from .base import Game from .utils import centre from .anim import AnimationHandler from .anim import AnimatedObject +from .events import AnimStart +from .events import AnimFinish \ No newline at end of file diff --git a/src/gameUtils/anim.py b/src/gameUtils/anim.py index 54a18bc..732d167 100644 --- a/src/gameUtils/anim.py +++ b/src/gameUtils/anim.py @@ -1,3 +1,5 @@ +from .events import AnimStart +from .events import AnimFinish import pygame import time @@ -18,12 +20,16 @@ class AnimatedObject: """ """ - def __init__(self, baseFrame): + def __init__(self, _game, objectID: str, baseFrame: str): """ Args: + _game: Used internally. + objectID: Used to identify this animated object. baseFrame: A still image that gets displayed when no other animation is playing. """ + self._game = _game + self.objectID = objectID self._currentAnim = None self._animStart = None self._animations = {} @@ -47,6 +53,7 @@ class AnimatedObject: frame = min(round((time.perf_counter() - self._animStart) * self._currentAnim.fps), self._currentAnim.frameCount - 1) self._surf.blit(self._currentAnim.frames[frame][0], (0, 0)) if frame == self._currentAnim.frameCount - 1: + self._game.onEvent(AnimFinish(self.objectID, self._currentAnim.animationID)) self._currentAnim = None self._animStart = None @@ -63,12 +70,16 @@ class AnimatedObject: """ self._animations.update({animation.animationID: animation}) - def playAnim(self, animationID: str): + def playAnim(self, animationID: str, overrideCurrentAnim=False): """ Plays an animation. Args: animationID (str): The animation ID to play. """ + + if self._currentAnim != None and not overrideCurrentAnim: return + self._animStart = time.perf_counter() self._currentAnim = self._animations[animationID] + self._game.onEvent(AnimStart(self.objectID, animationID)) diff --git a/src/gameUtils/base.py b/src/gameUtils/base.py index 6b07948..f9c3c87 100644 --- a/src/gameUtils/base.py +++ b/src/gameUtils/base.py @@ -1,3 +1,4 @@ +from .anim import AnimatedObject import tomllib class Game: @@ -23,4 +24,7 @@ class Game: pass def close(self): - pass \ No newline at end of file + pass + + def createAnimObj(self, *args, **kwargs): + return AnimatedObject(self, *args, **kwargs) diff --git a/src/gameUtils/events.py b/src/gameUtils/events.py new file mode 100644 index 0000000..8fd4b36 --- /dev/null +++ b/src/gameUtils/events.py @@ -0,0 +1,21 @@ +class _event: + def __init__(self): + self.type = self.__class__ + +class AnimStart(_event): + def __init__(self, objectID, animationID): + super().__init__() + self.objectID = objectID + self.animationID = animationID + + def __repr__(self): + return f'' + +class AnimFinish(_event): + def __init__(self, objectID, animationID): + super().__init__() + self.objectID = objectID + self.animationID = animationID + + def __repr__(self): + return f'' \ No newline at end of file