Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ecs/src/reflect/map_entities.rs
6600 views
1
use crate::entity::{EntityMapper, MapEntities};
2
use bevy_reflect::{FromReflect, FromType, PartialReflect};
3
4
/// For a specific type of value, this maps any fields with values of type [`Entity`] to a new world.
5
///
6
/// Since a given `Entity` ID is only valid for the world it came from, when performing deserialization
7
/// any stored IDs need to be re-allocated in the destination world.
8
///
9
/// See [`EntityMapper`] and [`MapEntities`] for more information.
10
///
11
/// [`Entity`]: crate::entity::Entity
12
/// [`EntityMapper`]: crate::entity::EntityMapper
13
#[derive(Clone)]
14
pub struct ReflectMapEntities {
15
map_entities: fn(&mut dyn PartialReflect, &mut dyn EntityMapper),
16
}
17
18
impl ReflectMapEntities {
19
/// A general method for remapping entities in a reflected value via an [`EntityMapper`].
20
///
21
/// # Panics
22
/// Panics if the type of the reflected value doesn't match.
23
pub fn map_entities(&self, reflected: &mut dyn PartialReflect, mapper: &mut dyn EntityMapper) {
24
(self.map_entities)(reflected, mapper);
25
}
26
}
27
28
impl<C: FromReflect + MapEntities> FromType<C> for ReflectMapEntities {
29
fn from_type() -> Self {
30
ReflectMapEntities {
31
map_entities: |reflected, mut mapper| {
32
let mut concrete = C::from_reflect(reflected).expect("reflected type should match");
33
concrete.map_entities(&mut mapper);
34
reflected.apply(&concrete);
35
},
36
}
37
}
38
}
39
40