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,
},
},
utils::HashSet,
};
fn main() {
@ -32,7 +33,8 @@ fn main() {
.add_startup_system(spawn_base_ui)
.add_system(spawn_models)
.add_system(spawn_ui)
.add_system(spawn_animation)
.add_system(catalog_animations)
.add_system(play_animation)
.add_system(rotate_model)
.add_system(zoom_model)
.add_system(scroll)
@ -45,7 +47,7 @@ fn main() {
/// Stores GLTF handles for later use
#[derive(Resource)]
struct Models {
_handles: Vec<Handle<Gltf>>,
handles: Vec<Handle<Gltf>>,
}
///
@ -80,12 +82,12 @@ struct ManageActive(Option<Entity>);
/// Load all GLTF models on startup
fn load_models(mut commands: Commands, ass: Res<AssetServer>) {
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()
.map(|weak| weak.clone().typed::<Gltf>())
.collect();
// info!("Scene Handles: {:#?}", _handles);
commands.insert_resource(Models { _handles });
// info!("Scene Handles: {:#?}", handles);
commands.insert_resource(Models { handles });
}
///
@ -274,10 +276,10 @@ fn spawn_models(
));
});
}
AssetEvent::Removed { handle: _handle } => {
AssetEvent::Removed { .. } => {
todo!("Remove deleted scene")
}
AssetEvent::Modified { handle: _handle } => {
AssetEvent::Modified { .. } => {
todo!("Update modified scene")
}
}
@ -321,26 +323,55 @@ fn spawn_ui(
}
}
fn spawn_animation(
mut animation_evr: EventReader<AssetEvent<AnimationClip>>,
mut player: Query<&mut AnimationPlayer>,
mut playing: Local<Vec<Handle<AnimationClip>>>,
fn catalog_animations(
mut events: EventReader<AssetEvent<AnimationClip>>,
clips: Res<Assets<AnimationClip>>,
) {
for event in animation_evr.iter() {
for event in events.iter() {
match event {
AssetEvent::Created { handle } => {
if !playing.contains(handle) {
if let Ok(mut player) = player.get_single_mut() {
player.start(handle.clone()).repeat();
(*playing).push(handle.clone());
if let Some(clip) = clips.get(handle) {
info!("Event: {:#?}", clip.curves());
}
}
_ => (),
}
}
}
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 {
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)) => {
for child in query.iter_descendants(*entity) {
if let Some(mut entity_commands) = commands.get_entity(child) {
// entity_commands.log_components();
entity_commands.insert(Active);
if let Some(mut child_commands) = commands.get_entity(child) {
// info!(
// "Active entity components: {:?} {:?}",
// child,
// names.get(child)
// );
// child_commands.log_components();
child_commands.insert(Active);
}
}
}

Loading…
Cancel
Save