mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
commit
938d381d45
1 changed files with 34 additions and 2 deletions
|
@ -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<dyn WorldWriter>),
|
||||
WriteResources(Box<dyn ResourcesWriter>),
|
||||
|
@ -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