Wow clearing assets works surprisingly well. Handle<T> components FTW

main
Elijah Voigt 2 years ago
parent 748875f765
commit e6ceffa8d1

@ -8,13 +8,13 @@
// //
// TODO: // TODO:
// * Disable auto start/load // * Disable auto start/load
// * Better handle hide/close monologue
// * (hard) Harden Active Camera // * (hard) Harden Active Camera
// * (medium) Despawn entire scene when GLTF changed? // * (medium) Despawn entire scene when GLTF changed?
// * (medium) Select Font -> "Default Font" Resource // * (medium) Select Font -> "Default Font" Resource
// * (medium) Pre-compute animation target entities // * (medium) Pre-compute animation target entities
// * (medium) Animation buttons only visible when playable // * (medium) Animation buttons only visible when playable
// * (easy) Clear button to wipe spawned scene // * (easy) Clear button to wipe spawned scene
// * Better handle hide/close monologue
// * (brutal) export level // * (brutal) export level
// * (hard) import level // * (hard) import level
// //
@ -119,6 +119,7 @@ fn main() {
directional_light_force_shadows, directional_light_force_shadows,
), ),
) )
.add_systems(Update, (clear_level, clear_assets))
.run(); .run();
} }
@ -197,6 +198,15 @@ fn initialize_ui(mut commands: Commands) {
// HACK: This is super janky but I think we need it like this for UI layout rules // HACK: This is super janky but I think we need it like this for UI layout rules
let mut content_containers: Vec<(String, Entity)> = Vec::new(); let mut content_containers: Vec<(String, Entity)> = Vec::new();
let simple_button = ButtonBundle {
style: Style {
..base_style.clone()
},
background_color: Color::WHITE.into(),
border_color: Color::BLACK.into(),
..default()
};
// Containers with asset content // Containers with asset content
parent parent
.spawn(( .spawn((
@ -274,24 +284,38 @@ fn initialize_ui(mut commands: Commands) {
ui::Select::Single, ui::Select::Single,
)) ))
.with_children(|parent| { .with_children(|parent| {
let b = ButtonBundle { content_containers.iter().enumerate().for_each(
style: Style { |(i, (name, target))| {
..base_style.clone() parent.spawn((
simple_button.clone(),
ui::Title {
text: name.clone(),
..default()
},
ui::Collapse { target: *target },
ui::Sorting(i as u8),
));
}, },
background_color: Color::WHITE.into(), );
border_color: Color::BLACK.into(),
..default() parent.spawn((
}; simple_button.clone(),
content_containers.iter().for_each(|(name, target)| { ClearAssets,
parent.spawn(( ui::Sorting(90),
b.clone(), ui::Title {
ui::Title { text: "Clear Assets".into(),
text: name.clone(), ..default()
..default() },
}, ));
ui::Collapse { target: *target }, parent.spawn((
)); simple_button.clone(),
}); ClearLevel,
ui::Sorting(95),
ui::Title {
text: "Reset Level".into(),
..default()
},
));
}); });
}) })
.id(); .id();
@ -786,6 +810,7 @@ mod animations {
} }
/// When a new scene is loaded, add any newly compatible animations /// When a new scene is loaded, add any newly compatible animations
/// TODO: Add target entity(s) too
pub fn add_animations_ui( pub fn add_animations_ui(
player_spawned: Query<&Name, Added<AnimationPlayer>>, player_spawned: Query<&Name, Added<AnimationPlayer>>,
widget: Query<Entity, With<AnimationWidget>>, widget: Query<Entity, With<AnimationWidget>>,
@ -822,7 +847,6 @@ mod animations {
// When a scene is de-selected, remove any outdated animation options // When a scene is de-selected, remove any outdated animation options
pub fn remove_animations_ui( pub fn remove_animations_ui(
mut removed_players: RemovedComponents<Handle<Scene>>, mut removed_players: RemovedComponents<Handle<Scene>>,
names: Query<&Name>,
current: Query<(Entity, &ui::TargetAsset<AnimationClip>)>, current: Query<(Entity, &ui::TargetAsset<AnimationClip>)>,
clips: Res<Assets<AnimationClip>>, clips: Res<Assets<AnimationClip>>,
targets: Query<(&AnimationPlayer, &Name)>, targets: Query<(&AnimationPlayer, &Name)>,
@ -1274,3 +1298,74 @@ mod lighting {
}) })
} }
} }
use reset::*;
mod reset {
use super::*;
#[derive(Debug, Component)]
pub struct ClearLevel;
pub fn clear_level(
events: Query<Entity, (With<ClearLevel>, Added<ui::Active>)>,
actives: Query<
Entity,
(
With<ui::Active>,
Or<(
With<ui::TargetEntity>,
With<ui::TargetAsset<Gltf>>,
With<ui::TargetAsset<Scene>>,
With<ui::TargetAsset<AnimationClip>>,
With<ui::TargetAsset<AudioSource>>,
With<ui::TargetAsset<Monologue>>,
With<ui::TargetAsset<Font>>,
)>,
),
>,
root: Query<Entity, With<LevelRoot>>,
mut commands: Commands,
) {
events.iter().for_each(|_| {
actives.iter().for_each(|entity| {
commands.entity(entity).remove::<ui::Active>();
});
commands.entity(root.single()).despawn_descendants();
})
}
#[derive(Debug, Component)]
pub struct ClearAssets;
pub fn clear_assets(
events: Query<Entity, (With<ClearAssets>, Added<ui::Active>)>,
asset_holders: Query<
Entity,
Or<(
With<ui::TargetAsset<Gltf>>,
With<Handle<Gltf>>,
With<ui::TargetAsset<Scene>>,
With<Handle<Scene>>,
With<ui::TargetAsset<AnimationClip>>,
With<Handle<AnimationClip>>,
With<ui::TargetAsset<AudioSource>>,
With<Handle<AudioSource>>,
With<ui::TargetAsset<Monologue>>,
With<Handle<Monologue>>,
With<ui::TargetAsset<Font>>,
With<Handle<Font>>,
)>,
>,
mut registry: ResMut<AssetRegistry>,
mut commands: Commands,
) {
events.iter().for_each(|entity| {
asset_holders
.iter()
.for_each(|entity| commands.entity(entity).despawn_recursive());
registry.0.clear();
// TODO: .clear() assets?
commands.entity(entity).remove::<ui::Active>();
})
}
}

Loading…
Cancel
Save