diff --git a/studio-frontend/src/app/app.element.ts b/studio-frontend/src/app/app.element.ts index 989d64f..3453133 100644 --- a/studio-frontend/src/app/app.element.ts +++ b/studio-frontend/src/app/app.element.ts @@ -208,7 +208,7 @@ export class AppElement extends LitElement { > ` : html` >, + r_assets: Res, + ra_gltfs: Res>, + ra_anims: Res>, + q_prefabs: Query<&GltfPrefab>, + q_named_ents: Query<(Entity, &Name)>, + mut q_players: Query<&mut AnimationPlayer>, +) { + for prefab in q_prefabs.iter() { + if l_done.contains(&prefab.path) { + continue; + } + + // TODO: Get the `Handle` directly from the entity once it's available + let gltf_handle = match r_assets.get_handle::(&prefab.path) { + Some(handle) => { + info!("Returning existing handle"); + handle + } + None => { + info!("Loading from path"); + r_assets.load::(&prefab.path) + } + }; + + let Some(gltf) = ra_gltfs.get(gltf_handle) else { + continue; + }; + + let mut playing_anims = 0_usize; + for anim_handle in gltf.animations.iter() { + let Some(anim) = ra_anims.get(anim_handle.clone()) else { + continue; + }; + + let Some(target) = q_named_ents.iter().find_map(|(ent, name)| { + if anim.compatible_with(name) { + Some(ent) + } else { + None + } + }) else { + continue; + }; + + if let Ok(mut player) = q_players.get_mut(target) { + if !player.is_playing_clip(anim_handle) { + player.play(anim_handle.clone()).repeat(); + } + } else { + let mut player = AnimationPlayer::default(); + player.play(anim_handle.clone()).repeat(); + cmd.entity(target).insert(player); + } + + playing_anims += 1; + } + + if playing_anims == gltf.animations.len() { + l_done.insert(prefab.path.clone()); + } + } +} + #[cfg(feature = "wasm")] mod wasm { use bevy::{prelude::*, window::PrimaryWindow};