use crate::{DynamicScene, Scene};
use bevy_asset::{AssetEvent, AssetId, Assets, Handle};
use bevy_ecs::{
entity::{Entity, EntityHashMap},
event::{EntityEvent, EventCursor, Events},
hierarchy::ChildOf,
reflect::AppTypeRegistry,
resource::Resource,
world::{Mut, World},
};
use bevy_platform::collections::{HashMap, HashSet};
use bevy_reflect::Reflect;
use bevy_utils::prelude::DebugName;
use thiserror::Error;
use uuid::Uuid;
use crate::{DynamicSceneRoot, SceneRoot};
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
change_detection::ResMut,
prelude::{Changed, Component, Without},
system::{Commands, Query},
};
#[derive(Clone, Copy, Debug, Eq, PartialEq, EntityEvent, Reflect)]
#[reflect(Debug, PartialEq, Clone)]
pub struct SceneInstanceReady {
pub instance_id: InstanceId,
}
#[derive(Debug)]
struct InstanceInfo {
entity_map: EntityHashMap<Entity>,
parent: Option<Entity>,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Reflect)]
#[reflect(Debug, PartialEq, Hash, Clone)]
pub struct InstanceId(Uuid);
impl InstanceId {
fn new() -> Self {
InstanceId(Uuid::new_v4())
}
}
#[derive(Default, Resource)]
pub struct SceneSpawner {
pub(crate) spawned_scenes: HashMap<AssetId<Scene>, HashSet<InstanceId>>,
pub(crate) spawned_dynamic_scenes: HashMap<AssetId<DynamicScene>, HashSet<InstanceId>>,
spawned_instances: HashMap<InstanceId, InstanceInfo>,
scene_asset_event_reader: EventCursor<AssetEvent<Scene>>,
debounced_scene_asset_events: HashMap<AssetId<Scene>, u32>,
dynamic_scene_asset_event_reader: EventCursor<AssetEvent<DynamicScene>>,
debounced_dynamic_scene_asset_events: HashMap<AssetId<DynamicScene>, u32>,
scenes_to_spawn: Vec<(Handle<Scene>, InstanceId, Option<Entity>)>,
dynamic_scenes_to_spawn: Vec<(Handle<DynamicScene>, InstanceId, Option<Entity>)>,
scenes_to_despawn: Vec<AssetId<Scene>>,
dynamic_scenes_to_despawn: Vec<AssetId<DynamicScene>>,
instances_to_despawn: Vec<InstanceId>,
instances_ready: Vec<(InstanceId, Option<Entity>)>,
}
#[derive(Error, Debug)]
pub enum SceneSpawnError {
#[error("scene contains the unregistered component `{type_path}`. consider adding `#[reflect(Component)]` to your type")]
UnregisteredComponent {
type_path: String,
},
#[error("scene contains the unregistered resource `{type_path}`. consider adding `#[reflect(Resource)]` to your type")]
UnregisteredResource {
type_path: String,
},
#[error(
"scene contains the unregistered type `{std_type_name}`. \
consider reflecting it with `#[derive(Reflect)]` \
and registering the type using `app.register_type::<T>()`"
)]
UnregisteredType {
std_type_name: DebugName,
},
#[error(
"scene contains the reflected type `{type_path}` but it was not found in the type registry. \
consider registering the type using `app.register_type::<T>()``"
)]
UnregisteredButReflectedType {
type_path: String,
},
#[error("scene contains dynamic type `{type_path}` without a represented type. consider changing this using `set_represented_type`.")]
NoRepresentedType {
type_path: String,
},
#[error("scene does not exist")]
NonExistentScene {
id: AssetId<DynamicScene>,
},
#[error("scene does not exist")]
NonExistentRealScene {
id: AssetId<Scene>,
},
}
impl SceneSpawner {
pub fn spawn_dynamic(&mut self, id: impl Into<Handle<DynamicScene>>) -> InstanceId {
let instance_id = InstanceId::new();
self.dynamic_scenes_to_spawn
.push((id.into(), instance_id, None));
instance_id
}
pub fn spawn_dynamic_as_child(
&mut self,
id: impl Into<Handle<DynamicScene>>,
parent: Entity,
) -> InstanceId {
let instance_id = InstanceId::new();
self.dynamic_scenes_to_spawn
.push((id.into(), instance_id, Some(parent)));
instance_id
}
pub fn spawn(&mut self, id: impl Into<Handle<Scene>>) -> InstanceId {
let instance_id = InstanceId::new();
self.scenes_to_spawn.push((id.into(), instance_id, None));
instance_id
}
pub fn spawn_as_child(&mut self, id: impl Into<Handle<Scene>>, parent: Entity) -> InstanceId {
let instance_id = InstanceId::new();
self.scenes_to_spawn
.push((id.into(), instance_id, Some(parent)));
instance_id
}
pub fn despawn(&mut self, id: impl Into<AssetId<Scene>>) {
self.scenes_to_despawn.push(id.into());
}
pub fn despawn_dynamic(&mut self, id: impl Into<AssetId<DynamicScene>>) {
self.dynamic_scenes_to_despawn.push(id.into());
}
pub fn despawn_instance(&mut self, instance_id: InstanceId) {
self.instances_to_despawn.push(instance_id);
}
pub fn unregister_instance(&mut self, instance_id: InstanceId) {
self.spawned_instances.remove(&instance_id);
}
pub fn despawn_sync(
&mut self,
world: &mut World,
id: impl Into<AssetId<Scene>>,
) -> Result<(), SceneSpawnError> {
if let Some(instance_ids) = self.spawned_scenes.remove(&id.into()) {
for instance_id in instance_ids {
self.despawn_instance_sync(world, &instance_id);
}
}
Ok(())
}
pub fn despawn_dynamic_sync(
&mut self,
world: &mut World,
id: impl Into<AssetId<DynamicScene>>,
) -> Result<(), SceneSpawnError> {
if let Some(instance_ids) = self.spawned_dynamic_scenes.remove(&id.into()) {
for instance_id in instance_ids {
self.despawn_instance_sync(world, &instance_id);
}
}
Ok(())
}
pub fn despawn_instance_sync(&mut self, world: &mut World, instance_id: &InstanceId) {
if let Some(mut instance) = self.spawned_instances.remove(instance_id) {
Self::despawn_instance_internal(world, &mut instance);
}
}
fn despawn_instance_internal(world: &mut World, instance: &mut InstanceInfo) {
for &entity in instance.entity_map.values() {
if let Ok(entity_mut) = world.get_entity_mut(entity) {
entity_mut.despawn();
};
}
instance.entity_map.clear();
}
pub fn spawn_dynamic_sync(
&mut self,
world: &mut World,
id: impl Into<AssetId<DynamicScene>>,
) -> Result<InstanceId, SceneSpawnError> {
let mut entity_map = EntityHashMap::default();
let id = id.into();
Self::spawn_dynamic_internal(world, id, &mut entity_map)?;
let instance_id = InstanceId::new();
self.spawned_instances.insert(
instance_id,
InstanceInfo {
entity_map,
parent: None,
},
);
let spawned = self.spawned_dynamic_scenes.entry(id).or_default();
spawned.insert(instance_id);
self.instances_ready.push((instance_id, None));
Ok(instance_id)
}
fn spawn_dynamic_internal(
world: &mut World,
id: AssetId<DynamicScene>,
entity_map: &mut EntityHashMap<Entity>,
) -> Result<(), SceneSpawnError> {
world.resource_scope(|world, scenes: Mut<Assets<DynamicScene>>| {
let scene = scenes
.get(id)
.ok_or(SceneSpawnError::NonExistentScene { id })?;
scene.write_to_world(world, entity_map)
})
}
pub fn spawn_sync(
&mut self,
world: &mut World,
id: impl Into<AssetId<Scene>>,
) -> Result<InstanceId, SceneSpawnError> {
let mut entity_map = EntityHashMap::default();
let id = id.into();
Self::spawn_sync_internal(world, id, &mut entity_map)?;
let instance_id = InstanceId::new();
self.spawned_instances.insert(
instance_id,
InstanceInfo {
entity_map,
parent: None,
},
);
let spawned = self.spawned_scenes.entry(id).or_default();
spawned.insert(instance_id);
self.instances_ready.push((instance_id, None));
Ok(instance_id)
}
fn spawn_sync_internal(
world: &mut World,
id: AssetId<Scene>,
entity_map: &mut EntityHashMap<Entity>,
) -> Result<(), SceneSpawnError> {
world.resource_scope(|world, scenes: Mut<Assets<Scene>>| {
let scene = scenes
.get(id)
.ok_or(SceneSpawnError::NonExistentRealScene { id })?;
scene.write_to_world_with(
world,
entity_map,
&world.resource::<AppTypeRegistry>().clone(),
)
})
}
pub fn update_spawned_scenes(
&mut self,
world: &mut World,
scene_ids: &[AssetId<Scene>],
) -> Result<(), SceneSpawnError> {
for id in scene_ids {
if let Some(spawned_instances) = self.spawned_scenes.get(id) {
for instance_id in spawned_instances {
if let Some(instance_info) = self.spawned_instances.get_mut(instance_id) {
Self::despawn_instance_internal(world, instance_info);
Self::spawn_sync_internal(world, *id, &mut instance_info.entity_map)?;
Self::set_scene_instance_parent_sync(world, instance_info);
self.instances_ready
.push((*instance_id, instance_info.parent));
}
}
}
}
Ok(())
}
pub fn update_spawned_dynamic_scenes(
&mut self,
world: &mut World,
scene_ids: &[AssetId<DynamicScene>],
) -> Result<(), SceneSpawnError> {
for id in scene_ids {
if let Some(spawned_instances) = self.spawned_dynamic_scenes.get(id) {
for instance_id in spawned_instances {
if let Some(instance_info) = self.spawned_instances.get_mut(instance_id) {
Self::despawn_instance_internal(world, instance_info);
Self::spawn_dynamic_internal(world, *id, &mut instance_info.entity_map)?;
Self::set_scene_instance_parent_sync(world, instance_info);
self.instances_ready
.push((*instance_id, instance_info.parent));
}
}
}
}
Ok(())
}
pub fn despawn_queued_scenes(&mut self, world: &mut World) -> Result<(), SceneSpawnError> {
let scenes_to_despawn = core::mem::take(&mut self.scenes_to_despawn);
for scene_handle in scenes_to_despawn {
self.despawn_sync(world, scene_handle)?;
}
let scenes_to_despawn = core::mem::take(&mut self.dynamic_scenes_to_despawn);
for scene_handle in scenes_to_despawn {
self.despawn_dynamic_sync(world, scene_handle)?;
}
Ok(())
}
pub fn despawn_queued_instances(&mut self, world: &mut World) {
let instances_to_despawn = core::mem::take(&mut self.instances_to_despawn);
for instance_id in instances_to_despawn {
self.despawn_instance_sync(world, &instance_id);
}
}
pub fn spawn_queued_scenes(&mut self, world: &mut World) -> Result<(), SceneSpawnError> {
let scenes_to_spawn = core::mem::take(&mut self.dynamic_scenes_to_spawn);
for (handle, instance_id, parent) in scenes_to_spawn {
let mut entity_map = EntityHashMap::default();
match Self::spawn_dynamic_internal(world, handle.id(), &mut entity_map) {
Ok(_) => {
let instance_info = InstanceInfo { entity_map, parent };
Self::set_scene_instance_parent_sync(world, &instance_info);
self.spawned_instances.insert(instance_id, instance_info);
let spawned = self.spawned_dynamic_scenes.entry(handle.id()).or_default();
spawned.insert(instance_id);
self.instances_ready.push((instance_id, parent));
}
Err(SceneSpawnError::NonExistentScene { .. }) => {
self.dynamic_scenes_to_spawn
.push((handle, instance_id, parent));
}
Err(err) => return Err(err),
}
}
let scenes_to_spawn = core::mem::take(&mut self.scenes_to_spawn);
for (scene_handle, instance_id, parent) in scenes_to_spawn {
let mut entity_map = EntityHashMap::default();
match Self::spawn_sync_internal(world, scene_handle.id(), &mut entity_map) {
Ok(_) => {
let instance_info = InstanceInfo { entity_map, parent };
Self::set_scene_instance_parent_sync(world, &instance_info);
self.spawned_instances.insert(instance_id, instance_info);
let spawned = self.spawned_scenes.entry(scene_handle.id()).or_default();
spawned.insert(instance_id);
self.instances_ready.push((instance_id, parent));
}
Err(SceneSpawnError::NonExistentRealScene { .. }) => {
self.scenes_to_spawn
.push((scene_handle, instance_id, parent));
}
Err(err) => return Err(err),
}
}
Ok(())
}
fn set_scene_instance_parent_sync(world: &mut World, instance: &InstanceInfo) {
let Some(parent) = instance.parent else {
return;
};
for &entity in instance.entity_map.values() {
if !world
.get_entity(entity)
.ok()
.is_none_or(|entity| entity.contains::<ChildOf>())
{
world.entity_mut(parent).add_child(entity);
}
}
}
fn trigger_scene_ready_events(&mut self, world: &mut World) {
for (instance_id, parent) in self.instances_ready.drain(..) {
if let Some(parent) = parent {
world
.commands()
.trigger_targets(SceneInstanceReady { instance_id }, parent);
} else {
world.commands().trigger(SceneInstanceReady { instance_id });
}
}
}
pub fn instance_is_ready(&self, instance_id: InstanceId) -> bool {
self.spawned_instances.contains_key(&instance_id)
}
pub fn iter_instance_entities(
&'_ self,
instance_id: InstanceId,
) -> impl Iterator<Item = Entity> + '_ {
self.spawned_instances
.get(&instance_id)
.map(|instance| instance.entity_map.values())
.into_iter()
.flatten()
.copied()
}
}
pub fn scene_spawner_system(world: &mut World) {
world.resource_scope(|world, mut scene_spawner: Mut<SceneSpawner>| {
let is_parent_alive = |parent: &Option<Entity>| {
parent
.map(|parent| world.get_entity(parent).is_ok())
.unwrap_or(true)
};
scene_spawner
.dynamic_scenes_to_spawn
.retain(|(_, _, parent)| is_parent_alive(parent));
scene_spawner
.scenes_to_spawn
.retain(|(_, _, parent)| is_parent_alive(parent));
let scene_asset_events = world.resource::<Events<AssetEvent<Scene>>>();
let dynamic_scene_asset_events = world.resource::<Events<AssetEvent<DynamicScene>>>();
let scene_spawner = &mut *scene_spawner;
let mut updated_spawned_scenes = Vec::new();
for event in scene_spawner
.scene_asset_event_reader
.read(scene_asset_events)
{
match event {
AssetEvent::Added { id } => {
scene_spawner.debounced_scene_asset_events.insert(*id, 0);
}
AssetEvent::Modified { id } => {
if scene_spawner
.debounced_scene_asset_events
.insert(*id, 0)
.is_none()
&& scene_spawner.spawned_scenes.contains_key(id)
{
updated_spawned_scenes.push(*id);
}
}
_ => {}
}
}
let mut updated_spawned_dynamic_scenes = Vec::new();
for event in scene_spawner
.dynamic_scene_asset_event_reader
.read(dynamic_scene_asset_events)
{
match event {
AssetEvent::Added { id } => {
scene_spawner
.debounced_dynamic_scene_asset_events
.insert(*id, 0);
}
AssetEvent::Modified { id } => {
if scene_spawner
.debounced_dynamic_scene_asset_events
.insert(*id, 0)
.is_none()
&& scene_spawner.spawned_dynamic_scenes.contains_key(id)
{
updated_spawned_dynamic_scenes.push(*id);
}
}
_ => {}
}
}
scene_spawner.despawn_queued_scenes(world).unwrap();
scene_spawner.despawn_queued_instances(world);
scene_spawner
.spawn_queued_scenes(world)
.unwrap_or_else(|err| panic!("{}", err));
scene_spawner
.update_spawned_scenes(world, &updated_spawned_scenes)
.unwrap();
scene_spawner
.update_spawned_dynamic_scenes(world, &updated_spawned_dynamic_scenes)
.unwrap();
scene_spawner.trigger_scene_ready_events(world);
const SCENE_ASSET_AGE_THRESHOLD: u32 = 2;
for asset_id in scene_spawner.debounced_scene_asset_events.clone().keys() {
let age = scene_spawner
.debounced_scene_asset_events
.get(asset_id)
.unwrap();
if *age > SCENE_ASSET_AGE_THRESHOLD {
scene_spawner.debounced_scene_asset_events.remove(asset_id);
} else {
scene_spawner
.debounced_scene_asset_events
.insert(*asset_id, *age + 1);
}
}
for asset_id in scene_spawner
.debounced_dynamic_scene_asset_events
.clone()
.keys()
{
let age = scene_spawner
.debounced_dynamic_scene_asset_events
.get(asset_id)
.unwrap();
if *age > SCENE_ASSET_AGE_THRESHOLD {
scene_spawner
.debounced_dynamic_scene_asset_events
.remove(asset_id);
} else {
scene_spawner
.debounced_dynamic_scene_asset_events
.insert(*asset_id, *age + 1);
}
}
});
}
#[derive(Component, Deref, DerefMut)]
pub struct SceneInstance(pub(crate) InstanceId);
pub fn scene_spawner(
mut commands: Commands,
mut scene_to_spawn: Query<
(Entity, &SceneRoot, Option<&mut SceneInstance>),
(Changed<SceneRoot>, Without<DynamicSceneRoot>),
>,
mut dynamic_scene_to_spawn: Query<
(Entity, &DynamicSceneRoot, Option<&mut SceneInstance>),
(Changed<DynamicSceneRoot>, Without<SceneRoot>),
>,
mut scene_spawner: ResMut<SceneSpawner>,
) {
for (entity, scene, instance) in &mut scene_to_spawn {
let new_instance = scene_spawner.spawn_as_child(scene.0.clone(), entity);
if let Some(mut old_instance) = instance {
scene_spawner.despawn_instance(**old_instance);
*old_instance = SceneInstance(new_instance);
} else {
commands.entity(entity).insert(SceneInstance(new_instance));
}
}
for (entity, dynamic_scene, instance) in &mut dynamic_scene_to_spawn {
let new_instance = scene_spawner.spawn_dynamic_as_child(dynamic_scene.0.clone(), entity);
if let Some(mut old_instance) = instance {
scene_spawner.despawn_instance(**old_instance);
*old_instance = SceneInstance(new_instance);
} else {
commands.entity(entity).insert(SceneInstance(new_instance));
}
}
}
#[cfg(test)]
mod tests {
use bevy_app::App;
use bevy_asset::{AssetPlugin, AssetServer, Handle};
use bevy_ecs::{
component::Component,
hierarchy::Children,
observer::On,
prelude::ReflectComponent,
query::With,
system::{Commands, Query, Res, ResMut, RunSystemOnce},
};
use bevy_reflect::Reflect;
use crate::{DynamicSceneBuilder, DynamicSceneRoot, ScenePlugin};
use super::*;
use crate::{DynamicScene, SceneSpawner};
use bevy_app::ScheduleRunnerPlugin;
use bevy_asset::Assets;
use bevy_ecs::{
entity::Entity,
prelude::{AppTypeRegistry, World},
};
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct ComponentA {
pub x: f32,
pub y: f32,
}
#[test]
fn spawn_and_delete() {
let mut app = App::new();
app.add_plugins(ScheduleRunnerPlugin::default())
.add_plugins(AssetPlugin::default())
.add_plugins(ScenePlugin);
app.update();
let mut scene_world = World::new();
let type_registry = app.world().resource::<AppTypeRegistry>().clone();
scene_world.insert_resource(type_registry);
scene_world.spawn(ComponentA { x: 3.0, y: 4.0 });
let scene = DynamicScene::from_world(&scene_world);
let scene_handle = app
.world_mut()
.resource_mut::<Assets<DynamicScene>>()
.add(scene);
let entity = app
.world_mut()
.spawn(DynamicSceneRoot(scene_handle.clone()))
.id();
app.update();
let (scene_entity, scene_component_a) = app
.world_mut()
.query::<(Entity, &ComponentA)>()
.single(app.world())
.unwrap();
assert_eq!(scene_component_a.x, 3.0);
assert_eq!(scene_component_a.y, 4.0);
assert_eq!(
app.world().entity(entity).get::<Children>().unwrap().len(),
1
);
let mut scene_spawner = app.world_mut().resource_mut::<SceneSpawner>();
scene_spawner.despawn_dynamic(&scene_handle);
app.update();
assert!(app.world().get_entity(scene_entity).is_err());
assert!(app.world().entity(entity).get::<Children>().is_none());
}
#[derive(Reflect, Component, Debug, PartialEq, Eq, Clone, Copy, Default)]
#[reflect(Component)]
struct A(usize);
#[test]
fn clone_dynamic_entities() {
let mut world = World::default();
let atr = AppTypeRegistry::default();
atr.write().register::<A>();
world.insert_resource(atr);
world.insert_resource(Assets::<DynamicScene>::default());
world.spawn(A(42));
assert_eq!(world.query::<&A>().iter(&world).len(), 1);
let mut scene_spawner = SceneSpawner::default();
let entity = world
.query_filtered::<Entity, With<A>>()
.single(&world)
.unwrap();
let scene = DynamicSceneBuilder::from_world(&world)
.extract_entity(entity)
.build();
let scene_id = world.resource_mut::<Assets<DynamicScene>>().add(scene);
let instance_id = scene_spawner
.spawn_dynamic_sync(&mut world, &scene_id)
.unwrap();
assert_eq!(world.query::<&A>().iter(&world).len(), 2);
let new_entity = scene_spawner
.iter_instance_entities(instance_id)
.next()
.unwrap();
assert_ne!(entity, new_entity);
let [old_a, new_a] = world
.query::<&A>()
.get_many(&world, [entity, new_entity])
.unwrap();
assert_eq!(old_a, new_a);
}
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct ComponentF;
#[derive(Resource, Default)]
struct TriggerCount(u32);
fn setup() -> App {
let mut app = App::new();
app.add_plugins((AssetPlugin::default(), ScenePlugin));
app.init_resource::<TriggerCount>();
app.register_type::<ComponentF>();
app.world_mut().spawn(ComponentF);
app.world_mut().spawn(ComponentF);
app
}
fn build_scene(app: &mut App) -> Handle<Scene> {
app.world_mut()
.run_system_once(
|world: &World,
type_registry: Res<'_, AppTypeRegistry>,
asset_server: Res<'_, AssetServer>| {
asset_server.add(
Scene::from_dynamic_scene(&DynamicScene::from_world(world), &type_registry)
.unwrap(),
)
},
)
.expect("Failed to run scene builder system.")
}
fn build_dynamic_scene(app: &mut App) -> Handle<DynamicScene> {
app.world_mut()
.run_system_once(|world: &World, asset_server: Res<'_, AssetServer>| {
asset_server.add(DynamicScene::from_world(world))
})
.expect("Failed to run dynamic scene builder system.")
}
fn observe_trigger(app: &mut App, scene_id: InstanceId, scene_entity: Option<Entity>) {
app.world_mut().add_observer(
move |event: On<SceneInstanceReady>,
scene_spawner: Res<SceneSpawner>,
mut trigger_count: ResMut<TriggerCount>| {
assert_eq!(
event.event().instance_id,
scene_id,
"`SceneInstanceReady` contains the wrong `InstanceId`"
);
assert_eq!(
event.entity(),
scene_entity.unwrap_or(Entity::PLACEHOLDER),
"`SceneInstanceReady` triggered on the wrong parent entity"
);
assert!(
scene_spawner.instance_is_ready(event.event().instance_id),
"`InstanceId` is not ready"
);
trigger_count.0 += 1;
},
);
app.update();
app.world_mut()
.run_system_once(|trigger_count: Res<TriggerCount>| {
assert_eq!(
trigger_count.0, 1,
"wrong number of `SceneInstanceReady` triggers"
);
})
.unwrap();
}
#[test]
fn observe_scene() {
let mut app = setup();
let scene = build_scene(&mut app);
let scene_id = app
.world_mut()
.run_system_once(move |mut scene_spawner: ResMut<'_, SceneSpawner>| {
scene_spawner.spawn(scene.clone())
})
.unwrap();
observe_trigger(&mut app, scene_id, None);
}
#[test]
fn observe_dynamic_scene() {
let mut app = setup();
let scene = build_dynamic_scene(&mut app);
let scene_id = app
.world_mut()
.run_system_once(move |mut scene_spawner: ResMut<'_, SceneSpawner>| {
scene_spawner.spawn_dynamic(scene.clone())
})
.unwrap();
observe_trigger(&mut app, scene_id, None);
}
#[test]
fn observe_scene_as_child() {
let mut app = setup();
let scene = build_scene(&mut app);
let (scene_id, scene_entity) = app
.world_mut()
.run_system_once(
move |mut commands: Commands<'_, '_>,
mut scene_spawner: ResMut<'_, SceneSpawner>| {
let entity = commands.spawn_empty().id();
let id = scene_spawner.spawn_as_child(scene.clone(), entity);
(id, entity)
},
)
.unwrap();
observe_trigger(&mut app, scene_id, Some(scene_entity));
}
#[test]
fn observe_dynamic_scene_as_child() {
let mut app = setup();
let scene = build_dynamic_scene(&mut app);
let (scene_id, scene_entity) = app
.world_mut()
.run_system_once(
move |mut commands: Commands<'_, '_>,
mut scene_spawner: ResMut<'_, SceneSpawner>| {
let entity = commands.spawn_empty().id();
let id = scene_spawner.spawn_dynamic_as_child(scene.clone(), entity);
(id, entity)
},
)
.unwrap();
observe_trigger(&mut app, scene_id, Some(scene_entity));
}
#[test]
fn despawn_scene() {
let mut app = App::new();
app.add_plugins((AssetPlugin::default(), ScenePlugin));
app.register_type::<ComponentF>();
let asset_server = app.world().resource::<AssetServer>();
let scene = asset_server.add(DynamicScene::default());
let count = 10;
let check = |world: &mut World, expected_count: usize| {
let scene_spawner = world.resource::<SceneSpawner>();
assert_eq!(
scene_spawner.spawned_dynamic_scenes[&scene.id()].len(),
expected_count
);
assert_eq!(scene_spawner.spawned_instances.len(), expected_count);
};
for _ in 0..count {
app.world_mut()
.spawn((ComponentF, DynamicSceneRoot(scene.clone())));
}
app.update();
check(app.world_mut(), count);
app.world_mut()
.run_system_once(
|mut commands: Commands, query: Query<Entity, With<ComponentF>>| {
for entity in query.iter() {
commands.entity(entity).despawn();
}
},
)
.unwrap();
app.update();
check(app.world_mut(), 0);
}
#[test]
fn scene_child_order_preserved_when_archetype_order_mismatched() {
let mut app = App::new();
app.add_plugins(ScheduleRunnerPlugin::default())
.add_plugins(AssetPlugin::default())
.add_plugins(ScenePlugin)
.register_type::<ComponentA>()
.register_type::<ComponentF>();
app.update();
let mut scene_world = World::new();
let root = scene_world.spawn_empty().id();
let temporary_root = scene_world.spawn_empty().id();
let child1 = scene_world
.spawn((ChildOf(temporary_root), ComponentA { x: 1.0, y: 1.0 }))
.id();
let child2 = scene_world
.spawn((ChildOf(temporary_root), ComponentA { x: 2.0, y: 2.0 }))
.id();
let child0 = scene_world
.spawn((ChildOf(temporary_root), ComponentF))
.id();
scene_world
.entity_mut(root)
.add_children(&[child0, child1, child2]);
let scene = Scene::new(scene_world);
let scene_handle = app.world_mut().resource_mut::<Assets<Scene>>().add(scene);
let spawned = app.world_mut().spawn(SceneRoot(scene_handle.clone())).id();
app.update();
let world = app.world_mut();
let spawned_root = world.entity(spawned).get::<Children>().unwrap()[1];
let children = world.entity(spawned_root).get::<Children>().unwrap();
assert_eq!(children.len(), 3);
assert!(world.entity(children[0]).get::<ComponentF>().is_some());
assert_eq!(
world.entity(children[1]).get::<ComponentA>().unwrap().x,
1.0
);
assert_eq!(
world.entity(children[2]).get::<ComponentA>().unwrap().x,
2.0
);
}
}