diff --git a/triple-dungeon/sprites.py b/triple-dungeon/sprites.py index 35ff8e8..24c1cc2 100644 --- a/triple-dungeon/sprites.py +++ b/triple-dungeon/sprites.py @@ -2,9 +2,13 @@ sprites.py A file dedicated to managing sprites and animations for characters. """ + +import arcade import os import re +from itertools import cycle + class AnimationSet(object): """ @@ -18,16 +22,25 @@ class AnimationSet(object): :param directory: A directory containing valid animation files in the correct format. """ - self.animations = os.path.listdir(directory) + self.directory = directory + self.animations = os.listdir(directory) def getAnimations(self, pattern: re.Pattern) -> iter: + """ + Loads all animations from the AnimationSet's directory that match the pattern. + The pattern must have 1 group that specifies the animation's index. + + :param pattern: A RegEx Pattern object. + :return: A infinite iterable looping through arcade.Texture objects. + """ + # Finds all matching files matches = [file for file in self.animations if re.match(pattern, file)] # Sort in ascending order based on the connected animation index. Zero-indexing or not does not affect order. matches.sort(key=lambda match : int(match.group(1))) # Grab the filename and load it to the file directory - matches = list(map(lambda match : os.path.join(directory, match.group(0)), matches)) - return matches + matches = list(map(lambda match : arcade.load_texture(os.path.join(self.directory, match.group(0))), matches)) + return cycle(matches) class PlayerAnimations(AnimationSet): """