Add RunIfExists command for only executing if a given entity exists.

This commit is contained in:
Nolan Darilek 2022-02-11 11:28:42 -06:00
parent 27baaceaea
commit 0b9fd0921d

22
src/commands.rs Normal file
View File

@ -0,0 +1,22 @@
use bevy::{
ecs::{system::Command, world::EntityMut},
prelude::*,
};
struct RunIfExistsCommand<F>(Entity, F);
impl<F: FnOnce(EntityMut) + Send + Sync + 'static> Command for RunIfExistsCommand<F> {
fn write(self, world: &mut World) {
world.get_entity_mut(self.0).map(self.1);
}
}
pub trait RunIfExistsExt {
fn run_if_exists(&mut self, entity: Entity, f: impl FnOnce(EntityMut) + Send + Sync + 'static);
}
impl RunIfExistsExt for Commands<'_, '_> {
fn run_if_exists(&mut self, entity: Entity, f: impl FnOnce(EntityMut) + Send + Sync + 'static) {
self.add(RunIfExistsCommand(entity, f));
}
}