Remove commands hack.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2023-04-01 07:20:59 -05:00
parent cbad1648e8
commit 865bdc333c
9 changed files with 24 additions and 67 deletions

View File

@ -15,8 +15,6 @@ _Blackout_ uses a few other libraries for its magic:
It provides, including but not limited to: It provides, including but not limited to:
* commands
* `RunIfExists` extension for only running a given command if its entity exists
* core * core
* `PointLike` trait for distance and other calculations between `Coordinates` and `Coordinates`-like things (I.e. `(f32, f32)`, `(i32, i32)`, ...) * `PointLike` trait for distance and other calculations between `Coordinates` and `Coordinates`-like things (I.e. `(f32, f32)`, `(i32, i32)`, ...)
* `Angle` type for handling angles in degrees/radians * `Angle` type for handling angles in degrees/radians
@ -49,4 +47,4 @@ It provides, including but not limited to:
* utils * utils
* Poorly-named `target_and_other` function for taking 2 entities, a predecate, and returning `Some((match, other))` if one matches or `None` if neither do. Good for, say, taking in a collision pair and returning a bullet entity first * Poorly-named `target_and_other` function for taking 2 entities, a predecate, and returning `Some((match, other))` if one matches or `None` if neither do. Good for, say, taking in a collision pair and returning a bullet entity first
* error * error
* Logger for logging errors in systems * Logger for logging piped errors in systems

View File

@ -1,22 +0,0 @@
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));
}
}

View File

@ -6,7 +6,6 @@ pub use bevy;
pub use bevy_rapier2d; pub use bevy_rapier2d;
pub use bevy_synthizer; pub use bevy_synthizer;
pub use bevy_tts; pub use bevy_tts;
pub mod commands;
pub use coord_2d; pub use coord_2d;
#[macro_use] #[macro_use]
pub mod core; pub mod core;

View File

@ -6,7 +6,6 @@ use bevy_tts::Tts;
use leafwing_input_manager::{axislike::DualAxisData, prelude::*}; use leafwing_input_manager::{axislike::DualAxisData, prelude::*};
use crate::{ use crate::{
commands::RunIfExistsExt,
core::{Angle, Area, CardinalDirection, GlobalTransformExt, Player}, core::{Angle, Area, CardinalDirection, GlobalTransformExt, Player},
error::error_handler, error::error_handler,
exploration::{ExplorationFocused, Exploring}, exploration::{ExplorationFocused, Exploring},
@ -309,9 +308,7 @@ fn remove_direction(
) { ) {
for entity in &mut removed { for entity in &mut removed {
if directions.contains(entity) { if directions.contains(entity) {
commands.run_if_exists(entity, |mut entity| { commands.entity(entity).remove::<CardinalDirection>();
entity.remove::<CardinalDirection>();
});
} }
} }
} }

View File

@ -14,7 +14,6 @@ use leafwing_input_manager::{axislike::DualAxisData, plugin::InputManagerSystem,
use pathfinding::prelude::*; use pathfinding::prelude::*;
use crate::{ use crate::{
commands::RunIfExistsExt,
core::{PointLike, TransformExt}, core::{PointLike, TransformExt},
map::{Map, MapObstruction}, map::{Map, MapObstruction},
navigation::{NavigationAction, RotationSpeed}, navigation::{NavigationAction, RotationSpeed},
@ -235,11 +234,11 @@ fn calculate_path(
shape_clone, shape_clone,
) )
}); });
commands.run_if_exists(entity, |mut entity| { commands
entity.insert(Calculating(task)); .entity(entity)
entity.remove::<Path>(); .insert(Calculating(task))
entity.remove::<NoPath>(); .remove::<Path>()
}); .remove::<NoPath>();
} }
} }

View File

