From 323e644ff4921248dbf3f1aed656b3682d0a4551 Mon Sep 17 00:00:00 2001 From: extrawurst Date: Fri, 7 Mar 2025 11:17:24 +0100 Subject: [PATCH] player walk animation --- src/alien.rs | 6 ++--- src/player.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/alien.rs b/src/alien.rs index 085a75b..cec64b8 100644 --- a/src/alien.rs +++ b/src/alien.rs @@ -4,9 +4,9 @@ use std::time::Duration; pub const ALIEN_ASSET_PATH: &str = "models/alien_naked.glb"; #[derive(Resource)] -struct Animations { - animations: Vec, - graph: Handle, +pub struct Animations { + pub animations: Vec, + pub graph: Handle, } pub fn plugin(app: &mut App) { diff --git a/src/player.rs b/src/player.rs index 2a750f3..10adbac 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,5 +1,7 @@ +use std::time::Duration; + use crate::{ - alien::ALIEN_ASSET_PATH, + alien::{ALIEN_ASSET_PATH, Animations}, camera::GameCameraRig, cash::{Cash, CashCollectEvent}, tb_entities::SpawnPoint, @@ -17,6 +19,9 @@ use bevy_tnua_avian3d::TnuaAvian3dSensorShape; #[derive(Component, Default)] pub struct Player; +#[derive(Component, Default)] +struct PlayerAnimations; + #[derive(Resource, Default)] struct PlayerSpawned { spawned: bool, @@ -25,7 +30,17 @@ struct PlayerSpawned { pub fn plugin(app: &mut App) { app.init_resource::(); app.add_systems(Startup, initial_grab_cursor); - app.add_systems(Update, (spawn, update_camera, cursor_events, collect_cash)); + app.add_systems( + Update, + ( + spawn, + update_camera, + cursor_events, + collect_cash, + toggle_animation, + setup_animations_marker_for_player, + ), + ); app.add_systems( FixedUpdate, apply_controls.in_set(TnuaUserControlsSystemSet), @@ -66,13 +81,13 @@ fn spawn( )) .with_child(( Name::from("head"), - Transform::from_translation(Vec3::new(0., 1.0, 0.)) + Transform::from_translation(Vec3::new(0., -0.5, 0.)) .with_rotation(Quat::from_rotation_y(std::f32::consts::PI)), SceneRoot(mesh), )) .with_child(( Name::from("body rig"), - Transform::from_translation(Vec3::new(0., -1., 0.)) + Transform::from_translation(Vec3::new(0., -3., 0.)) .with_rotation(Quat::from_rotation_y(std::f32::consts::PI)) .with_scale(Vec3::splat(1.5)), SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset(ALIEN_ASSET_PATH))), @@ -200,3 +215,51 @@ fn collect_cash( } } } + +fn setup_animations_marker_for_player( + mut commands: Commands, + animation_handles: Query>, + parent_query: Query<&Parent>, + player: Query<&Player>, +) { + for entity in animation_handles.iter() { + for ancestor in parent_query.iter_ancestors(entity) { + if player.contains(ancestor) { + commands.entity(entity).insert(PlayerAnimations); + } + } + } +} + +fn toggle_animation( + animations: Res, + mut transitions: Query< + (&mut AnimationTransitions, &mut AnimationPlayer), + With, + >, + keys: 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 keys.just_released(KeyCode::KeyW) { + for (mut transition, mut player) in &mut transitions { + transition + .play( + &mut player, + animations.animations[1], + Duration::from_millis(100), + ) + .repeat(); + } + } +}