2020-05-29 22:51:36 +00:00
|
|
|
use crate::serde::SceneSerializer;
|
2020-05-22 00:21:33 +00:00
|
|
|
use anyhow::Result;
|
2020-07-10 04:18:35 +00:00
|
|
|
use bevy_ecs::World;
|
2020-05-29 22:51:36 +00:00
|
|
|
use bevy_property::{DynamicProperties, PropertyTypeRegistry};
|
2020-05-28 02:27:55 +00:00
|
|
|
use bevy_type_registry::ComponentRegistry;
|
2020-05-25 19:03:50 +00:00
|
|
|
use serde::Serialize;
|
2020-05-24 05:07:17 +00:00
|
|
|
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Debug, Default)]
|
2020-05-24 05:07:17 +00:00
|
|
|
pub struct Scene {
|
2020-07-10 08:37:06 +00:00
|
|
|
pub entities: Vec<Entity>,
|
2020-05-22 22:25:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Debug)]
|
2020-05-25 19:03:50 +00:00
|
|
|
pub struct Entity {
|
2020-09-18 00:16:38 +00:00
|
|
|
pub entity: u32,
|
2020-05-25 02:36:01 +00:00
|
|
|
pub components: Vec<DynamicProperties>,
|
|
|
|
}
|
|
|
|
|
2020-05-24 05:07:17 +00:00
|
|
|
impl Scene {
|
|
|
|
pub fn from_world(world: &World, component_registry: &ComponentRegistry) -> Self {
|
|
|
|
let mut scene = Scene::default();
|
2020-07-10 04:18:35 +00:00
|
|
|
for archetype in world.archetypes() {
|
|
|
|
let mut entities = Vec::new();
|
|
|
|
for (index, entity) in archetype.iter_entities().enumerate() {
|
|
|
|
if index == entities.len() {
|
|
|
|
entities.push(Entity {
|
2020-09-18 00:16:38 +00:00
|
|
|
entity: entity.id(),
|
2020-07-10 04:18:35 +00:00
|
|
|
components: Vec::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for type_info in archetype.types() {
|
|
|
|
if let Some(component_registration) = component_registry.get(&type_info.id()) {
|
|
|
|
let properties =
|
|
|
|
component_registration.get_component_properties(&archetype, index);
|
2020-05-22 00:21:33 +00:00
|
|
|
|
2020-07-10 04:18:35 +00:00
|
|
|
entities[index].components.push(properties.to_dynamic());
|
2020-05-24 05:07:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 04:18:35 +00:00
|
|
|
|
|
|
|
scene.entities.extend(entities.drain(..));
|
2020-05-22 00:21:33 +00:00
|
|
|
}
|
2020-05-24 05:07:17 +00:00
|
|
|
|
|
|
|
scene
|
|
|
|
}
|
|
|
|
|
2020-05-26 00:50:17 +00:00
|
|
|
// TODO: move to AssetSaver when it is implemented
|
2020-09-10 01:54:20 +00:00
|
|
|
pub fn serialize_ron(&self, registry: &PropertyTypeRegistry) -> Result<String, ron::Error> {
|
2020-05-29 23:06:23 +00:00
|
|
|
serialize_ron(SceneSerializer::new(self, registry))
|
2020-05-26 00:50:17 +00:00
|
|
|
}
|
2020-05-22 00:21:33 +00:00
|
|
|
}
|
2020-05-29 23:06:23 +00:00
|
|
|
|
2020-09-10 01:51:59 +00:00
|
|
|
pub fn serialize_ron<S>(serialize: S) -> Result<String, ron::Error>
|
2020-05-29 23:06:23 +00:00
|
|
|
where
|
|
|
|
S: Serialize,
|
|
|
|
{
|
2020-09-10 01:51:59 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::default()
|
|
|
|
.with_decimal_floats(true)
|
|
|
|
.with_indentor(" ".to_string())
|
|
|
|
.with_new_line("\n".to_string());
|
2020-05-29 23:06:23 +00:00
|
|
|
let mut buf = Vec::new();
|
2020-09-10 01:51:59 +00:00
|
|
|
let mut ron_serializer = ron::ser::Serializer::new(&mut buf, Some(pretty_config), false)?;
|
2020-05-29 23:06:23 +00:00
|
|
|
serialize.serialize(&mut ron_serializer)?;
|
|
|
|
Ok(String::from_utf8(buf).unwrap())
|
|
|
|
}
|