Create a scene from a dynamic scene (#6229)
# Objective - Add a method to create a `Scene` from a `DynamicScene`
This commit is contained in:
parent
88700f3595
commit
b09b2c1056
@ -50,14 +50,15 @@ impl DynamicScene {
|
|||||||
/// Write the dynamic entities and their corresponding components to the given world.
|
/// Write the dynamic entities and their corresponding components to the given world.
|
||||||
///
|
///
|
||||||
/// This method will return a [`SceneSpawnError`] if a type either is not registered
|
/// This method will return a [`SceneSpawnError`] if a type either is not registered
|
||||||
/// or doesn't reflect the [`Component`](bevy_ecs::component::Component) trait.
|
/// in the provided [`AppTypeRegistry`] resource, or doesn't reflect the
|
||||||
pub fn write_to_world(
|
/// [`Component`](bevy_ecs::component::Component) trait.
|
||||||
|
pub fn write_to_world_with(
|
||||||
&self,
|
&self,
|
||||||
world: &mut World,
|
world: &mut World,
|
||||||
entity_map: &mut EntityMap,
|
entity_map: &mut EntityMap,
|
||||||
|
type_registry: &AppTypeRegistry,
|
||||||
) -> Result<(), SceneSpawnError> {
|
) -> Result<(), SceneSpawnError> {
|
||||||
let registry = world.resource::<AppTypeRegistry>().clone();
|
let type_registry = type_registry.read();
|
||||||
let type_registry = registry.read();
|
|
||||||
|
|
||||||
for scene_entity in &self.entities {
|
for scene_entity in &self.entities {
|
||||||
// Fetch the entity with the given entity id from the `entity_map`
|
// Fetch the entity with the given entity id from the `entity_map`
|
||||||
@ -99,6 +100,20 @@ impl DynamicScene {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Write the dynamic entities and their corresponding components to the given world.
|
||||||
|
///
|
||||||
|
/// This method will return a [`SceneSpawnError`] if a type either is not registered
|
||||||
|
/// in the world's [`AppTypeRegistry`] resource, or doesn't reflect the
|
||||||
|
/// [`Component`](bevy_ecs::component::Component) trait.
|
||||||
|
pub fn write_to_world(
|
||||||
|
&self,
|
||||||
|
world: &mut World,
|
||||||
|
entity_map: &mut EntityMap,
|
||||||
|
) -> Result<(), SceneSpawnError> {
|
||||||
|
let registry = world.resource::<AppTypeRegistry>().clone();
|
||||||
|
self.write_to_world_with(world, entity_map, ®istry)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: move to AssetSaver when it is implemented
|
// TODO: move to AssetSaver when it is implemented
|
||||||
/// Serialize this dynamic scene into rust object notation (ron).
|
/// Serialize this dynamic scene into rust object notation (ron).
|
||||||
pub fn serialize_ron(&self, registry: &TypeRegistryArc) -> Result<String, ron::Error> {
|
pub fn serialize_ron(&self, registry: &TypeRegistryArc) -> Result<String, ron::Error> {
|
||||||
|
|||||||
@ -6,7 +6,7 @@ use bevy_ecs::{
|
|||||||
};
|
};
|
||||||
use bevy_reflect::TypeUuid;
|
use bevy_reflect::TypeUuid;
|
||||||
|
|
||||||
use crate::{InstanceInfo, SceneSpawnError};
|
use crate::{DynamicScene, InstanceInfo, SceneSpawnError};
|
||||||
|
|
||||||
/// To spawn a scene, you can use either:
|
/// To spawn a scene, you can use either:
|
||||||
/// * [`SceneSpawner::spawn`](crate::SceneSpawner::spawn)
|
/// * [`SceneSpawner::spawn`](crate::SceneSpawner::spawn)
|
||||||
@ -25,6 +25,18 @@ impl Scene {
|
|||||||
Self { world }
|
Self { world }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new scene from a given dynamic scene.
|
||||||
|
pub fn from_dynamic_scene(
|
||||||
|
dynamic_scene: &DynamicScene,
|
||||||
|
type_registry: &AppTypeRegistry,
|
||||||
|
) -> Result<Scene, SceneSpawnError> {
|
||||||
|
let mut world = World::new();
|
||||||
|
let mut entity_map = EntityMap::default();
|
||||||
|
dynamic_scene.write_to_world_with(&mut world, &mut entity_map, type_registry)?;
|
||||||
|
|
||||||
|
Ok(Self { world })
|
||||||
|
}
|
||||||
|
|
||||||
/// Clone the scene.
|
/// Clone the scene.
|
||||||
///
|
///
|
||||||
/// This method will return a [`SceneSpawnError`] if a type either is not registered in the
|
/// This method will return a [`SceneSpawnError`] if a type either is not registered in the
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user