From a5fb8347ee825825750b40a02928cf949d6f6eb0 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 03:09:53 -0500 Subject: [PATCH] switch to AnimationSet implementation for texture loading --- triple-dungeon/mobs.py | 98 +++++++++++++++++++++------------------ triple-dungeon/sprites.py | 8 ++-- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 375cab3..2b4838e 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -45,26 +45,23 @@ class Player(Mob): self.animations = PlayerAnimations(SpritePaths.KNIGHT) self.character_dir = Enums.FRONT_FACING # Face front by default. - # Load textures for idle standing - self.idle_textures = self.animations.idles - self.walking_textures = self.animations. - for i in range(4): - texture = arcade.load_texture(f"{main_path}knight iso char_idle_{i}.png") - self.idle_textures.append(texture) - - # Load textures for running horizontally - for i in range(6): - self.walking_textures.append([arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png"), - arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png", - mirrored=True)]) - - # Load textures for running down - for i in range(5): - self.down_textures.append(arcade.load_texture(f"{main_path}knight iso char_run down_{i}.png")) - - # Load textures for running up - for i in range(5): - self.up_textures.append(arcade.load_texture(f"{main_path}knight iso char_run up_{i}.png")) + # for i in range(4): + # texture = arcade.load_texture(f"{main_path}knight iso char_idle_{i}.png") + # self.idle_textures.append(texture) + # + # # Load textures for running horizontally + # for i in range(6): + # self.walking_textures.append([arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png"), + # arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png", + # mirrored=True)]) + # + # # Load textures for running down + # for i in range(5): + # self.down_textures.append(arcade.load_texture(f"{main_path}knight iso char_run down_{i}.png")) + # + # # Load textures for running up + # for i in range(5): + # self.up_textures.append(arcade.load_texture(f"{main_path}knight iso char_run up_{i}.png")) def update_animation(self, delta_time: float = 1 / 60) -> None: """ @@ -73,34 +70,45 @@ class Player(Mob): """ # Figure out if we need to flip face left, right, up, or down - if self.change_x > 0: - self.character_dir = Enums.LEFT_FACING - elif self.change_x < 0: - self.character_dir = Enums.RIGHT_FACING - elif self.change_x == 0 and self.change_y == 0: - self.character_dir = Enums.FRONT_FACING + # if self.change_x > 0: + # self.character_dir = Enums.LEFT_FACING + # elif self.change_x < 0: + # self.character_dir = Enums.RIGHT_FACING + # elif self.change_x == 0 and self.change_y == 0: + # self.character_dir = Enums.FRONT_FACING + + if self.change_x == 0 and self.change_y == 0: + self.texture = next(self.animations.idles) + elif self.change_y > 0: # Up + self.texture = next(self.animations.up) + elif self.change_y < 0: # Down + self.texture = next(self.animations.down) + elif self.change_x > 0: # Left + self.texture = next(self.animations.right) + elif self.change_x < 0: # Right + self.texture = next(self.animations.left) # Idle Animation - if self.change_x == 0 and self.change_y == 0: - self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 3) - self.texture = self.idle_textures[self.cur_texture // Config.IDLE_UPDATES_PER_FRAME] - return + # if self.change_x == 0 and self.change_y == 0: + # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 3) + # self.texture = self.idle_textures[self.cur_texture // Config.IDLE_UPDATES_PER_FRAME] + # return + # + # # walk up animation + # if self.change_y > 0: + # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) + # self.texture = self.up_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] + # return + # + # # walk down animation + # if self.change_y < 0: + # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) + # self.texture = self.down_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] + # return - # walk up animation - if self.change_y > 0: - self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) - self.texture = self.up_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] - return - - # walk down animation - if self.change_y < 0: - self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) - self.texture = self.down_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] - return - - # Walking left/right animation - self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 5) - self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_dir.value] + # # Walking left/right animation + # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 5) + # self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_dir.value] def tick(self): """ diff --git a/triple-dungeon/sprites.py b/triple-dungeon/sprites.py index b47fc39..2b5f5f9 100644 --- a/triple-dungeon/sprites.py +++ b/triple-dungeon/sprites.py @@ -2,6 +2,7 @@ sprites.py A file dedicated to managing sprites and animations for characters. """ +from itertools import cycle import arcade import os @@ -35,12 +36,13 @@ class AnimationSet(object): """ # Finds all matching files - matches = [file for file in self.animations if re.match(pattern, file)] + matches = map(lambda file: re.match(pattern, file), self.animations) + matches = list(filter(lambda match: match is not None, matches)) # 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: arcade.load_texture(os.path.join(self.directory, match.group(0))), matches)) - return matches + return cycle(matches) class PlayerAnimations(AnimationSet): @@ -65,4 +67,4 @@ class PlayerAnimations(AnimationSet): self.down = self.getAnimations(re.compile(r'run_down_(\d+).png')) self.right = self.getAnimations(re.compile(r'run_right_(\d+).png')) self.up = self.getAnimations(re.compile(r'run_up_(\d+).png')) - self.down = self.getAnimations(re.compile(r'run_left_(\d+).png')) + self.left = self.getAnimations(re.compile(r'run_left_(\d+).png'))