Skip to content

Commit

Permalink
Use entity references everywhere
Browse files Browse the repository at this point in the history
Fix

Nit
  • Loading branch information
Suficio committed Jan 28, 2023
1 parent 1648e06 commit f586377
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
36 changes: 14 additions & 22 deletions crates/bevy_ecs/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ pub struct ReflectComponent(ReflectComponentFns);
#[derive(Clone)]
pub struct ReflectComponentFns {
/// Function pointer implementing [`ReflectComponent::insert()`].
pub insert: fn(&mut World, Entity, &dyn Reflect),
pub insert: fn(&mut EntityMut, &dyn Reflect),
/// Function pointer implementing [`ReflectComponent::apply()`].
pub apply: fn(&mut EntityMut, &dyn Reflect),
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
pub apply_or_insert: fn(&mut World, Entity, &dyn Reflect),
pub apply_or_insert: fn(&mut EntityMut, &dyn Reflect),
/// Function pointer implementing [`ReflectComponent::remove()`].
pub remove: fn(&mut EntityMut),
/// Function pointer implementing [`ReflectComponent::contains()`].
pub contains: fn(EntityRef) -> bool,
/// Function pointer implementing [`ReflectComponent::reflect()`].
pub reflect: fn(EntityRef) -> Option<&dyn Reflect>,
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
pub reflect_mut: for<'a> fn(&'a mut EntityMut) -> Option<Mut<'a, dyn Reflect>>,
pub reflect_mut: for<'a> fn(&'a mut EntityMut<'_>) -> Option<Mut<'a, dyn Reflect>>,
/// Function pointer implementing [`ReflectComponent::reflect_unchecked_mut()`].
///
/// # Safety
Expand All @@ -81,12 +81,8 @@ impl ReflectComponentFns {

impl ReflectComponent {
/// Insert a reflected [`Component`] into the entity like [`insert()`](crate::world::EntityMut::insert).
///
/// # Panics
///
/// Panics if there is no such entity.
pub fn insert(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
(self.0.insert)(world, entity, component);
pub fn insert(&self, entity: &mut EntityMut, component: &dyn Reflect) {
(self.0.insert)(entity, component);
}

/// Uses reflection to set the value of this [`Component`] type in the entity to the given value.
Expand All @@ -99,12 +95,8 @@ impl ReflectComponent {
}

/// Uses reflection to set the value of this [`Component`] type in the entity to the given value or insert a new one if it does not exist.
///
/// # Panics
///
/// Panics if the `entity` does not exist.
pub fn apply_or_insert(&self, world: &mut World, entity: Entity, component: &dyn Reflect) {
(self.0.apply_or_insert)(world, entity, component);
pub fn apply_or_insert(&self, entity: &mut EntityMut, component: &dyn Reflect) {
(self.0.apply_or_insert)(entity, component);
}

/// Removes this [`Component`] type from the entity. Does nothing if it doesn't exist.
Expand Down Expand Up @@ -184,22 +176,22 @@ impl ReflectComponent {
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);
insert: |entity, reflected_component| {
let mut component = entity.world_scope(|world| C::from_world(world));
component.apply(reflected_component);
world.entity_mut(entity).insert(component);
entity.insert(component);
},
apply: |entity, reflected_component| {
let mut component = entity.get_mut::<C>().unwrap();
component.apply(reflected_component);
},
apply_or_insert: |world, entity, reflected_component| {
if let Some(mut component) = world.get_mut::<C>(entity) {
apply_or_insert: |entity, reflected_component| {
if let Some(mut component) = entity.get_mut::<C>() {
component.apply(reflected_component);
} else {
let mut component = C::from_world(world);
let mut component = entity.world_scope(|world| C::from_world(world));
component.apply(reflected_component);
world.entity_mut(entity).insert(component);
entity.insert(component);
}
},
remove: |entity| {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_scene/src/dynamic_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl DynamicScene {
let entity = *entity_map
.entry(bevy_ecs::entity::Entity::from_raw(scene_entity.entity))
.or_insert_with(|| world.spawn_empty().id());
let entity_mut = &mut world.entity_mut(entity);

// Apply/ add each component to the given entity.
for component in &scene_entity.components {
Expand All @@ -89,7 +90,7 @@ impl DynamicScene {
// If the entity already has the given component attached,
// just apply the (possibly) new value, otherwise add the
// component to the entity.
reflect_component.apply_or_insert(world, entity, &**component);
reflect_component.apply_or_insert(entity_mut, &**component);
}
}

Expand Down

0 comments on commit f586377

Please sign in to comment.