diff --git a/crates/bevy_ecs/src/system/commands.rs b/crates/bevy_ecs/src/system/commands.rs index 9e2d0c31ca0fa..520f11e09a82d 100644 --- a/crates/bevy_ecs/src/system/commands.rs +++ b/crates/bevy_ecs/src/system/commands.rs @@ -26,16 +26,20 @@ impl CommandQueue { } #[inline] - pub fn push(&mut self, command: Box) { + pub fn push_boxed(&mut self, command: Box) { self.commands.push(command); } + + #[inline] + pub fn push(&mut self, command: T) { + self.push_boxed(Box::new(command)); + } } /// A list of commands that will be run to modify a `World` pub struct Commands<'a> { queue: &'a mut CommandQueue, entities: &'a Entities, - current_entity: Option, } impl<'a> Commands<'a> { @@ -43,7 +47,6 @@ impl<'a> Commands<'a> { Self { queue, entities: world.entities(), - current_entity: None, } } @@ -84,84 +87,74 @@ impl<'a> Commands<'a> { /// } /// # example_system.system(); /// ``` - pub fn spawn(&mut self, bundle: impl Bundle) -> &mut Self { + pub fn spawn(&mut self) -> EntityCommands<'a, '_> { let entity = self.entities.reserve_entity(); - self.set_current_entity(entity); - self.insert_bundle(entity, bundle); - self + EntityCommands { + entity, + commands: self, + } + } + + pub fn spawn_bundle<'b, T: Bundle>(&'b mut self, bundle: T) -> EntityCommands<'a, 'b> { + let mut e = self.spawn(); + e.insert_bundle(bundle); + e + } + + pub fn entity(&mut self, entity: Entity) -> EntityCommands<'a, '_> { + EntityCommands { + entity, + commands: self, + } } /// Equivalent to iterating `bundles_iter` and calling [`Self::spawn`] on each bundle, but /// slightly more performant. - pub fn spawn_batch(&mut self, bundles_iter: I) -> &mut Self + pub fn spawn_batch(&mut self, bundles_iter: I) where I: IntoIterator + Send + Sync + 'static, I::Item: Bundle, { - self.add_command(SpawnBatch { bundles_iter }) - } - - /// Despawns only the specified entity, not including its children. - pub fn despawn(&mut self, entity: Entity) -> &mut Self { - self.add_command(Despawn { entity }) - } - - /// Inserts a bundle of components into `entity`. - /// - /// See [crate::world::EntityMut::insert_bundle]. - pub fn insert_bundle(&mut self, entity: Entity, bundle: impl Bundle) -> &mut Self { - self.add_command(InsertBundle { entity, bundle }) + self.queue.push(SpawnBatch { bundles_iter }); } - /// Inserts a single component into `entity`. - /// - /// See [crate::world::EntityMut::insert]. - pub fn insert(&mut self, entity: Entity, component: impl Component) -> &mut Self { - self.add_command(Insert { entity, component }) + /// See [World::insert_resource]. + pub fn insert_resource(&mut self, resource: T) { + self.queue.push(InsertResource { resource }) } - /// See [crate::world::EntityMut::remove]. - pub fn remove(&mut self, entity: Entity) -> &mut Self - where - T: Component, - { - self.add_command(Remove:: { - entity, + pub fn remove_resource(&mut self) { + self.queue.push(RemoveResource:: { phantom: PhantomData, - }) + }); } - /// See [World::insert_resource]. - pub fn insert_resource(&mut self, resource: T) -> &mut Self { - self.add_command(InsertResource { resource }) + /// Adds a command directly to the command list. Prefer this to [`Self::add_command_boxed`] if + /// the type of `command` is statically known. + pub fn add(&mut self, command: C) { + self.queue.push(command); } +} - /// See [crate::world::EntityMut::remove_bundle]. - pub fn remove_bundle(&mut self, entity: Entity) -> &mut Self - where - T: Bundle, - { - self.add_command(RemoveBundle:: { - entity, - phantom: PhantomData, - }) - } +pub struct EntityCommands<'a, 'b> { + entity: Entity, + commands: &'b mut Commands<'a>, +} - pub fn remove_resource(&mut self) -> &mut Self { - self.add_command(RemoveResource:: { - phantom: PhantomData, - }) +impl<'a, 'b> EntityCommands<'a, 'b> { + #[inline] + pub fn id(&self) -> Entity { + self.entity } /// Adds a bundle of components to the current entity. /// /// See [`Self::with`], [`Self::current_entity`]. - pub fn with_bundle(&mut self, bundle: impl Bundle) -> &mut Self { - let current_entity = self.current_entity.expect("Cannot add bundle because the 'current entity' is not set. You should spawn an entity first."); - self.queue.push(Box::new(InsertBundle { - entity: current_entity, + pub fn insert_bundle(&mut self, bundle: impl Bundle) -> &mut Self { + self.commands.add(InsertBundle { + entity: self.entity, bundle, - })); + }); self } @@ -205,47 +198,47 @@ impl<'a> Commands<'a> { /// } /// # example_system.system(); /// ``` - pub fn with(&mut self, component: impl Component) -> &mut Self { - let current_entity = self.current_entity.expect("Cannot add component because the 'current entity' is not set. You should spawn an entity first."); - self.queue.push(Box::new(Insert { - entity: current_entity, + pub fn insert(&mut self, component: impl Component) -> &mut Self { + self.commands.add(Insert { + entity: self.entity, component, - })); + }); self } - /// Adds a command directly to the command list. Prefer this to [`Self::add_command_boxed`] if - /// the type of `command` is statically known. - pub fn add_command(&mut self, command: C) -> &mut Self { - self.queue.push(Box::new(command)); + /// See [crate::world::EntityMut::remove_bundle]. + pub fn remove_bundle(&mut self) -> &mut Self + where + T: Bundle, + { + self.commands.add(RemoveBundle:: { + entity: self.entity, + phantom: PhantomData, + }); self } - /// See [`Self::add_command`]. - pub fn add_command_boxed(&mut self, command: Box) -> &mut Self { - self.queue.push(command); + /// See [crate::world::EntityMut::remove]. + pub fn remove(&mut self) -> &mut Self + where + T: Component, + { + self.commands.add(Remove:: { + entity: self.entity, + phantom: PhantomData, + }); self } - /// Returns the current entity, set by [`Self::spawn`] or with [`Self::set_current_entity`]. - pub fn current_entity(&self) -> Option { - self.current_entity - } - - pub fn set_current_entity(&mut self, entity: Entity) { - self.current_entity = Some(entity); - } - - pub fn clear_current_entity(&mut self) { - self.current_entity = None; + /// Despawns only the specified entity, not including its children. + pub fn despawn(&mut self) { + self.commands.add(Despawn { + entity: self.entity, + }) } - pub fn for_current_entity(&mut self, f: impl FnOnce(Entity)) -> &mut Self { - let current_entity = self - .current_entity - .expect("The 'current entity' is not set. You should spawn an entity first."); - f(current_entity); - self + pub fn commands(&mut self) -> &mut Commands<'a> { + self.commands } } @@ -392,9 +385,8 @@ mod tests { let mut world = World::default(); let mut command_queue = CommandQueue::default(); let entity = Commands::new(&mut command_queue, &world) - .spawn((1u32, 2u64)) - .current_entity() - .unwrap(); + .spawn_bundle((1u32, 2u64)) + .id(); command_queue.apply(&mut world); assert!(world.entities().len() == 1); let results = world @@ -404,9 +396,11 @@ mod tests { .collect::>(); assert_eq!(results, vec![(1u32, 2u64)]); // test entity despawn - Commands::new(&mut command_queue, &world) - .despawn(entity) - .despawn(entity); // double despawn shouldn't panic + { + let mut commands = Commands::new(&mut command_queue, &world); + commands.entity(entity).despawn(); + commands.entity(entity).despawn(); // double despawn shouldn't panic + } command_queue.apply(&mut world); let results2 = world .query::<(&u32, &u64)>() @@ -421,9 +415,9 @@ mod tests { let mut world = World::default(); let mut command_queue = CommandQueue::default(); let entity = Commands::new(&mut command_queue, &world) - .spawn((1u32, 2u64)) - .current_entity() - .unwrap(); + .spawn() + .insert_bundle((1u32, 2u64)) + .id(); command_queue.apply(&mut world); let results_before = world .query::<(&u32, &u64)>() @@ -434,8 +428,9 @@ mod tests { // test component removal Commands::new(&mut command_queue, &world) - .remove::(entity) - .remove_bundle::<(u32, u64)>(entity); + .entity(entity) + .remove::() + .remove_bundle::<(u32, u64)>(); command_queue.apply(&mut world); let results_after = world .query::<(&u32, &u64)>() diff --git a/crates/bevy_ecs/src/system/exclusive_system.rs b/crates/bevy_ecs/src/system/exclusive_system.rs index 2196c2665ec08..607853294c5f7 100644 --- a/crates/bevy_ecs/src/system/exclusive_system.rs +++ b/crates/bevy_ecs/src/system/exclusive_system.rs @@ -131,7 +131,7 @@ mod tests { ) { for entity in query.iter() { *counter += 1; - commands.remove::(entity); + commands.entity(entity).remove::(); } } diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index dec120bc5bdfe..b36a607e8beb3 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -293,18 +293,18 @@ fn load_node( ) -> Result<(), GltfError> { let transform = gltf_node.transform(); let mut gltf_error = None; - let node = world_builder.spawn(( + let mut node = world_builder.spawn_bundle(( Transform::from_matrix(Mat4::from_cols_array_2d(&transform.matrix())), GlobalTransform::identity(), )); if let Some(name) = gltf_node.name() { - node.with(Name::new(name.to_string())); + node.insert(Name::new(name.to_string())); } // create camera node if let Some(camera) = gltf_node.camera() { - node.with(VisibleEntities { + node.insert(VisibleEntities { ..Default::default() }); @@ -322,12 +322,12 @@ fn load_node( ..Default::default() }; - node.with(Camera { + node.insert(Camera { name: Some(base::camera::CAMERA_2D.to_owned()), projection_matrix: orthographic_projection.get_projection_matrix(), ..Default::default() }); - node.with(orthographic_projection); + node.insert(orthographic_projection); } gltf::camera::Projection::Perspective(perspective) => { let mut perspective_projection: PerspectiveProjection = PerspectiveProjection { @@ -341,12 +341,12 @@ fn load_node( if let Some(aspect_ratio) = perspective.aspect_ratio() { perspective_projection.aspect_ratio = aspect_ratio; } - node.with(Camera { + node.insert(Camera { name: Some(base::camera::CAMERA_3D.to_owned()), projection_matrix: perspective_projection.get_projection_matrix(), ..Default::default() }); - node.with(perspective_projection); + node.insert(perspective_projection); } } } @@ -371,7 +371,7 @@ fn load_node( let material_asset_path = AssetPath::new_ref(load_context.path(), Some(&material_label)); - parent.spawn(PbrBundle { + parent.spawn_bundle(PbrBundle { mesh: load_context.get_handle(mesh_asset_path), material: load_context.get_handle(material_asset_path), ..Default::default() diff --git a/crates/bevy_scene/src/command.rs b/crates/bevy_scene/src/command.rs index e92dcfd5145bb..f7d04c60652c4 100644 --- a/crates/bevy_scene/src/command.rs +++ b/crates/bevy_scene/src/command.rs @@ -20,12 +20,12 @@ impl Command for SpawnScene { } pub trait SpawnSceneCommands { - fn spawn_scene(&mut self, scene: Handle) -> &mut Self; + fn spawn_scene(&mut self, scene: Handle); } impl<'a> SpawnSceneCommands for Commands<'a> { - fn spawn_scene(&mut self, scene_handle: Handle) -> &mut Self { - self.add_command(SpawnScene { scene_handle }) + fn spawn_scene(&mut self, scene_handle: Handle) { + self.add(SpawnScene { scene_handle }); } } diff --git a/crates/bevy_transform/src/hierarchy/child_builder.rs b/crates/bevy_transform/src/hierarchy/child_builder.rs index 66e042d338eb4..b2321e4af5bab 100644 --- a/crates/bevy_transform/src/hierarchy/child_builder.rs +++ b/crates/bevy_transform/src/hierarchy/child_builder.rs @@ -1,9 +1,8 @@ use crate::prelude::{Children, Parent, PreviousParent}; use bevy_ecs::{ bundle::Bundle, - component::Component, entity::Entity, - system::{Command, Commands}, + system::{Command, Commands, EntityCommands}, world::{EntityMut, World}, }; use smallvec::SmallVec; @@ -47,7 +46,7 @@ pub struct PushChildren { } pub struct ChildBuilder<'a, 'b> { - commands: &'a mut Commands<'b>, + commands: &'b mut Commands<'a>, push_children: PushChildren, } @@ -77,124 +76,65 @@ impl Command for PushChildren { } impl<'a, 'b> ChildBuilder<'a, 'b> { - pub fn spawn(&mut self, bundle: impl Bundle) -> &mut Self { - self.commands.spawn(bundle); - self.push_children - .children - .push(self.commands.current_entity().unwrap()); - self + pub fn spawn_bundle(&mut self, bundle: impl Bundle) -> EntityCommands<'a, '_> { + let e = self.commands.spawn_bundle(bundle); + self.push_children.children.push(e.id()); + e } - pub fn current_entity(&self) -> Option { - self.commands.current_entity() + pub fn spawn(&mut self) -> EntityCommands<'a, '_> { + let e = self.commands.spawn(); + self.push_children.children.push(e.id()); + e } pub fn parent_entity(&self) -> Entity { self.push_children.parent } - pub fn with_bundle(&mut self, bundle: impl Bundle) -> &mut Self { - self.commands.with_bundle(bundle); - self - } - - pub fn with(&mut self, component: impl Component) -> &mut Self { - self.commands.with(component); - self - } - - pub fn for_current_entity(&mut self, func: impl FnOnce(Entity)) -> &mut Self { - let current_entity = self - .commands - .current_entity() - .expect("The 'current entity' is not set. You should spawn an entity first."); - func(current_entity); - self - } - pub fn add_command(&mut self, command: C) -> &mut Self { - self.commands.add_command(command); + self.commands.add(command); self } } pub trait BuildChildren { fn with_children(&mut self, f: impl FnOnce(&mut ChildBuilder)) -> &mut Self; - fn push_children(&mut self, parent: Entity, children: &[Entity]) -> &mut Self; - fn insert_children(&mut self, parent: Entity, index: usize, children: &[Entity]) -> &mut Self; -} - -impl<'a> BuildChildren for Commands<'a> { - fn with_children(&mut self, parent: impl FnOnce(&mut ChildBuilder)) -> &mut Self { - let current_entity = self.current_entity().expect("Cannot add children because the 'current entity' is not set. You should spawn an entity first."); - self.clear_current_entity(); - let push_children = { - let mut builder = ChildBuilder { - commands: self, - push_children: PushChildren { - children: SmallVec::default(), - parent: current_entity, - }, - }; - parent(&mut builder); - builder.push_children - }; - - self.set_current_entity(current_entity); - self.add_command(push_children); - self - } - - fn push_children(&mut self, parent: Entity, children: &[Entity]) -> &mut Self { - self.add_command(PushChildren { - children: SmallVec::from(children), - parent, - }); - self - } - - fn insert_children(&mut self, parent: Entity, index: usize, children: &[Entity]) -> &mut Self { - self.add_command(InsertChildren { - children: SmallVec::from(children), - index, - parent, - }); - self - } + fn push_children(&mut self, children: &[Entity]) -> &mut Self; + fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self; } -impl<'a, 'b> BuildChildren for ChildBuilder<'a, 'b> { +impl<'a, 'b> BuildChildren for EntityCommands<'a, 'b> { fn with_children(&mut self, spawn_children: impl FnOnce(&mut ChildBuilder)) -> &mut Self { - let current_entity = self.commands.current_entity().expect("Cannot add children because the 'current entity' is not set. You should spawn an entity first."); - self.commands.clear_current_entity(); + let parent = self.id(); let push_children = { let mut builder = ChildBuilder { - commands: self.commands, + commands: self.commands(), push_children: PushChildren { children: SmallVec::default(), - parent: current_entity, + parent, }, }; - spawn_children(&mut builder); builder.push_children }; - self.commands.set_current_entity(current_entity); - self.commands.add_command(push_children); + self.commands().add(push_children); self } - fn push_children(&mut self, parent: Entity, children: &[Entity]) -> &mut Self { - self.commands.add_command(PushChildren { + fn push_children(&mut self, children: &[Entity]) -> &mut Self { + let parent = self.id(); + self.commands().add(PushChildren { children: SmallVec::from(children), parent, }); self } - fn insert_children(&mut self, parent: Entity, index: usize, children: &[Entity]) -> &mut Self { - self.commands.add_command(InsertChildren { + fn insert_children(&mut self, index: usize, children: &[Entity]) -> &mut Self { + let parent = self.id(); + self.commands().add(InsertChildren { children: SmallVec::from(children), index, parent, @@ -211,12 +151,8 @@ pub struct WorldChildBuilder<'w> { } impl<'w> WorldChildBuilder<'w> { - pub fn spawn(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> &mut Self { - let parent_entity = self - .parent_entities - .last() - .cloned() - .expect("There should always be a parent at this point."); + pub fn spawn_bundle(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> EntityMut<'_> { + let parent_entity = self.parent_entity(); let entity = self .world .spawn() @@ -231,33 +167,32 @@ impl<'w> WorldChildBuilder<'w> { parent.insert(Children(smallvec::smallvec![entity])); } } - self - } - - pub fn with_bundle(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> &mut Self { - self.world - .entity_mut(self.current_entity.unwrap()) - .insert_bundle(bundle); - self - } - - pub fn with(&mut self, component: impl Component) -> &mut Self { - self.world - .entity_mut(self.current_entity.unwrap()) - .insert(component); - self + self.world.entity_mut(entity) } - pub fn current_entity(&self) -> Option { - self.current_entity + pub fn spawn(&mut self) -> EntityMut<'_> { + let parent_entity = self.parent_entity(); + let entity = self + .world + .spawn() + .insert_bundle((Parent(parent_entity), PreviousParent(parent_entity))) + .id(); + self.current_entity = Some(entity); + if let Some(mut parent) = self.world.get_entity_mut(parent_entity) { + if let Some(mut children) = parent.get_mut::() { + children.0.push(entity); + } else { + parent.insert(Children(smallvec::smallvec![entity])); + } + } + self.world.entity_mut(entity) } - pub fn for_current_entity(&mut self, func: impl FnOnce(Entity)) -> &mut Self { - let current_entity = self - .current_entity() - .expect("The 'current entity' is not set. You should spawn an entity first."); - func(current_entity); - self + pub fn parent_entity(&self) -> Entity { + self.parent_entities + .last() + .cloned() + .expect("There should always be a parent at this point.") } } @@ -319,45 +254,28 @@ mod tests { let mut queue = CommandQueue::default(); let mut commands = Commands::new(&mut queue, &world); - let mut parent = None; - let mut child1 = None; - let mut child2 = None; - let mut child3 = None; - - commands - .spawn((1,)) - .for_current_entity(|e| parent = Some(e)) - .with_children(|parent| { - parent - .spawn((2,)) - .for_current_entity(|e| child1 = Some(e)) - .spawn((3,)) - .for_current_entity(|e| child2 = Some(e)) - .spawn((4,)); - - child3 = parent.current_entity(); - }); + let mut children = Vec::new(); + let parent = commands.spawn().insert(1).id(); + commands.entity(parent).with_children(|parent| { + children.push(parent.spawn().insert(2).id()); + children.push(parent.spawn().insert(3).id()); + children.push(parent.spawn().insert(4).id()); + }); queue.apply(&mut world); - let parent = parent.expect("parent should exist"); - let child1 = child1.expect("child1 should exist"); - let child2 = child2.expect("child2 should exist"); - let child3 = child3.expect("child3 should exist"); - let expected_children: SmallVec<[Entity; 8]> = smallvec![child1, child2, child3]; - assert_eq!( - world.get::(parent).unwrap().0.clone(), - expected_children + world.get::(parent).unwrap().0.as_slice(), + children.as_slice(), ); - assert_eq!(*world.get::(child1).unwrap(), Parent(parent)); - assert_eq!(*world.get::(child2).unwrap(), Parent(parent)); + assert_eq!(*world.get::(children[0]).unwrap(), Parent(parent)); + assert_eq!(*world.get::(children[1]).unwrap(), Parent(parent)); assert_eq!( - *world.get::(child1).unwrap(), + *world.get::(children[0]).unwrap(), PreviousParent(parent) ); assert_eq!( - *world.get::(child2).unwrap(), + *world.get::(children[1]).unwrap(), PreviousParent(parent) ); } @@ -373,7 +291,7 @@ mod tests { let mut queue = CommandQueue::default(); { let mut commands = Commands::new(&mut queue, &world); - commands.push_children(entities[0], &entities[1..3]); + commands.entity(entities[0]).push_children(&entities[1..3]); } queue.apply(&mut world); @@ -402,7 +320,7 @@ mod tests { { let mut commands = Commands::new(&mut queue, &world); - commands.insert_children(parent, 1, &entities[3..]); + commands.entity(parent).insert_children(1, &entities[3..]); } queue.apply(&mut world); diff --git a/crates/bevy_transform/src/hierarchy/hierarchy.rs b/crates/bevy_transform/src/hierarchy/hierarchy.rs index 455a88f202ac6..6eea7757eafaf 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy.rs @@ -44,13 +44,13 @@ impl Command for DespawnRecursive { pub trait DespawnRecursiveExt { /// Despawns the provided entity and its children. - fn despawn_recursive(&mut self, entity: Entity) -> &mut Self; + fn despawn_recursive(&mut self, entity: Entity); } impl<'a> DespawnRecursiveExt for Commands<'a> { /// Despawns the provided entity and its children. - fn despawn_recursive(&mut self, entity: Entity) -> &mut Self { - self.add_command(DespawnRecursive { entity }) + fn despawn_recursive(&mut self, entity: Entity) { + self.add(DespawnRecursive { entity }); } } @@ -73,31 +73,33 @@ mod tests { let mut commands = Commands::new(&mut queue, &world); commands - .spawn(("Another parent".to_owned(), 0u32)) + .spawn_bundle(("Another parent".to_owned(), 0u32)) .with_children(|parent| { - parent.spawn(("Another child".to_owned(), 1u32)); + parent.spawn_bundle(("Another child".to_owned(), 1u32)); }); // Create a grandparent entity which will _not_ be deleted - commands.spawn(("Grandparent".to_owned(), 2u32)); - grandparent_entity = commands.current_entity().unwrap(); - - commands.with_children(|parent| { + grandparent_entity = commands.spawn_bundle(("Grandparent".to_owned(), 2u32)).id(); + commands.entity(grandparent_entity).with_children(|parent| { // Add a child to the grandparent (the "parent"), which will get deleted - parent.spawn(("Parent, to be deleted".to_owned(), 3u32)); - // All descendents of the "parent" should also be deleted. - parent.with_children(|parent| { - parent - .spawn(("First Child, to be deleted".to_owned(), 4u32)) - .with_children(|parent| { - // child - parent.spawn(("First grand child, to be deleted".to_owned(), 5u32)); - }); - parent.spawn(("Second child, to be deleted".to_owned(), 6u32)); - }); + parent + .spawn_bundle(("Parent, to be deleted".to_owned(), 3u32)) + // All descendents of the "parent" should also be deleted. + .with_children(|parent| { + parent + .spawn_bundle(("First Child, to be deleted".to_owned(), 4u32)) + .with_children(|parent| { + // child + parent.spawn_bundle(( + "First grand child, to be deleted".to_owned(), + 5u32, + )); + }); + parent.spawn_bundle(("Second child, to be deleted".to_owned(), 6u32)); + }); }); - commands.spawn(("An innocent bystander".to_owned(), 7u32)); + commands.spawn_bundle(("An innocent bystander".to_owned(), 7u32)); } queue.apply(&mut world); diff --git a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs index 62ddec77b3388..541b10744b0ee 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs @@ -20,7 +20,7 @@ pub fn parent_update_system( for (entity, previous_parent) in removed_parent_query.iter() { if let Ok(mut previous_parent_children) = children_query.get_mut(previous_parent.0) { previous_parent_children.0.retain(|e| *e != entity); - commands.remove::(entity); + commands.entity(entity).remove::(); } } @@ -43,7 +43,7 @@ pub fn parent_update_system( // Set `PreviousParent = Parent`. *previous_parent = PreviousParent(parent.0); } else { - commands.insert(entity, PreviousParent(parent.0)); + commands.entity(entity).insert(PreviousParent(parent.0)); }; // Add to the parent's `Children` (either the real component, or @@ -67,8 +67,8 @@ pub fn parent_update_system( // 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)); + children_additions.iter().for_each(|(e, v)| { + commands.entity(*e).insert(Children::with(v)); }); } #[cfg(test)] @@ -96,19 +96,25 @@ mod test { // Add parent entities let mut command_queue = CommandQueue::default(); let mut commands = Commands::new(&mut command_queue, &world); - let mut parent = None; let mut children = Vec::new(); - commands - .spawn((Transform::from_xyz(1.0, 0.0, 0.0),)) - .for_current_entity(|entity| parent = Some(entity)) - .with_children(|parent| { + let parent = commands + .spawn() + .insert(Transform::from_xyz(1.0, 0.0, 0.0)) + .id(); + commands.entity(parent).with_children(|parent| { + children.push( parent - .spawn((Transform::from_xyz(0.0, 2.0, 0.0),)) - .for_current_entity(|entity| children.push(entity)) - .spawn((Transform::from_xyz(0.0, 0.0, 3.0),)) - .for_current_entity(|entity| children.push(entity)); - }); - let parent = parent.unwrap(); + .spawn() + .insert(Transform::from_xyz(0.0, 2.0, 0.0)) + .id(), + ); + children.push( + parent + .spawn() + .insert(Transform::from_xyz(0.0, 3.0, 0.0)) + .id(), + ); + }); command_queue.apply(&mut world); schedule.run(&mut world); diff --git a/crates/bevy_transform/src/transform_propagate_system.rs b/crates/bevy_transform/src/transform_propagate_system.rs index feb59d5012ef8..0c74d2579ce21 100644 --- a/crates/bevy_transform/src/transform_propagate_system.rs +++ b/crates/bevy_transform/src/transform_propagate_system.rs @@ -109,17 +109,22 @@ mod test { GlobalTransform::identity(), )) .with_children(|parent| { - parent - .spawn(( - Transform::from_xyz(0.0, 2.0, 0.), - GlobalTransform::identity(), - )) - .for_current_entity(|entity| children.push(entity)) - .spawn(( - Transform::from_xyz(0.0, 0.0, 3.), - GlobalTransform::identity(), - )) - .for_current_entity(|entity| children.push(entity)); + children.push( + parent + .spawn_bundle(( + Transform::from_xyz(0.0, 2.0, 0.), + GlobalTransform::identity(), + )) + .id(), + ); + children.push( + parent + .spawn_bundle(( + Transform::from_xyz(0.0, 0.0, 3.), + GlobalTransform::identity(), + )) + .id(), + ); }); schedule.run(&mut world); @@ -150,22 +155,27 @@ mod test { let mut commands = Commands::new(&mut queue, &world); let mut children = Vec::new(); commands - .spawn(( + .spawn_bundle(( Transform::from_xyz(1.0, 0.0, 0.0), GlobalTransform::identity(), )) .with_children(|parent| { - parent - .spawn(( - Transform::from_xyz(0.0, 2.0, 0.0), - GlobalTransform::identity(), - )) - .for_current_entity(|entity| children.push(entity)) - .spawn(( - Transform::from_xyz(0.0, 0.0, 3.0), - GlobalTransform::identity(), - )) - .for_current_entity(|entity| children.push(entity)); + children.push( + parent + .spawn_bundle(( + Transform::from_xyz(0.0, 2.0, 0.0), + GlobalTransform::identity(), + )) + .id(), + ); + children.push( + parent + .spawn_bundle(( + Transform::from_xyz(0.0, 0.0, 3.0), + GlobalTransform::identity(), + )) + .id(), + ); }); queue.apply(&mut world); schedule.run(&mut world); diff --git a/crates/bevy_ui/src/update.rs b/crates/bevy_ui/src/update.rs index 09c91a095f0ad..fd3fe3690e99c 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -80,42 +80,42 @@ mod tests { let mut world = World::default(); let mut queue = CommandQueue::default(); let mut commands = Commands::new(&mut queue, &world); - commands.spawn(node_with_transform("0")); + commands.spawn_bundle(node_with_transform("0")); commands - .spawn(node_with_transform("1")) + .spawn_bundle(node_with_transform("1")) .with_children(|parent| { parent - .spawn(node_with_transform("1-0")) + .spawn_bundle(node_with_transform("1-0")) .with_children(|parent| { - parent.spawn(node_with_transform("1-0-0")); - parent.spawn(node_without_transform("1-0-1")); - parent.spawn(node_with_transform("1-0-2")); + parent.spawn_bundle(node_with_transform("1-0-0")); + parent.spawn_bundle(node_without_transform("1-0-1")); + parent.spawn_bundle(node_with_transform("1-0-2")); }); - parent.spawn(node_with_transform("1-1")); + parent.spawn_bundle(node_with_transform("1-1")); parent - .spawn(node_without_transform("1-2")) + .spawn_bundle(node_without_transform("1-2")) .with_children(|parent| { - parent.spawn(node_with_transform("1-2-0")); - parent.spawn(node_with_transform("1-2-1")); + parent.spawn_bundle(node_with_transform("1-2-0")); + parent.spawn_bundle(node_with_transform("1-2-1")); parent - .spawn(node_with_transform("1-2-2")) + .spawn_bundle(node_with_transform("1-2-2")) .with_children(|_| ()); - parent.spawn(node_with_transform("1-2-3")); + parent.spawn_bundle(node_with_transform("1-2-3")); }); - parent.spawn(node_with_transform("1-3")); + parent.spawn_bundle(node_with_transform("1-3")); }); commands - .spawn(node_without_transform("2")) + .spawn_bundle(node_without_transform("2")) .with_children(|parent| { parent - .spawn(node_with_transform("2-0")) + .spawn_bundle(node_with_transform("2-0")) .with_children(|_parent| ()); parent - .spawn(node_with_transform("2-1")) + .spawn_bundle(node_with_transform("2-1")) .with_children(|parent| { - parent.spawn(node_with_transform("2-1-0")); + parent.spawn_bundle(node_with_transform("2-1-0")); }); }); queue.apply(&mut world); diff --git a/examples/2d/contributors.rs b/examples/2d/contributors.rs index 2ba11c06c452d..1e5c0c7edab1e 100644 --- a/examples/2d/contributors.rs +++ b/examples/2d/contributors.rs @@ -57,9 +57,8 @@ fn setup( let texture_handle = asset_server.load("branding/icon.png"); - commands - .spawn(OrthographicCameraBundle::new_2d()) - .spawn(UiCameraBundle::default()); + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + commands.spawn_bundle(UiCameraBundle::default()); let mut sel = ContributorSelection { order: vec![], @@ -79,13 +78,15 @@ fn setup( let transform = Transform::from_xyz(pos.0, pos.1, 0.0); - commands - .spawn((Contributor { hue },)) - .with(Velocity { - translation: velocity, - rotation: -dir * 5.0, - }) - .with_bundle(SpriteBundle { + let e = commands + .spawn_bundle(( + Contributor { hue }, + Velocity { + translation: velocity, + rotation: -dir * 5.0, + }, + )) + .insert_bundle(SpriteBundle { sprite: Sprite { size: Vec2::new(1.0, 1.0) * SPRITE_SIZE, resize_mode: SpriteResizeMode::Manual, @@ -98,20 +99,20 @@ fn setup( }), transform, ..Default::default() - }); - - let e = commands.current_entity().unwrap(); + }) + .id(); sel.order.push((name, e)); } sel.order.shuffle(&mut rnd); - commands.spawn((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true))); + commands.spawn_bundle((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true))); commands - .spawn((ContributorDisplay,)) - .with_bundle(TextBundle { + .spawn() + .insert(ContributorDisplay) + .insert_bundle(TextBundle { style: Style { align_self: AlignSelf::FlexEnd, ..Default::default() diff --git a/examples/2d/sprite.rs b/examples/2d/sprite.rs index c2bbe4ebfe4a1..4767e6b903b4d 100644 --- a/examples/2d/sprite.rs +++ b/examples/2d/sprite.rs @@ -13,10 +13,9 @@ fn setup( mut materials: ResMut>, ) { let texture_handle = asset_server.load("branding/icon.png"); - commands - .spawn(OrthographicCameraBundle::new_2d()) - .spawn(SpriteBundle { - material: materials.add(texture_handle.into()), - ..Default::default() - }); + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + commands.spawn_bundle(SpriteBundle { + material: materials.add(texture_handle.into()), + ..Default::default() + }); } diff --git a/examples/2d/sprite_flipping.rs b/examples/2d/sprite_flipping.rs index 6db1c759f1164..59403cc46b3eb 100644 --- a/examples/2d/sprite_flipping.rs +++ b/examples/2d/sprite_flipping.rs @@ -13,17 +13,16 @@ fn setup( mut materials: ResMut>, ) { let texture_handle = asset_server.load("branding/icon.png"); - commands - .spawn(OrthographicCameraBundle::new_2d()) - .spawn(SpriteBundle { - material: materials.add(texture_handle.into()), - sprite: Sprite { - // Flip the logo to the left - flip_x: true, - // And don't flip it upside-down ( the default ) - flip_y: false, - ..Default::default() - }, + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + commands.spawn_bundle(SpriteBundle { + material: materials.add(texture_handle.into()), + sprite: Sprite { + // Flip the logo to the left + flip_x: true, + // And don't flip it upside-down ( the default ) + flip_y: false, ..Default::default() - }); + }, + ..Default::default() + }); } diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index 622c98f7fedbb..a766087cc5364 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -30,12 +30,12 @@ fn setup( let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png"); let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1); let texture_atlas_handle = texture_atlases.add(texture_atlas); + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands - .spawn(OrthographicCameraBundle::new_2d()) - .spawn(SpriteSheetBundle { + .spawn_bundle(SpriteSheetBundle { texture_atlas: texture_atlas_handle, transform: Transform::from_scale(Vec3::splat(6.0)), ..Default::default() }) - .with(Timer::from_seconds(0.1, true)); + .insert(Timer::from_seconds(0.1, true)); } diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index 22c9f156e7361..a8cbf3c38a2c9 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -9,24 +9,23 @@ fn main() { } fn setup(mut commands: Commands, asset_server: Res) { - commands - // 2d camera - .spawn(OrthographicCameraBundle::new_2d()) - .spawn(Text2dBundle { - text: Text::with_section( - "This text is in the 2D scene.", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 60.0, - color: Color::WHITE, - }, - TextAlignment { - vertical: VerticalAlign::Center, - horizontal: HorizontalAlign::Center, - }, - ), - ..Default::default() - }); + // 2d camera + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + commands.spawn_bundle(Text2dBundle { + text: Text::with_section( + "This text is in the 2D scene.", + TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 60.0, + color: Color::WHITE, + }, + TextAlignment { + vertical: VerticalAlign::Center, + horizontal: HorizontalAlign::Center, + }, + ), + ..Default::default() + }); } fn animate(time: Res