From 424892cc2516bfc8ed7e7e4cce194bd1dfd28769 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 03:03:38 -0500 Subject: [PATCH] use modulus for animations in mobs.py, beginning to implement PlayerAnimations class, add more docstrings to PlayerAnimations, remove cycle --- triple-dungeon/mobs.py | 27 ++++++++++----------------- triple-dungeon/sprites.py | 12 ++++++++---- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 741bffe..375cab3 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -5,7 +5,8 @@ Organizes all classes related to Mobs, Entities, Enemies, Players and Items. import arcade -from config import Config, Enums +from config import Config, Enums, SpritePaths +from sprites import PlayerAnimations class Mob(arcade.Sprite): @@ -41,12 +42,12 @@ class Player(Mob): def __init__(self, *args, **kwargs) -> None: super(Player, self).__init__(*args, **kwargs) - main_path = "resources/images/character/knight/" - - # Default to face-front - self.character_dir = Enums.FRONT_FACING + 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) @@ -81,32 +82,24 @@ class Player(Mob): # Idle Animation if self.change_x == 0 and self.change_y == 0: - self.cur_texture += 1 - if self.cur_texture > 3 * Config.IDLE_UPDATES_PER_FRAME: - self.cur_texture = 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 += 1 - if self.cur_texture > 4 * Config.RUN_UPDATES_PER_FRAME: - self.cur_texture = 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 += 1 - if self.cur_texture > 4 * Config.RUN_UPDATES_PER_FRAME: - self.cur_texture = 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 += 1 - if self.cur_texture > 5 * Config.RUN_UPDATES_PER_FRAME: - self.cur_texture = 0 + 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 ea3301d..b47fc39 100644 --- a/triple-dungeon/sprites.py +++ b/triple-dungeon/sprites.py @@ -7,7 +7,7 @@ import arcade import os import re -from itertools import cycle +from typing import Pattern class AnimationSet(object): @@ -25,7 +25,7 @@ class AnimationSet(object): self.directory = directory self.animations = os.listdir(directory) - def getAnimations(self, pattern: re.Pattern) -> iter: + def getAnimations(self, pattern: 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. @@ -40,20 +40,24 @@ class AnimationSet(object): 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 cycle(matches) + return matches class PlayerAnimations(AnimationSet): """ A class dedicated to serving player animations. Player animations must be served to the class in the correct format. + + The correct format is: {action}[_{direction}]_{index}.png + action: [idle, run, slice] - The action being taken. + direction: [down, right, left, up] - The direction of the action, if applicable. Omit the underscore if not. + index: [0,) - The index of the animation. Index should be enumerated in ascending order. """ def __init__(self, directory: str): """ Initializes the PlayerAnimations class. """ - super(PlayerAnimations, self).__init__(directory) # Grabs all animations needed. These are infinite iters, use next(iter) to grab the next animation.