Add Command::remove_one

This commit is contained in:
John Doneth 2020-08-13 19:38:38 -04:00
parent d488d4e308
commit 589af3dc51

View file

@ -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)]