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
149
150
151
152
153
154
155
156
157
158
159
160
161
use discriminant;
use *;
/// Utility for deciding which animation to play.
///
/// Add `TnuaAnimatingState<State>` as a component, where `State` is a data type - usually an
/// `enum` - that determines which animation to play. Each frame, decide (with the help of
/// [`TnuaController`](crate::prelude::TnuaController)) which animation should
/// be played and the animation's parameters (like speed) and feed it to the `TnuaAnimatingState`.
/// Use the emitted [`TnuaAnimatingStateDirective`] to determine if this is a new animation or an
/// existing one (possibly with different parameters), and use that information to work the actual
/// animation player.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_tnua::prelude::*;
/// # use bevy_tnua::{TnuaAnimatingState, TnuaAnimatingStateDirective};
/// # use bevy_tnua::math::Float;
/// # #[derive(Resource)]
/// # struct AnimationNodes {
/// # standing: AnimationNodeIndex,
/// # running: AnimationNodeIndex,
/// # }
/// # #[derive(bevy_tnua::TnuaScheme)] #[scheme(basis = bevy_tnua::builtins::TnuaBuiltinWalk)] enum ControlScheme {}
/// enum AnimationState {
/// Standing,
/// Running(Float),
/// }
///
/// fn animating_system(
/// mut query: &mut Query<(
/// &mut TnuaAnimatingState<AnimationState>,
/// &TnuaController<ControlScheme>,
/// &mut AnimationPlayer,
/// )>,
/// animation_nodes: Res<AnimationNodes>,
/// ) {
/// for (mut animating_state, controller, mut animation_player) in query.iter_mut() {
/// match animating_state.update_by_discriminant({
/// let speed = controller.basis_memory.running_velocity.length();
/// if 0.01 < speed {
/// AnimationState::Running(speed)
/// } else {
/// AnimationState::Standing
/// }
/// }) {
/// TnuaAnimatingStateDirective::Maintain { state } => {
/// if let AnimationState::Running(speed) = state {
/// if let Some(active_animation) = animation_player.animation_mut(animation_nodes.running) {
/// active_animation.set_speed(*speed);
/// }
/// }
/// }
/// TnuaAnimatingStateDirective::Alter {
/// // We don't need the old state here, but it's available for transition
/// // animations.
/// old_state: _,
/// state,
/// } => {
/// animation_player.stop_all();
/// match state {
/// AnimationState::Standing => {
/// animation_player
/// .start(animation_nodes.standing)
/// .set_speed(1.0)
/// .repeat();
/// }
/// AnimationState::Running(speed) => {
/// animation_player
/// .start(animation_nodes.running)
/// .set_speed(*speed)
/// .repeat();
/// }
/// }
/// }
/// }
/// }
/// }
/// ```