2020-05-22 00:21:33 +00:00
|
|
|
use crate::{ComponentRegistry, ComponentRegistryContext, SceneDeserializer};
|
|
|
|
use anyhow::Result;
|
|
|
|
use bevy_app::FromResources;
|
|
|
|
use bevy_asset::AssetLoader;
|
2020-05-22 22:36:48 +00:00
|
|
|
use bevy_property::DynamicProperties;
|
2020-05-22 00:21:33 +00:00
|
|
|
use legion::prelude::{Resources, World};
|
|
|
|
use serde::de::DeserializeSeed;
|
2020-05-22 22:25:31 +00:00
|
|
|
use serde::{Serialize, Deserialize};
|
2020-05-22 00:21:33 +00:00
|
|
|
use std::{
|
|
|
|
path::Path,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
|
|
|
|
2020-05-22 22:25:31 +00:00
|
|
|
pub struct DynamicScene {
|
|
|
|
pub entities: Vec<SceneEntity>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SceneEntity {
|
|
|
|
pub entity: u32,
|
|
|
|
pub components: Vec<DynamicProperties>,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-22 00:21:33 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Scene {
|
|
|
|
pub world: World,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SceneLoader {
|
|
|
|
component_registry: Arc<RwLock<ComponentRegistry>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromResources for SceneLoader {
|
|
|
|
fn from_resources(resources: &Resources) -> Self {
|
|
|
|
let component_registry = resources
|
|
|
|
.get::<ComponentRegistryContext>()
|
|
|
|
.expect("SceneLoader requires the ComponentRegistry resource.");
|
|
|
|
SceneLoader {
|
|
|
|
component_registry: component_registry.value.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssetLoader<Scene> for SceneLoader {
|
|
|
|
fn from_bytes(&self, _asset_path: &Path, bytes: Vec<u8>) -> Result<Scene> {
|
|
|
|
let mut deserializer = ron::de::Deserializer::from_bytes(&bytes).unwrap();
|
|
|
|
let mut scene = Scene::default();
|
|
|
|
let scene_deserializer = SceneDeserializer {
|
|
|
|
component_registry: &self.component_registry.read().unwrap(),
|
|
|
|
scene: &mut scene,
|
|
|
|
};
|
|
|
|
|
|
|
|
scene_deserializer.deserialize(&mut deserializer).unwrap();
|
|
|
|
|
|
|
|
Ok(scene)
|
|
|
|
}
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
|
|
static EXTENSIONS: &[&str] = &["scn"];
|
|
|
|
EXTENSIONS
|
|
|
|
}
|
|
|
|
}
|