Skip to content

Commit

Permalink
Add Command::remove_one
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDoneth committed Aug 13, 2020
1 parent d488d4e commit 589af3d
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions crates/bevy_ecs/src/system/commands.rs
Original file line number Diff line number Diff line change
@@ -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>),
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 589af3d

Please sign in to comment.