diff --git a/.gitignore b/.gitignore index 48bbc7e0e1c91..aa82a29225742 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ Cargo.lock .cargo/config .cargo/config.toml /.idea +.iml /.vscode /benches/target diff --git a/crates/bevy_transform/src/hierarchy/child_builder.rs b/crates/bevy_transform/src/hierarchy/child_builder.rs index 66e042d338eb4..eb9e7584428d0 100644 --- a/crates/bevy_transform/src/hierarchy/child_builder.rs +++ b/crates/bevy_transform/src/hierarchy/child_builder.rs @@ -54,9 +54,13 @@ pub struct ChildBuilder<'a, 'b> { impl Command for PushChildren { fn write(self: Box, world: &mut World) { for child in self.children.iter() { + let previous = match world.get::(*child) { + Some(Parent(previous)) => *previous, + None => self.parent, + }; world .entity_mut(*child) - .insert_bundle((Parent(self.parent), PreviousParent(self.parent))); + .insert_bundle((Parent(self.parent), PreviousParent(previous))); } { let mut added = false; diff --git a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs index 62ddec77b3388..63bd6e384780f 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs @@ -4,8 +4,6 @@ use bevy_ecs::{ query::Without, system::{Commands, Query}, }; -use bevy_utils::HashMap; -use smallvec::SmallVec; pub fn parent_update_system( mut commands: Commands, @@ -24,9 +22,6 @@ pub fn parent_update_system( } } - // Tracks all newly created `Children` Components this frame. - let mut children_additions = HashMap::>::default(); - // Entities with a changed Parent (that also have a PreviousParent, even if None) for (entity, parent, possible_previous_parent) in parent_query.iter_mut() { if let Some(mut previous_parent) = possible_previous_parent { @@ -45,31 +40,7 @@ pub fn parent_update_system( } else { commands.insert(entity, PreviousParent(parent.0)); }; - - // Add to the parent's `Children` (either the real component, or - // `children_additions`). - if let Ok(mut new_parent_children) = children_query.get_mut(parent.0) { - // This is the parent - debug_assert!( - !(*new_parent_children).0.contains(&entity), - "children already added" - ); - (*new_parent_children).0.push(entity); - } else { - // The parent doesn't have a children entity, lets add it - children_additions - .entry(parent.0) - .or_insert_with(Default::default) - .push(entity); - } } - - // Flush the `children_additions` to the command buffer. It is stored separate to - // collect multiple new children that point to the same parent into the same - // SmallVec, and to prevent redundant add+remove operations. - children_additions.iter().for_each(|(k, v)| { - commands.insert(*k, Children::with(v)); - }); } #[cfg(test)] mod test { @@ -124,8 +95,10 @@ mod test { ); // Parent `e1` to `e2`. - (*world.get_mut::(children[0]).unwrap()).0 = children[1]; + let mut commands = Commands::new(&mut command_queue, &world); + commands.push_children(children[1], &[children[0]]); + command_queue.apply(&mut world); schedule.run(&mut world); assert_eq!(