@ -4,7 +4,7 @@ use bevy::{prelude::*, transform::TransformSystem};
use bevy_synthizer::{Buffer, Sound}; use bevy_synthizer::{Buffer, Sound};
use rand::random; use rand::random;
use crate::{commands::RunIfExistsExt, core::PointLike}; use crate::core::PointLike;
#[derive(Component, Clone, Debug, Reflect)] #[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)] #[reflect(Component)]
@ -31,13 +31,11 @@ impl Default for Footstep {
fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Footstep>>) { fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Footstep>>) {
for (entity, footstep) in &footsteps { for (entity, footstep) in &footsteps {
let buffer = footstep.buffer.clone(); let buffer = footstep.buffer.clone();
commands.run_if_exists(entity, move |mut entity| { commands.entity(entity).insert(Sound {
entity.insert(Sound {
audio: buffer.into(), audio: buffer.into(),
paused: true, paused: true,
..default() ..default()
}); });
});
} }
} }

View File

@ -6,7 +6,6 @@ use bevy_synthizer::{Audio, Buffer, Sound};
use rand::random; use rand::random;
use crate::{ use crate::{
commands::RunIfExistsExt,
core::Player, core::Player,
exploration::ExplorationFocused, exploration::ExplorationFocused,
visibility::{VisibilityChanged, Visible, VisibleEntities}, visibility::{VisibilityChanged, Visible, VisibleEntities},
@ -44,8 +43,7 @@ fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundI
let gain = icon.gain; let gain = icon.gain;
let pitch = icon.pitch; let pitch = icon.pitch;
let looping = icon.interval.is_none(); let looping = icon.interval.is_none();
commands.run_if_exists(entity, move |mut entity| { commands.entity(entity).insert(Sound {
entity.insert(Sound {
audio: buffer.into(), audio: buffer.into(),
gain, gain,
pitch, pitch,
@ -53,7 +51,6 @@ fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundI
paused: true, paused: true,
..default() ..default()
}); });
});
} }
} }

View File

@ -4,7 +4,6 @@ use bevy::prelude::*;
use crate::{ use crate::{
bevy_synthizer::{Listener, Sound}, bevy_synthizer::{Listener, Sound},
commands::RunIfExistsExt,
sound::SoundIcon, sound::SoundIcon,
}; };
@ -30,21 +29,15 @@ fn tag_behind(
let dot = v.dot(listener_forward); let dot = v.dot(listener_forward);
let is_behind = dot <= 0.; let is_behind = dot <= 0.;
if is_behind { if is_behind {
commands.run_if_exists(entity, |mut entity| { commands.entity(entity).insert(Behind);
entity.insert(Behind);
});
} else if !is_behind { } else if !is_behind {
commands.run_if_exists(entity, |mut entity| { commands.entity(entity).remove::<Behind>();
entity.remove::<Behind>();
});
} }
} }
} }
} else { } else {
for entity in &behind { for entity in &behind {
commands.run_if_exists(entity, |mut entity| { commands.entity(entity).remove::<Behind>();
entity.remove::<Behind>();
});
} }
} }
} }

View File

@ -2,7 +2,7 @@ use bevy::{prelude::*, transform::TransformSystem};
use bevy_rapier2d::{parry::query::ClosestPoints, prelude::*}; use bevy_rapier2d::{parry::query::ClosestPoints, prelude::*};
use bevy_synthizer::{Listener, Sound}; use bevy_synthizer::{Listener, Sound};
use crate::{commands::RunIfExistsExt, core::GlobalTransformExt}; use crate::core::GlobalTransformExt;
#[derive(Component, Default, Reflect, Clone, Copy, Debug)] #[derive(Component, Default, Reflect, Clone, Copy, Debug)]
#[reflect(Component)] #[reflect(Component)]
@ -41,9 +41,7 @@ fn update(
fn removed(mut commands: Commands, mut removed: RemovedComponents<Volumetric>) { fn removed(mut commands: Commands, mut removed: RemovedComponents<Volumetric>) {
for entity in &mut removed { for entity in &mut removed {
commands.run_if_exists(entity, |mut entity| { commands.entity(entity).insert(TransformBundle::default());
entity.insert(TransformBundle::default());
});
} }
} }