Use handles for queued scenes in SceneSpawner (#10619)

Fixes #10482

Store Handles instead of AssetIds for queued Scenes and DynamicScenes in
SceneSpawner.
This commit is contained in:
Carter Anderson 2023-11-17 17:27:05 -08:00
parent c458093cc3
commit 24b4c517d0

View file

@ -1,5 +1,5 @@
use crate::{DynamicScene, Scene};
use bevy_asset::{AssetEvent, AssetId, Assets};
use bevy_asset::{AssetEvent, AssetId, Assets, Handle};
use bevy_ecs::{
entity::Entity,
event::{Event, Events, ManualEventReader},
@ -63,8 +63,8 @@ pub struct SceneSpawner {
spawned_dynamic_scenes: HashMap<AssetId<DynamicScene>, Vec<InstanceId>>,
spawned_instances: HashMap<InstanceId, InstanceInfo>,
scene_asset_event_reader: ManualEventReader<AssetEvent<DynamicScene>>,
dynamic_scenes_to_spawn: Vec<(AssetId<DynamicScene>, InstanceId)>,
scenes_to_spawn: Vec<(AssetId<Scene>, InstanceId)>,
dynamic_scenes_to_spawn: Vec<(Handle<DynamicScene>, InstanceId)>,
scenes_to_spawn: Vec<(Handle<Scene>, InstanceId)>,
scenes_to_despawn: Vec<AssetId<DynamicScene>>,
instances_to_despawn: Vec<InstanceId>,
scenes_with_parent: Vec<(InstanceId, Entity)>,
@ -127,7 +127,7 @@ pub enum SceneSpawnError {
impl SceneSpawner {
/// Schedule the spawn of a new instance of the provided dynamic scene.
pub fn spawn_dynamic(&mut self, id: impl Into<AssetId<DynamicScene>>) -> InstanceId {
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));
instance_id
@ -136,7 +136,7 @@ impl SceneSpawner {
/// Schedule the spawn of a new instance of the provided dynamic scene as a child of `parent`.
pub fn spawn_dynamic_as_child(
&mut self,
id: impl Into<AssetId<DynamicScene>>,
id: impl Into<Handle<DynamicScene>>,
parent: Entity,
) -> InstanceId {
let instance_id = InstanceId::new();
@ -146,14 +146,14 @@ impl SceneSpawner {
}
/// Schedule the spawn of a new instance of the provided scene.
pub fn spawn(&mut self, id: impl Into<AssetId<Scene>>) -> InstanceId {
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));
instance_id
}
/// Schedule the spawn of a new instance of the provided scene as a child of `parent`.
pub fn spawn_as_child(&mut self, id: impl Into<AssetId<Scene>>, parent: Entity) -> InstanceId {
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));
self.scenes_with_parent.push((instance_id, parent));
@ -296,21 +296,21 @@ impl SceneSpawner {
pub fn spawn_queued_scenes(&mut self, world: &mut World) -> Result<(), SceneSpawnError> {
let scenes_to_spawn = std::mem::take(&mut self.dynamic_scenes_to_spawn);
for (id, instance_id) in scenes_to_spawn {
for (handle, instance_id) in scenes_to_spawn {
let mut entity_map = HashMap::default();
match Self::spawn_dynamic_internal(world, id, &mut entity_map) {
match Self::spawn_dynamic_internal(world, handle.id(), &mut entity_map) {
Ok(_) => {
self.spawned_instances
.insert(instance_id, InstanceInfo { entity_map });
let spawned = self
.spawned_dynamic_scenes
.entry(id)
.entry(handle.id())
.or_insert_with(Vec::new);
spawned.push(instance_id);
}
Err(SceneSpawnError::NonExistentScene { .. }) => {
self.dynamic_scenes_to_spawn.push((id, instance_id));
self.dynamic_scenes_to_spawn.push((handle, instance_id));
}
Err(err) => return Err(err),
}
@ -319,10 +319,10 @@ impl SceneSpawner {
let scenes_to_spawn = std::mem::take(&mut self.scenes_to_spawn);
for (scene_handle, instance_id) in scenes_to_spawn {
match self.spawn_sync_internal(world, scene_handle, instance_id) {
match self.spawn_sync_internal(world, scene_handle.id(), instance_id) {
Ok(_) => {}
Err(SceneSpawnError::NonExistentRealScene { id: handle }) => {
self.scenes_to_spawn.push((handle, instance_id));
Err(SceneSpawnError::NonExistentRealScene { .. }) => {
self.scenes_to_spawn.push((scene_handle, instance_id));
}
Err(err) => return Err(err),
}