mirror of
https://github.com/n0remac/game-jam-2020.git
synced 2025-12-10 06:05:20 -06:00
add infinite cycle iter and arcade texture object loading, fix docstrings
This commit is contained in:
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user