Added animation events

This commit is contained in:
2025-06-20 19:26:43 +01:00
parent 44f75b52d0
commit a421069a63
4 changed files with 41 additions and 3 deletions

View File

@ -2,3 +2,5 @@ from .base import Game
from .utils import centre from .utils import centre
from .anim import AnimationHandler from .anim import AnimationHandler
from .anim import AnimatedObject from .anim import AnimatedObject
from .events import AnimStart
from .events import AnimFinish

View File

@ -1,3 +1,5 @@
from .events import AnimStart
from .events import AnimFinish
import pygame import pygame
import time import time
@ -18,12 +20,16 @@ class AnimatedObject:
""" """
""" """
def __init__(self, baseFrame): def __init__(self, _game, objectID: str, baseFrame: str):
""" """
Args: Args:
_game: Used internally.
objectID: Used to identify this animated object.
baseFrame: A still image that gets displayed when no other animation is playing. baseFrame: A still image that gets displayed when no other animation is playing.
""" """
self._game = _game
self.objectID = objectID
self._currentAnim = None self._currentAnim = None
self._animStart = None self._animStart = None
self._animations = {} self._animations = {}
@ -47,6 +53,7 @@ class AnimatedObject:
frame = min(round((time.perf_counter() - self._animStart) * self._currentAnim.fps), self._currentAnim.frameCount - 1) 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)) self._surf.blit(self._currentAnim.frames[frame][0], (0, 0))
if frame == self._currentAnim.frameCount - 1: if frame == self._currentAnim.frameCount - 1:
self._game.onEvent(AnimFinish(self.objectID, self._currentAnim.animationID))
self._currentAnim = None self._currentAnim = None
self._animStart = None self._animStart = None
@ -63,12 +70,16 @@ class AnimatedObject:
""" """
self._animations.update({animation.animationID: animation}) self._animations.update({animation.animationID: animation})
def playAnim(self, animationID: str): def playAnim(self, animationID: str, overrideCurrentAnim=False):
""" """
Plays an animation. Plays an animation.
Args: Args:
animationID (str): The animation ID to play. animationID (str): The animation ID to play.
""" """
if self._currentAnim != None and not overrideCurrentAnim: return
self._animStart = time.perf_counter() self._animStart = time.perf_counter()
self._currentAnim = self._animations[animationID] self._currentAnim = self._animations[animationID]
self._game.onEvent(AnimStart(self.objectID, animationID))

View File

@ -1,3 +1,4 @@
from .anim import AnimatedObject
import tomllib import tomllib
class Game: class Game:
@ -23,4 +24,7 @@ class Game:
pass pass
def close(self): def close(self):
pass pass
def createAnimObj(self, *args, **kwargs):
return AnimatedObject(self, *args, **kwargs)

21
src/gameUtils/events.py Normal file
View File

@ -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'<AnimFinish | {self.objectID=} | {self.animationID=}>'
class AnimFinish(_event):
def __init__(self, objectID, animationID):
super().__init__()
self.objectID = objectID
self.animationID = animationID
def __repr__(self):
return f'<AnimFinish | {self.objectID=} | {self.animationID=}>'