Implemented AnimatedObject
This commit is contained in:
28
src/anim.py
28
src/anim.py
@ -1,4 +1,5 @@
|
||||
import pygame
|
||||
import time
|
||||
|
||||
class AnimationHandler(pygame.Surface):
|
||||
"""
|
||||
@ -6,9 +7,13 @@ class AnimationHandler(pygame.Surface):
|
||||
webp file, and handles drawing it to the screen in
|
||||
a way that doesn't rely on frame counters.
|
||||
"""
|
||||
def __init__(self, animationPath):
|
||||
pass
|
||||
def __init__(self, animationID: str, animationPath: str, fps: int):
|
||||
self.animationID = animationID
|
||||
self.frames = pygame.image.load_animation(animationPath)
|
||||
self.fps = fps
|
||||
self.frameCount = len(self.frames)
|
||||
|
||||
# TODO: Add optional transparancy?
|
||||
class AnimatedObject:
|
||||
"""
|
||||
|
||||
@ -22,7 +27,8 @@ class AnimatedObject:
|
||||
self._currentAnim = None
|
||||
self._animStart = None
|
||||
self._animations = {}
|
||||
#self.baseFrame = pygame.image.load(baseFrame)
|
||||
self.baseFrame = pygame.image.load(baseFrame)
|
||||
self._surf = pygame.Surface(self.baseFrame.size)
|
||||
|
||||
def getFrame(self) -> pygame.Surface:
|
||||
"""
|
||||
@ -32,9 +38,21 @@ class AnimatedObject:
|
||||
pygame.Surface: The current frame
|
||||
"""
|
||||
|
||||
self._surf.fill((0, 0, 0))
|
||||
|
||||
if self._currentAnim == None:
|
||||
self._surf.blit(self.baseFrame, (0, 0))
|
||||
else:
|
||||
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._currentAnim = None
|
||||
self._animStart = None
|
||||
|
||||
return self._surf
|
||||
|
||||
|
||||
def addAnimation(self, animation: AnimationHandler, animationID: str):
|
||||
def addAnimation(self, animation: AnimationHandler):
|
||||
"""
|
||||
Adds an animation to the object.
|
||||
|
||||
@ -42,7 +60,7 @@ class AnimatedObject:
|
||||
animation (AnimationHandler): The actual animation.
|
||||
animationID (str): The ID that's later used to play the animation.
|
||||
"""
|
||||
self.animations.update({animationID: animation})
|
||||
self._animations.update({animation.animationID: animation})
|
||||
|
||||
def playAnim(self, animationID: str):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user