Files
guardian/src/commands/bogeydope.rs

94 lines
2.6 KiB
Rust
Executable File

use bevy::ecs::{
query::{With, Without},
system::{Commands, Query, Resource},
};
use clap::Parser;
use guardian_commands::{ConsoleCommand, NamedCommand};
use guardian_core::{components::*, dcs::text::TextMessage, srs::voice::VoiceMessage};
use tracing::debug;
use crate::braa::Braa;
#[derive(Parser, Debug)]
pub struct BogeyDope;
impl NamedCommand for BogeyDope {
fn name() -> &'static str {
"[shopping|bogey dope]"
}
}
impl Resource for BogeyDope {}
pub fn bogey_dope(
mut commands: Commands,
mut cmd: ConsoleCommand<BogeyDope>,
awacs: Query<&Callsign>,
player: Query<(&Callsign, &Position)>,
npc: Query<(&Position, &Heading, &Velocity), (Without<Player>, With<Red>)>,
) {
let Some(Ok(BogeyDope)) = cmd.take() else {
return;
};
let Some(pilot) = &cmd.pilot else {
return; // no pilot
};
let Some(operator) = &cmd.operator else {
return;
};
let Ok((p_callsign, p_pos)) = player.get(*pilot) else {
debug!("no player, died?");
return;
};
let Ok(a_callsign) = awacs.get(*operator) else {
debug!("no awacs, they died?");
return;
};
let mut distance: f64 = f64::MAX;
let mut n = (None, None);
for (n_pos, n_heading, n_velocity) in npc.iter() {
if n_velocity.speed() < 10.0 {
// ignore slow/on-ground units
continue;
}
let d = p_pos.distance_to_nmi(n_pos);
if d < distance {
distance = d;
n = (Some(n_pos), Some(n_heading));
}
}
let (Some(n_position), Some(n_heading)) = n else {
let message = format!("{}, {}, {}", p_callsign, a_callsign, "Skies are clear");
let voice_message = format!(
"{} . . . {} . . . {}",
p_callsign.to_voice(),
a_callsign.to_voice(),
"Skies are clear"
);
commands.entity(*pilot).insert(TextMessage::new(message));
commands
.entity(*operator)
.insert(VoiceMessage::new(voice_message));
return;
};
let bra = Braa::new(p_pos, n_position, n_heading);
let message = format!("{}, {}, {}", p_callsign, a_callsign, bra);
let voice_message = format!(
"{} . . . {} . . . {}",
p_callsign.to_voice(),
a_callsign.to_voice(),
bra.to_voice()
);
commands.entity(*pilot).insert(TextMessage::new(message));
commands
.entity(*operator)
.insert(VoiceMessage::new(voice_message));
}