2020-05-22 00:21:33 +00:00
|
|
|
use anyhow::Result;
|
2020-05-28 02:27:55 +00:00
|
|
|
use bevy_type_registry::ComponentRegistry;
|
|
|
|
use bevy_property::{PropertyTypeRegistry, DynamicProperties};
|
2020-05-27 07:18:08 +00:00
|
|
|
use legion::prelude::{Resources, World};
|
2020-05-25 19:03:50 +00:00
|
|
|
use serde::Serialize;
|
|
|
|
use std::num::Wrapping;
|
2020-05-28 02:27:55 +00:00
|
|
|
use crate::serde::SceneSerializer;
|
2020-05-24 05:07:17 +00:00
|
|
|
|
2020-05-25 02:36:01 +00:00
|
|
|
#[derive(Default)]
|
2020-05-24 05:07:17 +00:00
|
|
|
pub struct Scene {
|
2020-05-25 19:03:50 +00:00
|
|
|
pub entities: Vec<Entity>,
|
2020-05-22 22:25:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 19:03:50 +00:00
|
|
|
pub struct Entity {
|
2020-05-28 02:27:55 +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();
|
|
|
|
for archetype in world.storage().archetypes() {
|
|
|
|
for chunkset in archetype.chunksets() {
|
|
|
|
for component_storage in chunkset.occupied() {
|
|
|
|
let mut entities = Vec::new();
|
|
|
|
for (component_type_id, _component_meta) in archetype.description().components()
|
|
|
|
{
|
|
|
|
if let Some(component_registration) =
|
|
|
|
component_registry.get(component_type_id)
|
|
|
|
{
|
|
|
|
let component_resource_set =
|
|
|
|
component_storage.components(*component_type_id).unwrap();
|
|
|
|
for (index, entity) in component_storage.entities().iter().enumerate() {
|
|
|
|
if index == entities.len() {
|
2020-05-25 19:03:50 +00:00
|
|
|
entities.push(Entity {
|
2020-05-28 02:27:55 +00:00
|
|
|
entity: entity.index(),
|
2020-05-24 05:07:17 +00:00
|
|
|
components: Vec::new(),
|
|
|
|
})
|
|
|
|
}
|
2020-05-22 22:25:31 +00:00
|
|
|
|
2020-05-28 02:27:55 +00:00
|
|
|
let properties = component_registration.get_component_properties(
|
2020-05-24 05:07:17 +00:00
|
|
|
&component_resource_set,
|
|
|
|
index,
|
|
|
|
);
|
2020-05-22 00:21:33 +00:00
|
|
|
|
2020-05-24 05:07:17 +00:00
|
|
|
entities[index].components.push(properties.to_dynamic());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 00:21:33 +00:00
|
|
|
|
2020-05-24 05:07:17 +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-05-28 02:27:55 +00:00
|
|
|
pub fn serialize_ron(&self, registry: &PropertyTypeRegistry) -> Result<String, ron::Error> {
|
2020-05-26 00:50:17 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::default()
|
|
|
|
.with_decimal_floats(true)
|
2020-05-27 02:49:46 +00:00
|
|
|
.with_indentor(" ".to_string())
|
2020-05-26 00:50:17 +00:00
|
|
|
.with_new_line("\n".to_string());
|
|
|
|
let mut buf = Vec::new();
|
2020-05-28 02:27:55 +00:00
|
|
|
let mut serializer = ron::ser::Serializer::new(&mut buf, Some(pretty_config), false)?;
|
|
|
|
let scene_serializer = SceneSerializer::new(self, registry);
|
|
|
|
scene_serializer.serialize(&mut serializer)?;
|
2020-05-26 00:50:17 +00:00
|
|
|
Ok(String::from_utf8(buf).unwrap())
|
|
|
|
}
|
2020-05-22 00:21:33 +00:00
|
|
|
}
|