2023-06-18 23:43:10 +00:00
|
|
|
use crate::{
|
|
|
|
component::Component,
|
2023-08-28 17:18:16 +00:00
|
|
|
entity::{Entity, EntityMapper, MapEntities},
|
2023-06-18 23:43:10 +00:00
|
|
|
world::World,
|
|
|
|
};
|
|
|
|
use bevy_reflect::FromType;
|
2023-08-28 17:18:16 +00:00
|
|
|
use bevy_utils::HashMap;
|
2023-06-18 23:43:10 +00:00
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// See [`MapEntities`] for more information.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ReflectMapEntities {
|
|
|
|
map_all_entities: fn(&mut World, &mut EntityMapper),
|
|
|
|
map_entities: fn(&mut World, &mut EntityMapper, &[Entity]),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReflectMapEntities {
|
2023-08-28 17:18:16 +00:00
|
|
|
/// A general method for applying [`MapEntities`] behavior to all elements in an [`HashMap<Entity, Entity>`].
|
2023-06-18 23:43:10 +00:00
|
|
|
///
|
2023-08-28 17:18:16 +00:00
|
|
|
/// Be mindful in its usage: Works best in situations where the entities in the [`HashMap<Entity, 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
|
2023-08-28 17:18:16 +00:00
|
|
|
/// by the [`HashMap<Entity, 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.
|
2023-08-28 17:18:16 +00:00
|
|
|
pub fn map_all_entities(&self, world: &mut World, entity_map: &mut HashMap<Entity, Entity>) {
|
|
|
|
EntityMapper::world_scope(entity_map, world, self.map_all_entities);
|
2023-06-18 23:43:10 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:18:16 +00:00
|
|
|
/// A general method for applying [`MapEntities`] behavior to elements in an [`HashMap<Entity, 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
|
2023-08-28 17:18:16 +00:00
|
|
|
/// in the [`HashMap<Entity, 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,
|
|
|
|
entity_map: &mut HashMap<Entity, Entity>,
|
|
|
|
entities: &[Entity],
|
|
|
|
) {
|
|
|
|
EntityMapper::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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|