use nightshade::ecs::scene::commands::{save_scene_binary_to_bytes, spawn_scene, subtree_to_scene};
use nightshade::ecs::scene::{
Scene, SceneError, apply_scene_settings, capture_scene_settings, embed_referenced_meshes,
embed_referenced_textures, load_scene_binary_from_bytes,
};
use nightshade::prelude::{Entity, Vec3, World, tracing};
#[derive(Clone)]
pub struct Prefab {
scene: Scene,
}
pub fn make_prefab(world: &World, root: Entity) -> Prefab {
let mut scene = subtree_to_scene(world, root, "prefab");
embed_referenced_meshes(world, &mut scene);
embed_referenced_textures(world, &mut scene);
Prefab { scene }
}
pub fn spawn_prefab(world: &mut World, prefab: &Prefab, position: Vec3) -> Vec<Entity> {
let settings = capture_scene_settings(world);
let atmosphere = world.resources.render_settings.atmosphere;
let roots = match spawn_scene(world, &prefab.scene, None) {
Ok(result) => result.root_entities,
Err(error) => {
tracing::error!("Failed to spawn prefab: {error}");
Vec::new()
}
};
apply_scene_settings(world, &settings);
world.resources.render_settings.atmosphere = atmosphere;
for &root in &roots {
crate::placement::set_position(world, root, position);
}
roots
}
pub fn save_prefab(prefab: &Prefab) -> Result<Vec<u8>, SceneError> {
let mut scene = prefab.scene.clone();
save_scene_binary_to_bytes(&mut scene)
}
pub fn load_prefab(bytes: &[u8]) -> Result<Prefab, SceneError> {
let scene = load_scene_binary_from_bytes(bytes)?;
Ok(Prefab { scene })
}