use crate::{
change_detection::Mut,
component::Component,
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
system::Resource,
world::{FromWorld, World},
};
use bevy_reflect::{
impl_from_reflect_value, impl_reflect_value, FromType, Reflect, ReflectDeserialize,
ReflectSerialize,
};
#[derive(Clone)]
pub struct ReflectComponent(ReflectComponentFns);
#[derive(Clone)]
pub struct ReflectComponentFns {
pub insert: fn(&mut World, Entity, &dyn Reflect),
pub apply: fn(&mut World, Entity, &dyn Reflect),
pub apply_or_insert: fn(&mut World, Entity, &dyn Reflect),
pub remove: fn(&mut World, Entity),
pub reflect: fn(&World, Entity) -> Option<&dyn Reflect>,
pub reflect_mut: unsafe fn(&World, Entity) -> Option<Mut<dyn Reflect>>,
pub copy: fn(&World, &mut World, Entity, Entity),
}
impl ReflectComponentFns {
pub fn new<T: Component + Reflect + FromWorld>() -> Self {
<ReflectComponent as FromType<T>>::from_type().0
}
}
impl ReflectComponent {
pub fn insert(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
(self.0.insert)(world, entity, component);
}
pub fn apply(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
(self.0.apply)(world, entity, component);
}
pub fn apply_or_insert(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
(self.0.apply_or_insert)(world, entity, component);
}
pub fn remove(&self, world: &mut World, entity: Entity) {
(self.0.remove)(world, entity);
}
pub fn reflect<'a>(&self, world: &'a World, entity: Entity) -> Option<&'a dyn Reflect> {
(self.0.reflect)(world, entity)
}
pub fn reflect_mut<'a>(
&self,
world: &'a mut World,
entity: Entity,
) -> Option<Mut<'a, dyn Reflect>> {
unsafe { (self.0.reflect_mut)(world, entity) }
}
pub unsafe fn reflect_unchecked_mut<'a>(
&self,
world: &'a World,
entity: Entity,
) -> Option<Mut<'a, dyn Reflect>> {
(self.0.reflect_mut)(world, entity)
}
pub fn copy(
&self,
source_world: &World,
destination_world: &mut World,
source_entity: Entity,
destination_entity: Entity,
) {
(self.0.copy)(
source_world,
destination_world,
source_entity,
destination_entity,
);
}
pub fn new(fns: ReflectComponentFns) -> Self {
Self(fns)
}
}
impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
fn from_type() -> Self {
ReflectComponent(ReflectComponentFns {
insert: |world, entity, reflected_component| {
let mut component = C::from_world(world);
component.apply(reflected_component);
world.entity_mut(entity).insert(component);
},
apply: |world, entity, reflected_component| {
let mut component = world.get_mut::<C>(entity).unwrap();
component.apply(reflected_component);
},
apply_or_insert: |world, entity, reflected_component| {
if let Some(mut component) = world.get_mut::<C>(entity) {
component.apply(reflected_component);
} else {
let mut component = C::from_world(world);
component.apply(reflected_component);
world.entity_mut(entity).insert(component);
}
},
remove: |world, entity| {
world.entity_mut(entity).remove::<C>();
},
copy: |source_world, destination_world, source_entity, destination_entity| {
let source_component = source_world.get::<C>(source_entity).unwrap();
let mut destination_component = C::from_world(destination_world);
destination_component.apply(source_component);
destination_world
.entity_mut(destination_entity)
.insert(destination_component);
},
reflect: |world, entity| {
world
.get_entity(entity)?
.get::<C>()
.map(|c| c as &dyn Reflect)
},
reflect_mut: |world, entity| {
unsafe {
world
.get_entity(entity)?
.get_unchecked_mut::<C>(world.last_change_tick(), world.read_change_tick())
.map(|c| Mut {
value: c.value as &mut dyn Reflect,
ticks: c.ticks,
})
}
},
})
}
}
#[derive(Clone)]
pub struct ReflectResource(ReflectResourceFns);
#[derive(Clone)]
pub struct ReflectResourceFns {
pub insert: fn(&mut World, &dyn Reflect),
pub apply: fn(&mut World, &dyn Reflect),
pub apply_or_insert: fn(&mut World, &dyn Reflect),
pub remove: fn(&mut World),
pub reflect: fn(&World) -> Option<&dyn Reflect>,
pub reflect_unchecked_mut: unsafe fn(&World) -> Option<Mut<dyn Reflect>>,
pub copy: fn(&World, &mut World),
}
impl ReflectResourceFns {
pub fn new<T: Resource + Reflect + FromWorld>() -> Self {
<ReflectResource as FromType<T>>::from_type().0
}
}
impl ReflectResource {
pub fn insert(&self, world: &mut World, resource: &dyn Reflect) {
(self.0.insert)(world, resource);
}
pub fn apply(&self, world: &mut World, resource: &dyn Reflect) {
(self.0.apply)(world, resource);
}
pub fn apply_or_insert(&self, world: &mut World, resource: &dyn Reflect) {
(self.0.apply_or_insert)(world, resource);
}
pub fn remove(&self, world: &mut World) {
(self.0.remove)(world);
}
pub fn reflect<'a>(&self, world: &'a World) -> Option<&'a dyn Reflect> {
(self.0.reflect)(world)
}
pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option<Mut<'a, dyn Reflect>> {
unsafe { (self.0.reflect_unchecked_mut)(world) }
}
pub unsafe fn reflect_unchecked_mut<'a>(
&self,
world: &'a World,
) -> Option<Mut<'a, dyn Reflect>> {
(self.0.reflect_unchecked_mut)(world)
}
pub fn copy(&self, source_world: &World, destination_world: &mut World) {
(self.0.copy)(source_world, destination_world);
}
pub fn new(&self, fns: ReflectResourceFns) -> Self {
Self(fns)
}
}
impl<C: Resource + Reflect + FromWorld> FromType<C> for ReflectResource {
fn from_type() -> Self {
ReflectResource(ReflectResourceFns {
insert: |world, reflected_resource| {
let mut resource = C::from_world(world);
resource.apply(reflected_resource);
world.insert_resource(resource);
},
apply: |world, reflected_resource| {
let mut resource = world.resource_mut::<C>();
resource.apply(reflected_resource);
},
apply_or_insert: |world, reflected_resource| {
if let Some(mut resource) = world.get_resource_mut::<C>() {
resource.apply(reflected_resource);
} else {
let mut resource = C::from_world(world);
resource.apply(reflected_resource);
world.insert_resource(resource);
}
},
remove: |world| {
world.remove_resource::<C>();
},
reflect: |world| world.get_resource::<C>().map(|res| res as &dyn Reflect),
reflect_unchecked_mut: |world| {
unsafe {
world.get_resource_unchecked_mut::<C>().map(|res| Mut {
value: res.value as &mut dyn Reflect,
ticks: res.ticks,
})
}
},
copy: |source_world, destination_world| {
let source_resource = source_world.resource::<C>();
let mut destination_resource = C::from_world(destination_world);
destination_resource.apply(source_resource);
destination_world.insert_resource(destination_resource);
},
})
}
}
impl_reflect_value!(Entity(Hash, PartialEq, Serialize, Deserialize));
impl_from_reflect_value!(Entity);
#[derive(Clone)]
pub struct ReflectMapEntities {
map_entities: fn(&mut World, &EntityMap) -> Result<(), MapEntitiesError>,
}
impl ReflectMapEntities {
pub fn map_entities(
&self,
world: &mut World,
entity_map: &EntityMap,
) -> Result<(), MapEntitiesError> {
(self.map_entities)(world, entity_map)
}
}
impl<C: Component + MapEntities> FromType<C> for ReflectMapEntities {
fn from_type() -> Self {
ReflectMapEntities {
map_entities: |world, entity_map| {
for entity in entity_map.values() {
if let Some(mut component) = world.get_mut::<C>(entity) {
component.map_entities(entity_map)?;
}
}
Ok(())
},
}
}
}