diff --git a/src/player.rs b/src/player.rs index 677d72d..ba4e791 100644 --- a/src/player.rs +++ b/src/player.rs @@ -32,8 +32,14 @@ struct PlayerSpawned { spawned: bool, } +#[derive(Resource, Default)] +struct PlayerMovement { + any_direction: bool, +} + pub fn plugin(app: &mut App) { app.init_resource::(); + app.init_resource::(); app.add_systems(Startup, initial_grab_cursor); app.add_systems( Update, @@ -147,6 +153,7 @@ fn apply_controls( keyboard: Res>, mut query: Query<&mut TnuaController>, player: Query<&Transform, With>, + mut controls: ResMut, ) { let Ok(mut controller) = query.get_single_mut() else { return; @@ -171,6 +178,10 @@ fn apply_controls( direction += player.right().as_vec3(); } + if controls.any_direction != (direction != Vec3::ZERO) { + controls.any_direction = direction != Vec3::ZERO; + } + controller.basis(TnuaBuiltinWalk { // The `desired_velocity` determines how the character will move. desired_velocity: direction.normalize_or_zero() * 8.0, @@ -254,26 +265,16 @@ fn toggle_animation( (&mut AnimationTransitions, &mut AnimationPlayer), With, >, - keys: Res>, + movement: Res, ) { - if keys.just_pressed(KeyCode::KeyW) { - for (mut transition, mut player) in &mut transitions { - transition - .play( - &mut player, - animations.animations[0], - Duration::from_millis(100), - ) - .repeat(); - } - } + if movement.is_changed() { + let index = if movement.any_direction { 0 } else { 1 }; - if keys.just_released(KeyCode::KeyW) { for (mut transition, mut player) in &mut transitions { transition .play( &mut player, - animations.animations[1], + animations.animations[index], Duration::from_millis(100), ) .repeat();