2023-06-18 23:43:10 +00:00
|
|
|
use crate::{
|
|
|
|
component::Component,
|
2024-02-12 15:02:24 +00:00
|
|
|
entity::{Entity, EntityHashMap, MapEntities, SceneEntityMapper},
|
2023-06-18 23:43:10 +00:00
|
|
|
world::World,
|
|
|
|
};
|
|
|
|
use bevy_reflect::FromType;
|
|
|
|
|
|
|
|
/// For a specific type of component, this maps any fields with values of type [`Entity`] to a new world.
|
2023-07-10 00:11:51 +00:00
|
|
|
/// Since a given `Entity` ID is only valid for the world it came from, when performing deserialization
|
2023-06-18 23:43:10 +00:00
|
|
|
/// any stored IDs need to be re-allocated in the destination world.
|
|
|
|
///
|
2024-01-28 19:51:46 +00:00
|
|
|
/// See [`SceneEntityMapper`] and [`MapEntities`] for more information.
|
2023-06-18 23:43:10 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ReflectMapEntities {
|
2024-01-28 19:51:46 +00:00
|
|
|
map_all_entities: fn(&mut World, &mut SceneEntityMapper),
|
|
|
|
map_entities: fn(&mut World, &mut SceneEntityMapper, &[Entity]),
|
2023-06-18 23:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReflectMapEntities {
|
2024-02-12 15:02:24 +00:00
|
|
|
/// A general method for applying [`MapEntities`] behavior to all elements in an [`EntityHashMap<Entity>`].
|
2023-06-18 23:43:10 +00:00
|
|
|
///
|
2024-02-12 15:02:24 +00:00
|
|
|
/// Be mindful in its usage: Works best in situations where the entities in the [`EntityHashMap<Entity>`] are newly
|
2023-06-18 23:43:10 +00:00
|
|
|
/// created, before systems have a chance to add new components. If some of the entities referred to
|
2024-02-12 15:02:24 +00:00
|
|
|
/// by the [`EntityHashMap<Entity>`] might already contain valid entity references, you should use [`map_entities`](Self::map_entities).
|
2023-06-18 23:43:10 +00:00
|
|
|
///
|
|
|
|
/// An example of this: A scene can be loaded with `Parent` components, but then a `Parent` component can be added
|
|
|
|
/// to these entities after they have been loaded. If you reload the scene using [`map_all_entities`](Self::map_all_entities), those `Parent`
|
|
|
|
/// components with already valid entity references could be updated to point at something else entirely.
|
2024-02-12 15:02:24 +00:00
|
|
|
pub fn map_all_entities(&self, world: &mut World, entity_map: &mut EntityHashMap<Entity>) {
|
2024-01-28 19:51:46 +00:00
|
|
|
SceneEntityMapper::world_scope(entity_map, world, self.map_all_entities);
|
2023-06-18 23:43:10 +00:00
|
|
|
}
|
|
|
|
|
2024-02-12 15:02:24 +00:00
|
|
|
/// A general method for applying [`MapEntities`] behavior to elements in an [`EntityHashMap<Entity>`]. Unlike
|
2023-06-18 23:43:10 +00:00
|
|
|
/// [`map_all_entities`](Self::map_all_entities), this is applied to specific entities, not all values
|
2024-02-12 15:02:24 +00:00
|
|
|
/// in the [`EntityHashMap<Entity>`].
|
2023-06-18 23:43:10 +00:00
|
|
|
///
|
|
|
|
/// This is useful mostly for when you need to be careful not to update components that already contain valid entity
|
|
|
|
/// values. See [`map_all_entities`](Self::map_all_entities) for more details.
|
2023-08-28 17:18:16 +00:00
|
|
|
pub fn map_entities(
|
|
|
|
&self,
|
|
|
|
world: &mut World,
|
2024-02-12 15:02:24 +00:00
|
|
|
entity_map: &mut EntityHashMap<Entity>,
|
2023-08-28 17:18:16 +00:00
|
|
|
entities: &[Entity],
|
|
|
|
) {
|
2024-01-28 19:51:46 +00:00
|
|
|
SceneEntityMapper::world_scope(entity_map, world, |world, mapper| {
|
2023-06-18 23:43:10 +00:00
|
|
|
(self.map_entities)(world, mapper, entities);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: Component + MapEntities> FromType<C> for ReflectMapEntities {
|
|
|
|
fn from_type() -> Self {
|
|
|
|
ReflectMapEntities {
|
|
|
|
map_entities: |world, entity_mapper, entities| {
|
|
|
|
for &entity in entities {
|
|
|
|
if let Some(mut component) = world.get_mut::<C>(entity) {
|
|
|
|
component.map_entities(entity_mapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
map_all_entities: |world, entity_mapper| {
|
2023-08-28 17:18:16 +00:00
|
|
|
let entities = entity_mapper
|
|
|
|
.get_map()
|
|
|
|
.values()
|
|
|
|
.copied()
|
|
|
|
.collect::<Vec<Entity>>();
|
2023-06-18 23:43:10 +00:00
|
|
|
for entity in &entities {
|
|
|
|
if let Some(mut component) = world.get_mut::<C>(*entity) {
|
|
|
|
component.map_entities(entity_mapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|