Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add set_parent and remove_parent to EntityCommands #6189

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 39 additions & 2 deletions crates/bevy_hierarchy/src/child_builder.rs
Expand Up @@ -146,7 +146,7 @@ impl Command for InsertChildren {
}
}

/// Command that pushes children to the end of the entity's children
/// Command that pushes children to the end of the entity's [`Children`].
#[derive(Debug)]
pub struct PushChildren {
parent: Entity,
Expand All @@ -166,7 +166,7 @@ impl Command for PushChildren {
}
}

/// Command that removes children from an entity, and removes that child's parent and inserts it into the previous parent component
/// Command that removes children from an entity, and removes that child's parent.
pub struct RemoveChildren {
parent: Entity,
children: SmallVec<[Entity; 8]>,
Expand All @@ -178,6 +178,27 @@ impl Command for RemoveChildren {
}
}

/// Command that removes the parent of an entity, and removes that entity from the parent's [`Children`].
pub struct RemoveParent {
child: Entity,
}

impl Command for RemoveParent {
fn write(self, world: &mut World) {
if let Some(parent) = world.get::<Parent>(self.child) {
let parent_entity = parent.get();
remove_from_children(world, parent_entity, self.child);
world.entity_mut(self.child).remove::<Parent>();
if let Some(mut events) = world.get_resource_mut::<Events<_>>() {
events.send(HierarchyEvent::ChildRemoved {
child: self.child,
parent: parent_entity,
});
}
}
}
}

/// Struct for building children onto an entity
pub struct ChildBuilder<'w, 's, 'a> {
commands: &'a mut Commands<'w, 's>,
Expand Down Expand Up @@ -265,6 +286,10 @@ pub trait BuildChildren {
fn remove_children(&mut self, children: &[Entity]) -> &mut Self;
/// Adds a single child
fn add_child(&mut self, child: Entity) -> &mut Self;
/// Sets the parent of this entity.
fn set_parent(&mut self, parent: Entity) -> &mut Self;
/// Removes the parent of this entity.
fn remove_parent(&mut self) -> &mut Self;
}

impl<'w, 's, 'a> BuildChildren for EntityCommands<'w, 's, 'a> {
Expand Down Expand Up @@ -323,6 +348,18 @@ impl<'w, 's, 'a> BuildChildren for EntityCommands<'w, 's, 'a> {
self.commands().add(AddChild { child, parent });
self
}

fn set_parent(&mut self, parent: Entity) -> &mut Self {
let child = self.id();
self.commands().add(AddChild { child, parent });
self
}

fn remove_parent(&mut self) -> &mut Self {
let child = self.id();
self.commands().add(RemoveParent { child });
self
}
}

/// Struct for adding children to an entity directly through the [`World`] for use in exclusive systems
Expand Down