1
1
use crate :: player:: camera:: CameraController ;
2
2
use fyrox:: {
3
3
animation:: {
4
- machine:: { Machine , Parameter , PoseNode , State , Transition } ,
4
+ machine:: { Machine , MachineLayer , Parameter , PoseNode , State , Transition } ,
5
5
Animation ,
6
6
} ,
7
7
core:: {
@@ -12,9 +12,14 @@ use fyrox::{
12
12
event:: { DeviceEvent , ElementState , KeyboardInput , VirtualKeyCode } ,
13
13
resource:: model:: Model ,
14
14
scene:: {
15
- base:: BaseBuilder , collider:: ColliderBuilder , collider:: ColliderShape ,
16
- graph:: physics:: CoefficientCombineRule , node:: Node , rigidbody:: RigidBodyBuilder ,
17
- transform:: TransformBuilder , Scene ,
15
+ animation:: AnimationPlayer ,
16
+ base:: BaseBuilder ,
17
+ collider:: { ColliderBuilder , ColliderShape } ,
18
+ graph:: physics:: CoefficientCombineRule ,
19
+ node:: Node ,
20
+ rigidbody:: RigidBodyBuilder ,
21
+ transform:: TransformBuilder ,
22
+ Scene ,
18
23
} ,
19
24
} ;
20
25
@@ -44,7 +49,7 @@ impl Player {
44
49
. request_model ( "data/models/paladin/paladin.fbx" )
45
50
. await
46
51
. unwrap ( )
47
- . instantiate_geometry ( scene) ;
52
+ . instantiate ( scene) ;
48
53
49
54
// Scale down paladin's model because it is too big.
50
55
scene. graph [ model]
@@ -218,20 +223,20 @@ impl Player {
218
223
fn create_play_animation_state (
219
224
animation_resource : Model ,
220
225
name : & str ,
221
- machine : & mut Machine ,
226
+ layer : & mut MachineLayer ,
222
227
scene : & mut Scene ,
223
228
model : Handle < Node > ,
224
229
) -> ( Handle < Animation > , Handle < State > ) {
225
230
// Animations retargetting just makes an instance of animation and binds it to
226
231
// given model using names of bones.
227
232
let animation = * animation_resource
228
- . retarget_animations ( model, scene)
233
+ . retarget_animations ( model, & mut scene. graph )
229
234
. get ( 0 )
230
235
. unwrap ( ) ;
231
236
// Create new PlayAnimation node and add it to machine.
232
- let node = machine . add_node ( PoseNode :: make_play_animation ( animation) ) ;
237
+ let node = layer . add_node ( PoseNode :: make_play_animation ( animation) ) ;
233
238
// Make a state using the node we've made.
234
- let state = machine . add_state ( State :: new ( name, node) ) ;
239
+ let state = layer . add_state ( State :: new ( name, node) ) ;
235
240
( animation, state)
236
241
}
237
242
@@ -242,6 +247,7 @@ pub struct AnimationMachineInput {
242
247
243
248
pub struct AnimationMachine {
244
249
machine : Machine ,
250
+ animation_player : Handle < Node > ,
245
251
}
246
252
247
253
impl AnimationMachine {
@@ -254,7 +260,13 @@ impl AnimationMachine {
254
260
model : Handle < Node > ,
255
261
resource_manager : ResourceManager ,
256
262
) -> Self {
257
- let mut machine = Machine :: new ( model) ;
263
+ let animation_player = scene. graph . find ( model, & mut |n| {
264
+ n. query_component_ref :: < AnimationPlayer > ( ) . is_some ( )
265
+ } ) ;
266
+
267
+ let mut machine = Machine :: new ( ) ;
268
+
269
+ let root = machine. layers_mut ( ) . first_mut ( ) . unwrap ( ) ;
258
270
259
271
// Load animations in parallel.
260
272
let ( walk_animation_resource, idle_animation_resource) = fyrox:: core:: futures:: join!(
@@ -266,21 +278,21 @@ impl AnimationMachine {
266
278
let ( _, idle_state) = create_play_animation_state (
267
279
idle_animation_resource. unwrap ( ) ,
268
280
"Idle" ,
269
- & mut machine ,
281
+ root ,
270
282
scene,
271
283
model,
272
284
) ;
273
285
274
286
let ( walk_animation, walk_state) = create_play_animation_state (
275
287
walk_animation_resource. unwrap ( ) ,
276
288
"Walk" ,
277
- & mut machine ,
289
+ root ,
278
290
scene,
279
291
model,
280
292
) ;
281
293
282
294
// Next, define transitions between states.
283
- machine . add_transition ( Transition :: new (
295
+ root . add_transition ( Transition :: new (
284
296
// A name for debugging.
285
297
"Idle->Walk" ,
286
298
// Source state.
@@ -292,7 +304,7 @@ impl AnimationMachine {
292
304
// A name of transition rule parameter.
293
305
Self :: IDLE_TO_WALK ,
294
306
) ) ;
295
- machine . add_transition ( Transition :: new (
307
+ root . add_transition ( Transition :: new (
296
308
"Walk->Idle" ,
297
309
walk_state,
298
310
idle_state,
@@ -301,18 +313,25 @@ impl AnimationMachine {
301
313
) ) ;
302
314
303
315
// Define entry state.
304
- machine . set_entry_state ( idle_state) ;
316
+ root . set_entry_state ( idle_state) ;
305
317
306
- Self { machine }
318
+ Self {
319
+ machine,
320
+ animation_player,
321
+ }
307
322
}
308
323
309
324
pub fn update ( & mut self , scene : & mut Scene , dt : f32 , input : AnimationMachineInput ) {
325
+ let animation_player = scene. graph [ self . animation_player ]
326
+ . query_component_ref :: < AnimationPlayer > ( )
327
+ . unwrap ( ) ;
328
+
310
329
self . machine
311
330
// Set transition parameters.
312
331
. set_parameter ( Self :: WALK_TO_IDLE , Parameter :: Rule ( !input. walk ) )
313
332
. set_parameter ( Self :: IDLE_TO_WALK , Parameter :: Rule ( input. walk ) )
314
333
// Update machine and evaluate final pose.
315
- . evaluate_pose ( & scene . animations , dt)
334
+ . evaluate_pose ( animation_player . animations ( ) , dt)
316
335
// Apply the pose to the graph.
317
336
. apply ( & mut scene. graph ) ;
318
337
}
0 commit comments