From 0b9fd0921d480caf41719c14be13cd3a0525c7cd Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Fri, 11 Feb 2022 11:28:42 -0600 Subject: [PATCH] Add `RunIfExists` command for only executing if a given entity exists. --- src/commands.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/commands.rs diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..129f4c6 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,22 @@ +use bevy::{ + ecs::{system::Command, world::EntityMut}, + prelude::*, +}; + +struct RunIfExistsCommand(Entity, F); + +impl Command for RunIfExistsCommand { + 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)); + } +}