1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use bevy::{prelude::*, scene::SceneInstance};

use crate::InheritOutlineBundle;

/// A component for triggering the `AsyncSceneInheritOutlinePlugin`.
#[derive(Component)]
pub struct AsyncSceneInheritOutline;

fn process_async_scene_outline(
    mut commands: Commands,
    scene_spawner: Res<SceneSpawner>,
    async_query: Query<(Entity, &SceneInstance), With<AsyncSceneInheritOutline>>,
) {
    for (entity, instance) in async_query.iter() {
        if scene_spawner.instance_is_ready(**instance) {
            for child in scene_spawner.iter_instance_entities(**instance) {
                commands
                    .entity(child)
                    .insert(InheritOutlineBundle::default());
            }
            commands.entity(entity).remove::<AsyncSceneInheritOutline>();
        }
    }
}

/// Automatically inherits outlines for the entities in a scene.
///
/// Once a `SceneInstance` marked with `AsyncSceneInheritOutline` is ready, this plugin will add
/// `InheritOutlineBundle` to all of its entities and then remove the marker component.
pub struct AsyncSceneInheritOutlinePlugin;

impl Plugin for AsyncSceneInheritOutlinePlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(
            Update,
            process_async_scene_outline.run_if(any_with_component::<AsyncSceneInheritOutline>),
        );
    }
}