forked from janhohenheim/foxtrot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.rs
148 lines (141 loc) · 5.23 KB
/
animation.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use crate::system_set::GameSystemSet;
use crate::util::error;
use bevy::{animation::AnimationPlayer, prelude::*};
use bevy_gltf_blueprints::{AnimationPlayerLink, Animations};
use bevy_tnua::{
builtins::TnuaBuiltinWalk, controller::TnuaController, TnuaAnimatingState,
TnuaAnimatingStateDirective,
};
use std::time::Duration;
pub(super) fn plugin(app: &mut App) {
app.register_type::<CharacterAnimationNames>().add_systems(
Update,
play_animations
.pipe(error)
.in_set(GameSystemSet::PlayAnimation),
);
}
/// Managed by [`play_animations`]
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum AnimationState {
Standing,
Airborne,
Walking(f32),
Running(f32),
}
#[derive(Debug, Clone, PartialEq, Component, Reflect, Default)]
#[reflect(Component)]
struct CharacterAnimationNames {
idle: String,
walk: String,
aerial: String,
}
fn play_animations(
mut query: Query<(
Entity,
&mut TnuaAnimatingState<AnimationState>,
&TnuaController,
&AnimationPlayerLink,
&Animations,
)>,
children: Query<&Children>,
animation_names: Query<&CharacterAnimationNames>,
mut animation_players: Query<&mut AnimationPlayer>,
) -> anyhow::Result<()> {
#[cfg(feature = "tracing")]
let _span = info_span!("play_animations").entered();
for (entity, mut animating_state, controller, link, animations) in query.iter_mut() {
let Some(animation_names) = children
.iter_descendants(entity)
.filter_map(|entity| animation_names.get(entity).ok())
.next()
else {
continue;
};
let mut animation_player = animation_players.get_mut(link.0)?;
match animating_state.update_by_discriminant({
let Some((_, basis_state)) = controller.concrete_basis::<TnuaBuiltinWalk>() else {
continue;
};
let speed = basis_state.running_velocity.length();
if controller.is_airborne()? {
AnimationState::Airborne
} else if speed > 10.0 {
AnimationState::Running(speed)
} else if speed > 0.01 {
AnimationState::Walking(speed)
} else {
AnimationState::Standing
}
}) {
TnuaAnimatingStateDirective::Maintain { state } => {
if let AnimationState::Running(speed) = state {
let anim_speed = (speed / 7.0).max(1.0);
animation_player.adjust_speeds(anim_speed);
}
}
TnuaAnimatingStateDirective::Alter {
// We don't need the old state here, but it's available for transition
// animations.
old_state: _,
state,
} => match state {
AnimationState::Airborne | AnimationState::Running(..) => {
animation_player
.play(
*animations.named_indices
.get(&animation_names.aerial)
.unwrap()
).repeat().set_speed(0.2);
// animation_player
// .play_with_transition(
// animations
// .named_animations
// .get(&animation_names.aerial)
// .unwrap()
// .clone_weak(),
// Duration::from_secs_f32(0.2),
// )
// .repeat();
}
AnimationState::Standing => {
animation_player
.play(
*animations.named_indices
.get(&animation_names.idle)
.unwrap()
).repeat().set_speed(0.2);
// animation_player
// .play_with_transition(
// animations
// .named_animations
// .get(&animation_names.idle)
// .unwrap()
// .clone_weak(),
// Duration::from_secs_f32(0.2),
// )
// .repeat();
}
AnimationState::Walking(_speed) => {
animation_player
.play(
*animations.named_indices
.get(&animation_names.walk)
.unwrap()
).repeat().set_speed(0.1);
// animation_player
// .play_with_transition(
// animations
// .named_animations
// .get(&animation_names.walk)
// .unwrap()
// .clone_weak(),
// Duration::from_secs_f32(0.1),
// )
// .repeat();
}
},
}
}
Ok(())
}