mirror of
https://github.com/n0remac/game-jam-2020.git
synced 2025-12-16 10:10:00 -06:00
switch to AnimationSet implementation for texture loading
This commit is contained in:
@@ -45,26 +45,23 @@ class Player(Mob):
|
|||||||
self.animations = PlayerAnimations(SpritePaths.KNIGHT)
|
self.animations = PlayerAnimations(SpritePaths.KNIGHT)
|
||||||
self.character_dir = Enums.FRONT_FACING # Face front by default.
|
self.character_dir = Enums.FRONT_FACING # Face front by default.
|
||||||
|
|
||||||
# Load textures for idle standing
|
# for i in range(4):
|
||||||
self.idle_textures = self.animations.idles
|
# texture = arcade.load_texture(f"{main_path}knight iso char_idle_{i}.png")
|
||||||
self.walking_textures = self.animations.
|
# self.idle_textures.append(texture)
|
||||||
for i in range(4):
|
#
|
||||||
texture = arcade.load_texture(f"{main_path}knight iso char_idle_{i}.png")
|
# # Load textures for running horizontally
|
||||||
self.idle_textures.append(texture)
|
# for i in range(6):
|
||||||
|
# self.walking_textures.append([arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png"),
|
||||||
# Load textures for running horizontally
|
# arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png",
|
||||||
for i in range(6):
|
# mirrored=True)])
|
||||||
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",
|
# # Load textures for running down
|
||||||
mirrored=True)])
|
# 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 down
|
#
|
||||||
for i in range(5):
|
# # Load textures for running up
|
||||||
self.down_textures.append(arcade.load_texture(f"{main_path}knight iso char_run down_{i}.png"))
|
# for i in range(5):
|
||||||
|
# self.up_textures.append(arcade.load_texture(f"{main_path}knight iso char_run up_{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:
|
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
|
# Figure out if we need to flip face left, right, up, or down
|
||||||
if self.change_x > 0:
|
# if self.change_x > 0:
|
||||||
self.character_dir = Enums.LEFT_FACING
|
# self.character_dir = Enums.LEFT_FACING
|
||||||
elif self.change_x < 0:
|
# elif self.change_x < 0:
|
||||||
self.character_dir = Enums.RIGHT_FACING
|
# self.character_dir = Enums.RIGHT_FACING
|
||||||
elif self.change_x == 0 and self.change_y == 0:
|
# elif self.change_x == 0 and self.change_y == 0:
|
||||||
self.character_dir = Enums.FRONT_FACING
|
# 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
|
# Idle Animation
|
||||||
if self.change_x == 0 and self.change_y == 0:
|
# if self.change_x == 0 and self.change_y == 0:
|
||||||
self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 3)
|
# 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]
|
# self.texture = self.idle_textures[self.cur_texture // Config.IDLE_UPDATES_PER_FRAME]
|
||||||
return
|
# 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
|
# # Walking left/right animation
|
||||||
if self.change_y > 0:
|
# self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 5)
|
||||||
self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4)
|
# self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_dir.value]
|
||||||
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]
|
|
||||||
|
|
||||||
def tick(self):
|
def tick(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -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.
|
||||||
"""
|
"""
|
||||||
|
from itertools import cycle
|
||||||
|
|
||||||
import arcade
|
import arcade
|
||||||
import os
|
import os
|
||||||
@@ -35,12 +36,13 @@ class AnimationSet(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Finds all matching files
|
# 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.
|
# 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)))
|
matches.sort(key=lambda match: int(match.group(1)))
|
||||||
# Grab the filename and load it to the file directory
|
# 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))
|
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):
|
class PlayerAnimations(AnimationSet):
|
||||||
@@ -65,4 +67,4 @@ class PlayerAnimations(AnimationSet):
|
|||||||
self.down = self.getAnimations(re.compile(r'run_down_(\d+).png'))
|
self.down = self.getAnimations(re.compile(r'run_down_(\d+).png'))
|
||||||
self.right = self.getAnimations(re.compile(r'run_right_(\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.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'))
|
||||||
|
|||||||
Reference in New Issue
Block a user