player walk animation

This commit is contained in:
2025-03-07 11:17:24 +01:00
parent e50d3009ca
commit 323e644ff4
2 changed files with 70 additions and 7 deletions

View File

@@ -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::<PlayerSpawned>();
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<Entity, Added<AnimationGraphHandle>>,
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<Animations>,
mut transitions: Query<
(&mut AnimationTransitions, &mut AnimationPlayer),
With<PlayerAnimations>,
>,
keys: Res<ButtonInput<KeyCode>>,
) {
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();
}
}
}