From f5751f6651fe08135adc6d9defacb5a597d4147b Mon Sep 17 00:00:00 2001 From: Alula <6276139+alula@users.noreply.github.com> Date: Tue, 23 Mar 2021 14:28:51 +0100 Subject: [PATCH] fix weapon being desynchronized with player animation --- src/player/mod.rs | 33 ++++++++++++++++----------------- src/player/skin/basic.rs | 8 +++++--- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/player/mod.rs b/src/player/mod.rs index aa1073a..9f4e614 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -4,7 +4,7 @@ use num_derive::FromPrimitive; use num_traits::clamp; use crate::caret::CaretType; -use crate::common::{interpolate_fix9_scale, Condition, Direction, Equipment, Flag, Rect}; +use crate::common::{Condition, Direction, Equipment, Flag, interpolate_fix9_scale, Rect}; use crate::entity::GameEntity; use crate::frame::Frame; use crate::framework::context::Context; @@ -13,8 +13,8 @@ use crate::input::dummy_player_controller::DummyPlayerController; use crate::input::player_controller::PlayerController; use crate::npc::list::NPCList; use crate::npc::NPC; -use crate::player::skin::basic::BasicPlayerSkin; use crate::player::skin::{PlayerAnimationState, PlayerAppearanceState, PlayerSkin}; +use crate::player::skin::basic::BasicPlayerSkin; use crate::rng::RNG; use crate::shared_game_state::SharedGameState; @@ -550,7 +550,8 @@ impl Player { if self.flags.hit_bottom_wall() { if self.cond.interacted() { self.skin.set_state(PlayerAnimationState::Examining); - self.anim_num = 11; + self.anim_num = 0; + self.anim_counter = 0; } else if state.control_flags.control_enabled() && self.controller.move_up() && (self.controller.move_left() || self.controller.move_right()) @@ -597,7 +598,8 @@ impl Player { self.cond.set_fallen(false); self.skin.set_state(PlayerAnimationState::LookingUp); - self.anim_num = 5; + self.anim_num = 0; + self.anim_counter = 0; } else { if self.cond.fallen() { state.sound_manager.play_sfx(24); @@ -606,20 +608,24 @@ impl Player { self.cond.set_fallen(false); self.skin.set_state(PlayerAnimationState::Idle); self.anim_num = 0; + self.anim_counter = 0; } } else if self.controller.look_up() { self.skin.set_state(PlayerAnimationState::FallingLookingUp); - self.anim_num = 6; + self.anim_num = 0; + self.anim_counter = 0; } else if self.controller.look_down() { self.skin.set_state(PlayerAnimationState::FallingLookingDown); - self.anim_num = 10; + self.anim_num = 0; + self.anim_counter = 0; } else { self.skin.set_state(if self.vel_y > 0 { PlayerAnimationState::Jumping } else { PlayerAnimationState::Falling }); - self.anim_num = if self.vel_y > 0 { 1 } else { 3 }; + self.anim_num = 0; + self.anim_counter = 0; } self.weapon_offset_y = 0; @@ -628,16 +634,9 @@ impl Player { self.weapon_rect.right = self.weapon_rect.left + 24; self.weapon_rect.bottom = self.weapon_rect.top + 16; - match self.direction { - Direction::Left => { - self.anim_rect = state.constants.my_char.frames_left[self.anim_num as usize]; - } - Direction::Right => { - self.weapon_rect.top += 16; - self.weapon_rect.bottom += 16; - self.anim_rect = state.constants.my_char.frames_right[self.anim_num as usize]; - } - _ => {} + if self.direction == Direction::Right { + self.weapon_rect.top += 16; + self.weapon_rect.bottom += 16; } if self.up { diff --git a/src/player/skin/basic.rs b/src/player/skin/basic.rs index df18d40..45adf4d 100644 --- a/src/player/skin/basic.rs +++ b/src/player/skin/basic.rs @@ -31,12 +31,12 @@ impl PlayerSkin for BasicPlayerSkin { PlayerAnimationState::Walking => { const WALK_INDEXES: [u16; 4] = [1, 0, 2, 0]; - WALK_INDEXES[(self.tick as usize / 4) % 4] + WALK_INDEXES[(tick as usize / 5) % 4] } PlayerAnimationState::WalkingUp => { const WALK_UP_INDEXES: [u16; 4] = [4, 3, 5, 3]; - WALK_UP_INDEXES[(self.tick as usize / 4) % 4] + WALK_UP_INDEXES[(tick as usize / 5) % 4] } PlayerAnimationState::LookingUp => 3, PlayerAnimationState::Examining => 7, @@ -64,7 +64,9 @@ impl PlayerSkin for BasicPlayerSkin { } fn tick(&mut self) { - self.tick = self.tick.wrapping_add(1); + if self.state == PlayerAnimationState::Walking || self.state == PlayerAnimationState::WalkingUp { + self.tick = self.tick.wrapping_add(1); + } } fn set_state(&mut self, state: PlayerAnimationState) {