mirror of
https://github.com/n0remac/game-jam-2020.git
synced 2025-12-14 22:09:55 -06:00
implement getAnimations, correct regex patterns with capturing groups
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
sprites.py
|
sprites.py
|
||||||
A file dedicated to managing sprites and animations for characters.
|
A file dedicated to managing sprites and animations for characters.
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
@@ -16,8 +17,17 @@ class AnimationSet(object):
|
|||||||
|
|
||||||
:param directory: A directory containing valid animation files in the correct format.
|
:param directory: A directory containing valid animation files in the correct format.
|
||||||
"""
|
"""
|
||||||
pass
|
|
||||||
|
|
||||||
|
self.animations = os.path.listdir(directory)
|
||||||
|
|
||||||
|
def getAnimations(self, pattern: re.Pattern) -> iter:
|
||||||
|
# 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
|
||||||
|
|
||||||
class PlayerAnimations(AnimationSet):
|
class PlayerAnimations(AnimationSet):
|
||||||
"""
|
"""
|
||||||
@@ -32,10 +42,11 @@ class PlayerAnimations(AnimationSet):
|
|||||||
|
|
||||||
super(PlayerAnimations, self).__init__()
|
super(PlayerAnimations, self).__init__()
|
||||||
|
|
||||||
self.idles = self.getAnimations(re.compile(r'idle_\d+.png'))
|
# Grabs all animations needed. These are infinite iters, use next(iter) to grab the next animation.
|
||||||
self.down = self.getAnimations(re.compile(r'run_down_\d+.png'))
|
self.idles = self.getAnimations(re.compile(r'idle_(\d+).png'))
|
||||||
self.right = self.getAnimations(re.compile(r'run_right_\d+.png'))
|
self.down = self.getAnimations(re.compile(r'run_down_(\d+).png'))
|
||||||
self.up = self.getAnimations(re.compile(r'run_up_\d+.png'))
|
self.right = self.getAnimations(re.compile(r'run_right_(\d+).png'))
|
||||||
self.down = self.getAnimations(re.compile(r'run_left_\d+.png'))
|
self.up = self.getAnimations(re.compile(r'run_up_(\d+).png'))
|
||||||
|
self.down = self.getAnimations(re.compile(r'run_left_(\d+).png'))
|
||||||
|
|
||||||
def __loadAnimations(self):
|
def __loadAnimations(self):
|
||||||
Reference in New Issue
Block a user