Merge pull request #178 from JohnDoneth/master

Add Command::remove_one
This commit is contained in:
Carter Anderson 2020-08-13 17:26:22 -07:00 committed by GitHub
commit 938d381d45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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