2022-10-12 01:48:16 +00:00
|
|
|
use crate::{DynamicEntity, DynamicScene};
|
|
|
|
use bevy_app::AppTypeRegistry;
|
|
|
|
use bevy_ecs::{prelude::Entity, reflect::ReflectComponent, world::World};
|
2022-10-27 23:38:03 +00:00
|
|
|
use bevy_utils::default;
|
|
|
|
use std::collections::BTreeMap;
|
2022-10-12 01:48:16 +00:00
|
|
|
|
|
|
|
/// A [`DynamicScene`] builder, used to build a scene from a [`World`] by extracting some entities.
|
|
|
|
///
|
2022-10-27 23:38:03 +00:00
|
|
|
/// # Entity Order
|
|
|
|
///
|
2022-11-02 15:19:50 +00:00
|
|
|
/// Extracted entities will always be stored in ascending order based on their [id](Entity::index).
|
2022-10-27 23:38:03 +00:00
|
|
|
/// This means that inserting `Entity(1v0)` then `Entity(0v0)` will always result in the entities
|
|
|
|
/// being ordered as `[Entity(0v0), Entity(1v0)]`.
|
|
|
|
///
|
|
|
|
/// # Example
|
2022-10-12 01:48:16 +00:00
|
|
|
/// ```
|
|
|
|
/// # use bevy_scene::DynamicSceneBuilder;
|
|
|
|
/// # use bevy_app::AppTypeRegistry;
|
|
|
|
/// # use bevy_ecs::{
|
|
|
|
/// # component::Component, prelude::Entity, query::With, reflect::ReflectComponent, world::World,
|
|
|
|
/// # };
|
|
|
|
/// # use bevy_reflect::Reflect;
|
|
|
|
/// # #[derive(Component, Reflect, Default, Eq, PartialEq, Debug)]
|
|
|
|
/// # #[reflect(Component)]
|
|
|
|
/// # struct ComponentA;
|
|
|
|
/// # let mut world = World::default();
|
|
|
|
/// # world.init_resource::<AppTypeRegistry>();
|
|
|
|
/// # let entity = world.spawn(ComponentA).id();
|
|
|
|
/// let mut builder = DynamicSceneBuilder::from_world(&world);
|
|
|
|
/// builder.extract_entity(entity);
|
|
|
|
/// let dynamic_scene = builder.build();
|
|
|
|
/// ```
|
|
|
|
pub struct DynamicSceneBuilder<'w> {
|
2022-12-20 16:16:58 +00:00
|
|
|
extracted_scene: BTreeMap<u32, DynamicEntity>,
|
2022-10-12 01:48:16 +00:00
|
|
|
type_registry: AppTypeRegistry,
|
2022-12-20 16:16:58 +00:00
|
|
|
original_world: &'w World,
|
2022-10-12 01:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'w> DynamicSceneBuilder<'w> {
|
|
|
|
/// Prepare a builder that will extract entities and their component from the given [`World`].
|
|
|
|
/// All components registered in that world's [`AppTypeRegistry`] resource will be extracted.
|
|
|
|
pub fn from_world(world: &'w World) -> Self {
|
|
|
|
Self {
|
2022-12-20 16:16:58 +00:00
|
|
|
extracted_scene: default(),
|
2022-10-12 01:48:16 +00:00
|
|
|
type_registry: world.resource::<AppTypeRegistry>().clone(),
|
2022-12-20 16:16:58 +00:00
|
|
|
original_world: world,
|
2022-10-12 01:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare a builder that will extract entities and their component from the given [`World`].
|
|
|
|
/// Only components registered in the given [`AppTypeRegistry`] will be extracted.
|
|
|
|
pub fn from_world_with_type_registry(world: &'w World, type_registry: AppTypeRegistry) -> Self {
|
|
|
|
Self {
|
2022-12-20 16:16:58 +00:00
|
|
|
extracted_scene: default(),
|
2022-10-12 01:48:16 +00:00
|
|
|
type_registry,
|
2022-12-20 16:16:58 +00:00
|
|
|
original_world: world,
|
2022-10-12 01:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume the builder, producing a [`DynamicScene`].
|
2022-12-20 16:16:58 +00:00
|
|
|
///
|
|
|
|
/// To make sure the dynamic scene doesn't contain entities without any components, call
|
|
|
|
/// [`Self::remove_empty_entities`] before building the scene.
|
2022-10-12 01:48:16 +00:00
|
|
|
pub fn build(self) -> DynamicScene {
|
|
|
|
DynamicScene {
|
2022-12-20 16:16:58 +00:00
|
|
|
entities: self.extracted_scene.into_values().collect(),
|
2022-10-12 01:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract one entity from the builder's [`World`].
|
|
|
|
///
|
|
|
|
/// Re-extracting an entity that was already extracted will have no effect.
|
|
|
|
pub fn extract_entity(&mut self, entity: Entity) -> &mut Self {
|
|
|
|
self.extract_entities(std::iter::once(entity))
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:43:30 +00:00
|
|
|
/// Despawns all entities with no components.
|
2022-12-20 16:16:58 +00:00
|
|
|
///
|
|
|
|
/// These were likely created because none of their components were present in the provided type registry upon extraction.
|
|
|
|
pub fn remove_empty_entities(&mut self) -> &mut Self {
|
|
|
|
self.extracted_scene
|
|
|
|
.retain(|_, entity| !entity.components.is_empty());
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-10-12 01:48:16 +00:00
|
|
|
/// Extract entities from the builder's [`World`].
|
|
|
|
///
|
|
|
|
/// Re-extracting an entity that was already extracted will have no effect.
|
|
|
|
///
|
|
|
|
/// Extracting entities can be used to extract entities from a query:
|
|
|
|
/// ```
|
|
|
|
/// # use bevy_scene::DynamicSceneBuilder;
|
|
|
|
/// # use bevy_app::AppTypeRegistry;
|
|
|
|
/// # use bevy_ecs::{
|
|
|
|
/// # component::Component, prelude::Entity, query::With, reflect::ReflectComponent, world::World,
|
|
|
|
/// # };
|
|
|
|
/// # use bevy_reflect::Reflect;
|
|
|
|
/// #[derive(Component, Default, Reflect)]
|
|
|
|
/// #[reflect(Component)]
|
|
|
|
/// struct MyComponent;
|
|
|
|
///
|
|
|
|
/// # let mut world = World::default();
|
|
|
|
/// # world.init_resource::<AppTypeRegistry>();
|
|
|
|
/// # let _entity = world.spawn(MyComponent).id();
|
|
|
|
/// let mut query = world.query_filtered::<Entity, With<MyComponent>>();
|
|
|
|
///
|
|
|
|
/// let mut builder = DynamicSceneBuilder::from_world(&world);
|
|
|
|
/// builder.extract_entities(query.iter(&world));
|
|
|
|
/// let scene = builder.build();
|
|
|
|
/// ```
|
|
|
|
pub fn extract_entities(&mut self, entities: impl Iterator<Item = Entity>) -> &mut Self {
|
|
|
|
let type_registry = self.type_registry.read();
|
|
|
|
|
|
|
|
for entity in entities {
|
2022-11-02 15:19:50 +00:00
|
|
|
let index = entity.index();
|
2022-10-27 23:38:03 +00:00
|
|
|
|
2022-12-20 16:16:58 +00:00
|
|
|
if self.extracted_scene.contains_key(&index) {
|
2022-10-12 01:48:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut entry = DynamicEntity {
|
2022-11-02 15:19:50 +00:00
|
|
|
entity: index,
|
2022-10-12 01:48:16 +00:00
|
|
|
components: Vec::new(),
|
|
|
|
};
|
|
|
|
|
2023-02-03 05:53:58 +00:00
|
|
|
let entity = self.original_world.entity(entity);
|
|
|
|
for component_id in entity.archetype().components() {
|
2022-10-12 01:48:16 +00:00
|
|
|
let reflect_component = self
|
2022-12-20 16:16:58 +00:00
|
|
|
.original_world
|
2022-10-12 01:48:16 +00:00
|
|
|
.components()
|
|
|
|
.get_info(component_id)
|
|
|
|
.and_then(|info| type_registry.get(info.type_id().unwrap()))
|
2023-02-03 05:53:58 +00:00
|
|
|
.and_then(|registration| registration.data::<ReflectComponent>())
|
|
|
|
.and_then(|reflect_component| reflect_component.reflect(entity));
|
2022-10-12 01:48:16 +00:00
|
|
|
|
|
|
|
if let Some(reflect_component) = reflect_component {
|
2023-02-03 05:53:58 +00:00
|
|
|
entry.components.push(reflect_component.clone_value());
|
2022-10-12 01:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-20 16:16:58 +00:00
|
|
|
self.extracted_scene.insert(index, entry);
|
2022-10-12 01:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drop(type_registry);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use bevy_app::AppTypeRegistry;
|
|
|
|
use bevy_ecs::{
|
|
|
|
component::Component, prelude::Entity, query::With, reflect::ReflectComponent, world::World,
|
|
|
|
};
|
|
|
|
|
|
|
|
use bevy_reflect::Reflect;
|
|
|
|
|
|
|
|
use super::DynamicSceneBuilder;
|
|
|
|
|
|
|
|
#[derive(Component, Reflect, Default, Eq, PartialEq, Debug)]
|
|
|
|
#[reflect(Component)]
|
|
|
|
struct ComponentA;
|
|
|
|
#[derive(Component, Reflect, Default, Eq, PartialEq, Debug)]
|
|
|
|
#[reflect(Component)]
|
|
|
|
struct ComponentB;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_one_entity() {
|
|
|
|
let mut world = World::default();
|
|
|
|
|
|
|
|
let atr = AppTypeRegistry::default();
|
|
|
|
atr.write().register::<ComponentA>();
|
|
|
|
world.insert_resource(atr);
|
|
|
|
|
|
|
|
let entity = world.spawn((ComponentA, ComponentB)).id();
|
|
|
|
|
|
|
|
let mut builder = DynamicSceneBuilder::from_world(&world);
|
|
|
|
builder.extract_entity(entity);
|
|
|
|
let scene = builder.build();
|
|
|
|
|
|
|
|
assert_eq!(scene.entities.len(), 1);
|
2022-11-02 15:19:50 +00:00
|
|
|
assert_eq!(scene.entities[0].entity, entity.index());
|
2022-10-12 01:48:16 +00:00
|
|
|
assert_eq!(scene.entities[0].components.len(), 1);
|
|
|
|
assert!(scene.entities[0].components[0].represents::<ComponentA>());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_one_entity_twice() {
|
|
|
|
let mut world = World::default();
|
|
|
|
|
|
|
|
let atr = AppTypeRegistry::default();
|
|
|
|
atr.write().register::<ComponentA>();
|
|
|
|
world.insert_resource(atr);
|
|
|
|
|
|
|
|
let entity = world.spawn((ComponentA, ComponentB)).id();
|
|
|
|
|
|
|
|
let mut builder = DynamicSceneBuilder::from_world(&world);
|
|
|
|
builder.extract_entity(entity);
|
|
|
|
builder.extract_entity(entity);
|
|
|
|
let scene = builder.build();
|
|
|
|
|
|
|
|
assert_eq!(scene.entities.len(), 1);
|
2022-11-02 15:19:50 +00:00
|
|
|
assert_eq!(scene.entities[0].entity, entity.index());
|
2022-10-12 01:48:16 +00:00
|
|
|
assert_eq!(scene.entities[0].components.len(), 1);
|
|
|
|
assert!(scene.entities[0].components[0].represents::<ComponentA>());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_one_entity_two_components() {
|
|
|
|
let mut world = World::default();
|
|
|
|
|
|
|
|
let atr = AppTypeRegistry::default();
|
|
|
|
{
|
|
|
|
let mut register = atr.write();
|
|
|
|
register.register::<ComponentA>();
|
|
|
|
register.register::<ComponentB>();
|
|
|
|
}
|
|
|
|
world.insert_resource(atr);
|
|
|
|
|
|
|
|
let entity = world.spawn((ComponentA, ComponentB)).id();
|
|
|
|
|
|
|
|
let mut builder = DynamicSceneBuilder::from_world(&world);
|
|
|
|
builder.extract_entity(entity);
|
|
|
|
let scene = builder.build();
|
|
|
|
|
|
|
|
assert_eq!(scene.entities.len(), 1);
|
2022-11-02 15:19:50 +00:00
|
|
|
assert_eq!(scene.entities[0].entity, entity.index());
|
2022-10-12 01:48:16 +00:00
|
|
|
assert_eq!(scene.entities[0].components.len(), 2);
|
|
|
|
assert!(scene.entities[0].components[0].represents::<ComponentA>());
|
|
|
|
assert!(scene.entities[0].components[1].represents::<ComponentB>());
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:38:03 +00:00
|
|
|
#[test]
|
|
|
|
fn extract_entity_order() {
|
|
|
|
let mut world = World::default();
|
|
|
|
world.init_resource::<AppTypeRegistry>();
|
|
|
|
|
|
|
|
// Spawn entities in order
|
|
|
|
let entity_a = world.spawn_empty().id();
|
|
|
|
let entity_b = world.spawn_empty().id();
|
|
|
|
let entity_c = world.spawn_empty().id();
|
|
|
|
let entity_d = world.spawn_empty().id();
|
|
|
|
|
|
|
|
let mut builder = DynamicSceneBuilder::from_world(&world);
|
|
|
|
|
|
|
|
// Insert entities out of order
|
|
|
|
builder.extract_entity(entity_b);
|
|
|
|
builder.extract_entities([entity_d, entity_a].into_iter());
|
|
|
|
builder.extract_entity(entity_c);
|
|
|
|
|
|
|
|
let mut entities = builder.build().entities.into_iter();
|
|
|
|
|
|
|
|
// Assert entities are ordered
|
2022-11-02 15:19:50 +00:00
|
|
|
assert_eq!(entity_a.index(), entities.next().map(|e| e.entity).unwrap());
|
|
|
|
assert_eq!(entity_b.index(), entities.next().map(|e| e.entity).unwrap());
|
|
|
|
assert_eq!(entity_c.index(), entities.next().map(|e| e.entity).unwrap());
|
|
|
|
assert_eq!(entity_d.index(), entities.next().map(|e| e.entity).unwrap());
|
2022-10-27 23:38:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 01:48:16 +00:00
|
|
|
#[test]
|
|
|
|
fn extract_query() {
|
|
|
|
let mut world = World::default();
|
|
|
|
|
|
|
|
let atr = AppTypeRegistry::default();
|
|
|
|
{
|
|
|
|
let mut register = atr.write();
|
|
|
|
register.register::<ComponentA>();
|
|
|
|
register.register::<ComponentB>();
|
|
|
|
}
|
|
|
|
world.insert_resource(atr);
|
|
|
|
|
|
|
|
let entity_a_b = world.spawn((ComponentA, ComponentB)).id();
|
|
|
|
let entity_a = world.spawn(ComponentA).id();
|
|
|
|
let _entity_b = world.spawn(ComponentB).id();
|
|
|
|
|
|
|
|
let mut query = world.query_filtered::<Entity, With<ComponentA>>();
|
|
|
|
let mut builder = DynamicSceneBuilder::from_world(&world);
|
|
|
|
builder.extract_entities(query.iter(&world));
|
|
|
|
let scene = builder.build();
|
|
|
|
|
|
|
|
assert_eq!(scene.entities.len(), 2);
|
|
|
|
let mut scene_entities = vec![scene.entities[0].entity, scene.entities[1].entity];
|
|
|
|
scene_entities.sort();
|
2022-11-02 15:19:50 +00:00
|
|
|
assert_eq!(scene_entities, [entity_a_b.index(), entity_a.index()]);
|
2022-10-12 01:48:16 +00:00
|
|
|
}
|
2022-12-20 16:16:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_componentless_entity() {
|
|
|
|
let mut world = World::default();
|
|
|
|
|
|
|
|
let atr = AppTypeRegistry::default();
|
|
|
|
atr.write().register::<ComponentA>();
|
|
|
|
world.insert_resource(atr);
|
|
|
|
|
|
|
|
let entity_a = world.spawn(ComponentA).id();
|
|
|
|
let entity_b = world.spawn(ComponentB).id();
|
|
|
|
|
|
|
|
let mut builder = DynamicSceneBuilder::from_world(&world);
|
|
|
|
builder.extract_entities([entity_a, entity_b].into_iter());
|
|
|
|
builder.remove_empty_entities();
|
|
|
|
let scene = builder.build();
|
|
|
|
|
|
|
|
assert_eq!(scene.entities.len(), 1);
|
|
|
|
assert_eq!(scene.entities[0].entity, entity_a.index());
|
|
|
|
}
|
2022-10-12 01:48:16 +00:00
|
|
|
}
|