From 2280e816290c5b6f6b6418f438ae83e46814fd6a Mon Sep 17 00:00:00 2001 From: Skyball2000 Date: Wed, 29 Mar 2023 07:37:50 +0200 Subject: [PATCH] player animation will now update based on player input --- project/physics/sprites/PlayerSprite.py | 24 +++++++++++++++++++++++- project/sprite/Sprite.py | 14 ++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/project/physics/sprites/PlayerSprite.py b/project/physics/sprites/PlayerSprite.py index 652f9e9..6e80bab 100644 --- a/project/physics/sprites/PlayerSprite.py +++ b/project/physics/sprites/PlayerSprite.py @@ -19,17 +19,22 @@ class PlayerSprite(DynamicSprite): self.gravity = 9.81 / 10 self.max_motion_horizontal_via_input = 5 - self.id = 'player' + # animation properties + self.no_motion_since = 100 + self.last_directional_input = KeyManager.KEY_RIGHT + def tick(self, tick_data: TickData): super().tick(tick_data) if tick_data.key_manager.is_keymap_down(KeyManager.KEY_RIGHT): + self.last_directional_input = KeyManager.KEY_RIGHT if self.motion[0] < self.max_motion_horizontal_via_input: self.motion = (self.motion[0] + self.acceleration_horizontal, self.motion[1]) if tick_data.key_manager.is_keymap_down(KeyManager.KEY_LEFT): + self.last_directional_input = KeyManager.KEY_LEFT if self.motion[0] > -self.max_motion_horizontal_via_input: self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1]) @@ -41,3 +46,20 @@ class PlayerSprite(DynamicSprite): self.motion = (self.motion[0], self.motion[1] - 0.55) if self.jump_time >= 0: self.jump_time -= 1 + + + # update animation state + if self.last_directional_input == KeyManager.KEY_RIGHT: + animation_dir = 'r' + else: + animation_dir = 'l' + + if abs(self.motion[0]) < 0.1: + self.no_motion_since += tick_data.dt + else: + self.no_motion_since = 0 + + if self.no_motion_since > 20: + self.set_animation_state('idle_' + animation_dir) + else: + self.set_animation_state('walk_' + animation_dir) diff --git a/project/sprite/Sprite.py b/project/sprite/Sprite.py index 9393eee..8116c5e 100644 --- a/project/sprite/Sprite.py +++ b/project/sprite/Sprite.py @@ -48,6 +48,9 @@ class Sprite(UiElement): return None def tick(self, tick_data: TickData): + self.update_image(tick_data) + + def update_image(self, tick_data: TickData): if self.spritesheet is None: self.image = None return @@ -64,23 +67,22 @@ class Sprite(UiElement): self.last_image = self.image self.image = animation['images'][self.animation_frame % len(animation['images'])] + empty_tick_data = TickData(0, [], KeyManager(), [], PositionScale()) + def set_animation_state(self, state: str): - if self.spritesheet is None: + if self.spritesheet is None or self.animation_state == state: return if state in self.spritesheet.animations: self.animation_state = state self.animation_delay = 0 - self.tick(self.empty_tick_data()) + self.update_image(self.empty_tick_data) else: print('Unknown animation state: ' + state) def set_animation_frame(self, frame: int): self.animation_frame = frame - self.tick(self.empty_tick_data()) - - def empty_tick_data(self) -> TickData: - return TickData(0, [], KeyManager(), [], PositionScale()) + self.update_image(self.empty_tick_data) def render_sprite_image(self) -> pygame.Surface: return self.image