mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
Add Command::remove_one
This commit is contained in:
parent
d488d4e308
commit
589af3dc51
1 changed files with 34 additions and 2 deletions
|
@ -1,7 +1,10 @@
|
|||
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]
|
||||
pub enum Command {
|
||||
|
@ -109,6 +112,25 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RemoveOne<T>
|
||||
where
|
||||
T: Component,
|
||||
{
|
||||
entity: Entity,
|
||||
phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> WorldWriter for RemoveOne<T>
|
||||
where
|
||||
T: Component,
|
||||
{
|
||||
fn write(self: Box<Self>, world: &mut World) {
|
||||
if world.get::<T>(self.entity).is_ok() {
|
||||
world.remove_one::<T>(self.entity).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ResourcesWriter: Send + Sync {
|
||||
fn write(self: Box<Self>, resources: &mut Resources);
|
||||
}
|
||||
|
@ -321,6 +343,16 @@ impl Commands {
|
|||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn remove_one<T>(&mut self, entity: Entity) -> &mut Self
|
||||
where
|
||||
T: Component,
|
||||
{
|
||||
self.write_world(RemoveOne::<T> {
|
||||
entity,
|
||||
phantom: PhantomData,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue