Get selected animation working

Animation API is good but incomplete.
Cannot find animations related to an entity which means multiple
animations would likely break this flow.

With more precise asset control of Animations + Entities it would be
fine, like in the example, but not "Load the GLTF and figure it out in
the code" like I'm trying to do.
main
Elijah Voigt 2 years ago
parent c7209ee246
commit 95c6eae828

@ -14,6 +14,7 @@ use bevy::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
}, },
}, },
utils::HashSet,
}; };
fn main() { fn main() {
@ -32,7 +33,8 @@ fn main() {
.add_startup_system(spawn_base_ui) .add_startup_system(spawn_base_ui)
.add_system(spawn_models) .add_system(spawn_models)
.add_system(spawn_ui) .add_system(spawn_ui)
.add_system(spawn_animation) .add_system(catalog_animations)
.add_system(play_animation)
.add_system(rotate_model) .add_system(rotate_model)
.add_system(zoom_model) .add_system(zoom_model)
.add_system(scroll) .add_system(scroll)
@ -45,7 +47,7 @@ fn main() {
/// Stores GLTF handles for later use /// Stores GLTF handles for later use
#[derive(Resource)] #[derive(Resource)]
struct Models { struct Models {
_handles: Vec<Handle<Gltf>>, handles: Vec<Handle<Gltf>>,
} }
/// ///
@ -80,12 +82,12 @@ struct ManageActive(Option<Entity>);
/// Load all GLTF models on startup /// Load all GLTF models on startup
fn load_models(mut commands: Commands, ass: Res<AssetServer>) { fn load_models(mut commands: Commands, ass: Res<AssetServer>) {
let weak_handles = ass.load_folder("models").expect("Load gltfs"); let weak_handles = ass.load_folder("models").expect("Load gltfs");
let _handles: Vec<Handle<Gltf>> = weak_handles let handles: Vec<Handle<Gltf>> = weak_handles
.iter() .iter()
.map(|weak| weak.clone().typed::<Gltf>()) .map(|weak| weak.clone().typed::<Gltf>())
.collect(); .collect();
// info!("Scene Handles: {:#?}", _handles); // info!("Scene Handles: {:#?}", handles);
commands.insert_resource(Models { _handles }); commands.insert_resource(Models { handles });
} }
/// ///
@ -274,10 +276,10 @@ fn spawn_models(
)); ));
}); });
} }
AssetEvent::Removed { handle: _handle } => { AssetEvent::Removed { .. } => {
todo!("Remove deleted scene") todo!("Remove deleted scene")
} }
AssetEvent::Modified { handle: _handle } => { AssetEvent::Modified { .. } => {
todo!("Update modified scene") todo!("Update modified scene")
} }
} }
@ -321,26 +323,55 @@ fn spawn_ui(
} }
} }
fn spawn_animation( fn catalog_animations(
mut animation_evr: EventReader<AssetEvent<AnimationClip>>, mut events: EventReader<AssetEvent<AnimationClip>>,
mut player: Query<&mut AnimationPlayer>,
mut playing: Local<Vec<Handle<AnimationClip>>>,
clips: Res<Assets<AnimationClip>>, clips: Res<Assets<AnimationClip>>,
) { ) {
for event in animation_evr.iter() { for event in events.iter() {
match event { match event {
AssetEvent::Created { handle } => { AssetEvent::Created { handle } => {
if !playing.contains(handle) { if let Some(clip) = clips.get(handle) {
if let Ok(mut player) = player.get_single_mut() { info!("Event: {:#?}", clip.curves());
player.start(handle.clone()).repeat(); }
(*playing).push(handle.clone()); }
_ => (),
}
}
}
fn play_animation(
mut events: EventReader<ManageActive>,
mut players: Query<&mut AnimationPlayer>,
children: Query<&Children>,
models: Res<Models>,
gltfs: Res<Assets<Gltf>>,
) {
for event in events.iter() {
match event {
ManageActive(None) => {
info!("Disabling all animations");
for mut player in players.iter_mut() {
player.stop_repeating();
}
}
ManageActive(Some(entity)) => {
info!("Setting {:?} Active Animation", entity);
for child in children.iter_descendants(*entity) {
if let Ok(mut player) = players.get_mut(child) {
for gltf_handle in models.handles.iter() {
if let Some(gltf) = gltfs.get(gltf_handle) {
for animation_handle in gltf.animations.iter() {
player.start(animation_handle.clone()).repeat();
}
} else {
info!("Failed to get GLTF handle");
}
}
} else { } else {
info!("Could not load animation player"); info!("Failed to get active animation player");
} }
} }
} }
AssetEvent::Removed { .. } => (),
AssetEvent::Modified { .. } => (),
} }
} }
} }
@ -494,9 +525,14 @@ fn manage_active(
} }
ManageActive(Some(entity)) => { ManageActive(Some(entity)) => {
for child in query.iter_descendants(*entity) { for child in query.iter_descendants(*entity) {
if let Some(mut entity_commands) = commands.get_entity(child) { if let Some(mut child_commands) = commands.get_entity(child) {
// entity_commands.log_components(); // info!(
entity_commands.insert(Active); // "Active entity components: {:?} {:?}",
// child,
// names.get(child)
// );
// child_commands.log_components();
child_commands.insert(Active);
} }
} }
} }

Loading…
Cancel
Save