Add remove_children
and remove_related
to EntityWorldMut
and EntityCommands
(#18835)
Fixes #18834. `EntityWorldMut::remove_children` and `EntityCommands::remove_children` were removed in the relationships overhaul (#17398) and never got replaced. I don't *think* this was intentional (the methods were never mentioned in the PR or its comments), but I could've missed something.
This commit is contained in:
parent
ea15c85977
commit
f006f02f78
@ -293,6 +293,11 @@ impl<'w> EntityWorldMut<'w> {
|
|||||||
self.add_related::<ChildOf>(&[child])
|
self.add_related::<ChildOf>(&[child])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes the relationship between this entity and the given entities.
|
||||||
|
pub fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
|
||||||
|
self.remove_related::<ChildOf>(children)
|
||||||
|
}
|
||||||
|
|
||||||
/// Replaces all the related children with a new set of children.
|
/// Replaces all the related children with a new set of children.
|
||||||
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
|
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
|
||||||
self.replace_related::<ChildOf>(children)
|
self.replace_related::<ChildOf>(children)
|
||||||
@ -375,6 +380,11 @@ impl<'a> EntityCommands<'a> {
|
|||||||
self.add_related::<ChildOf>(&[child])
|
self.add_related::<ChildOf>(&[child])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes the relationship between this entity and the given entities.
|
||||||
|
pub fn remove_children(&mut self, children: &[Entity]) -> &mut Self {
|
||||||
|
self.remove_related::<ChildOf>(children)
|
||||||
|
}
|
||||||
|
|
||||||
/// Replaces the children on this entity with a new list of children.
|
/// Replaces the children on this entity with a new list of children.
|
||||||
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
|
pub fn replace_children(&mut self, children: &[Entity]) -> &mut Self {
|
||||||
self.replace_related::<ChildOf>(children)
|
self.replace_related::<ChildOf>(children)
|
||||||
@ -646,6 +656,26 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn remove_children() {
|
||||||
|
let mut world = World::new();
|
||||||
|
let child1 = world.spawn_empty().id();
|
||||||
|
let child2 = world.spawn_empty().id();
|
||||||
|
let child3 = world.spawn_empty().id();
|
||||||
|
let child4 = world.spawn_empty().id();
|
||||||
|
|
||||||
|
let mut root = world.spawn_empty();
|
||||||
|
root.add_children(&[child1, child2, child3, child4]);
|
||||||
|
root.remove_children(&[child2, child3]);
|
||||||
|
let root = root.id();
|
||||||
|
|
||||||
|
let hierarchy = get_hierarchy(&world, root);
|
||||||
|
assert_eq!(
|
||||||
|
hierarchy,
|
||||||
|
Node::new_with(root, vec![Node::new(child1), Node::new(child4)])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn self_parenting_invalid() {
|
fn self_parenting_invalid() {
|
||||||
let mut world = World::new();
|
let mut world = World::new();
|
||||||
|
@ -105,6 +105,23 @@ impl<'w> EntityWorldMut<'w> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes the relation `R` between this entity and the given entities.
|
||||||
|
pub fn remove_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
|
||||||
|
let id = self.id();
|
||||||
|
self.world_scope(|world| {
|
||||||
|
for related in related {
|
||||||
|
if world
|
||||||
|
.get::<R>(*related)
|
||||||
|
.is_some_and(|relationship| relationship.get() == id)
|
||||||
|
{
|
||||||
|
world.entity_mut(*related).remove::<R>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Replaces all the related entities with a new set of entities.
|
/// Replaces all the related entities with a new set of entities.
|
||||||
pub fn replace_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
|
pub fn replace_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
|
||||||
type Collection<R> =
|
type Collection<R> =
|
||||||
@ -383,6 +400,15 @@ impl<'a> EntityCommands<'a> {
|
|||||||
self.add_related::<R>(&[entity])
|
self.add_related::<R>(&[entity])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes the relation `R` between this entity and the given entities.
|
||||||
|
pub fn remove_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
|
||||||
|
let related: Box<[Entity]> = related.into();
|
||||||
|
|
||||||
|
self.queue(move |mut entity: EntityWorldMut| {
|
||||||
|
entity.remove_related::<R>(&related);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Replaces all the related entities with the given set of new related entities.
|
/// Replaces all the related entities with the given set of new related entities.
|
||||||
pub fn replace_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
|
pub fn replace_related<R: Relationship>(&mut self, related: &[Entity]) -> &mut Self {
|
||||||
let related: Box<[Entity]> = related.into();
|
let related: Box<[Entity]> = related.into();
|
||||||
|
Loading…
Reference in New Issue
Block a user