diff --git a/crates/bevy_ecs/src/system/commands.rs b/crates/bevy_ecs/src/system/commands.rs index 9086f83aa7..7191fb5f76 100644 --- a/crates/bevy_ecs/src/system/commands.rs +++ b/crates/bevy_ecs/src/system/commands.rs @@ -1,9 +1,12 @@ use super::SystemId; use crate::resource::{Resource, Resources}; use bevy_hecs::{Bundle, Component, DynamicBundle, Entity, World}; -use std::sync::{Arc, Mutex}; +use std::{ + marker::PhantomData, + sync::{Arc, Mutex}, +}; -/// A queued command to mutate the current [World] or [Resources] +/// A queued command to mutate the current [World] or [Resources] pub enum Command { WriteWorld(Box), WriteResources(Box), @@ -109,6 +112,25 @@ where } } +pub(crate) struct RemoveOne +where + T: Component, +{ + entity: Entity, + phantom: PhantomData, +} + +impl WorldWriter for RemoveOne +where + T: Component, +{ + fn write(self: Box, world: &mut World) { + if world.get::(self.entity).is_ok() { + world.remove_one::(self.entity).unwrap(); + } + } +} + pub trait ResourcesWriter: Send + Sync { fn write(self: Box, resources: &mut Resources); } @@ -321,6 +343,16 @@ impl Commands { } self } + + pub fn remove_one(&mut self, entity: Entity) -> &mut Self + where + T: Component, + { + self.write_world(RemoveOne:: { + entity, + phantom: PhantomData, + }) + } } #[cfg(test)]