Files
guardian/src/commands/bogeydope.rs
2023-12-22 04:50:17 +01:00

86 lines
2.2 KiB
Rust

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 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,
cmd: ConsoleCommand<BogeyDope>,
awacs: Query<&Callsign>,
player: Query<(&Callsign, &Position)>,
npc: Query<(&Position, &Heading), (Without<Player>, With<Red>)>,
) {
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 {
eprintln!("no player, died?");
return;
};
let Ok(a_callsign) = awacs.get(*operator) else {
eprintln!("no awacs, they died?");
return;
};
let mut distance: f64 = 0.0;
let mut n = (None, None);
for (n_pos, n_heading) in npc.iter() {
let d = p_pos.distance_to_nmi(n_pos);
if distance < d {
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.spawn(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.spawn(TextMessage::new(message));
commands
.entity(*operator)
.insert(VoiceMessage::new(voice_message));
}