Skip to content

Commit

Permalink
SceneSpawner: add unload() and unload_sync()
Browse files Browse the repository at this point in the history
This allows you to unload scenes.

Refs: #255
  • Loading branch information
Fishrock123 committed Aug 25, 2020
1 parent 0ae74a4 commit 3e37378
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct SceneSpawner {
scene_asset_event_reader: EventReader<AssetEvent<Scene>>,
scenes_to_spawn: Vec<Handle<Scene>>,
scenes_to_load: Vec<Handle<Scene>>,
scenes_to_unload: Vec<Handle<Scene>>,
}

#[derive(Error, Debug)]
Expand All @@ -47,6 +48,10 @@ impl SceneSpawner {
self.scenes_to_load.push(scene_handle);
}

pub fn unload(&mut self, scene_handle: Handle<Scene>) {
self.scenes_to_unload.push(scene_handle);
}

pub fn load_sync(
&mut self,
world: &mut World,
Expand All @@ -58,6 +63,28 @@ impl SceneSpawner {
Ok(())
}

pub fn unload_sync(
&mut self,
world: &mut World,
scene_handle: Handle<Scene>,
) -> Result<(), SceneSpawnError> {
if let Some(instance_ids) = self.spawned_scenes.get(&scene_handle) {
for instance_id in instance_ids {
if let Some(instance) = self.spawned_instances.get(&instance_id) {
for entity in instance.entity_map.values() {
let _ = world.despawn(*entity); // Ignore the result, unload only cares if it exists.
}
}
}

self.loaded_scenes
.retain(|handle| handle.id != scene_handle.id);
self.spawned_scenes
.retain(|handle, _| handle.id != scene_handle.id);
}
Ok(())
}

pub fn spawn_sync(
&mut self,
world: &mut World,
Expand Down Expand Up @@ -152,19 +179,32 @@ impl SceneSpawner {
world: &mut World,
resources: &Resources,
) -> Result<(), SceneSpawnError> {
let scenes_to_load = self.scenes_to_load.drain(..).collect::<Vec<_>>();
let mut non_existent_scenes = Vec::new();
let mut scenes_to_load = Vec::new();
std::mem::swap(&mut self.scenes_to_load, &mut scenes_to_load);

for scene_handle in scenes_to_load {
match self.load_sync(world, resources, scene_handle) {
Ok(_) => {}
Err(SceneSpawnError::NonExistentScene { .. }) => {
non_existent_scenes.push(scene_handle)
self.scenes_to_spawn.push(scene_handle)
}
Err(err) => return Err(err),
}
}

self.scenes_to_load = non_existent_scenes;
Ok(())
}

pub fn unload_queued_scenes(&mut self, world: &mut World) -> Result<(), SceneSpawnError> {
let mut scenes_to_unload = Vec::new();
std::mem::swap(&mut self.scenes_to_unload, &mut scenes_to_unload);

for scene_handle in scenes_to_unload {
match self.unload_sync(world, scene_handle) {
Ok(_) => {}
Err(err) => return Err(err),
}
}
Ok(())
}

Expand All @@ -173,19 +213,19 @@ impl SceneSpawner {
world: &mut World,
resources: &Resources,
) -> Result<(), SceneSpawnError> {
let scenes_to_spawn = self.scenes_to_spawn.drain(..).collect::<Vec<_>>();
let mut non_existent_scenes = Vec::new();
let mut scenes_to_spawn = Vec::new();
std::mem::swap(&mut self.scenes_to_spawn, &mut scenes_to_spawn);

for scene_handle in scenes_to_spawn {
match self.spawn_sync(world, resources, scene_handle) {
Ok(_) => {}
Err(SceneSpawnError::NonExistentScene { .. }) => {
non_existent_scenes.push(scene_handle)
self.scenes_to_spawn.push(scene_handle)
}
Err(err) => return Err(err),
}
}

self.scenes_to_spawn = non_existent_scenes;
Ok(())
}
}
Expand All @@ -209,6 +249,7 @@ pub fn scene_spawner_system(world: &mut World, resources: &mut Resources) {
}
}

scene_spawner.unload_queued_scenes(world).unwrap();
scene_spawner.load_queued_scenes(world, resources).unwrap();
scene_spawner.spawn_queued_scenes(world, resources).unwrap();
scene_spawner
Expand Down

0 comments on commit 3e37378

Please sign in to comment.