outfly/src/hud.rs

133 lines
3.8 KiB
Rust

use crate::{settings, player};
use bevy::prelude::*;
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
pub struct OutFlyHudPlugin;
impl Plugin for OutFlyHudPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Update, (update, handle_input));
app.insert_resource(FPSUpdateTimer(Timer::from_seconds(0.25, TimerMode::Repeating)));
}
}
const FONT: &str = "tmp/fonts/NotoSansSC-Thin.ttf";
#[derive(Component)]
struct GaugesText;
#[derive(Resource)]
struct FPSUpdateTimer(Timer);
fn setup(
mut commands: Commands,
settings: Res<settings::Settings>,
asset_server: Res<AssetServer>,
) {
let visibility = if settings.ar_active {
Visibility::Inherited
} else {
Visibility::Hidden
};
let mut bundle_fps = TextBundle::from_sections([
TextSection::new(
" 帧率 ",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
},
),
TextSection::new(
"",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
}
),
TextSection::new(
"\n OXYGEN ",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
},
),
TextSection::new(
"",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
}
),
TextSection::new(
"\n Adren水平 ",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
},
),
TextSection::new(
"",
TextStyle {
font: asset_server.load(FONT),
font_size: settings.font_size_hud,
color: Color::GRAY,
..default()
}
),
]);
bundle_fps.visibility = visibility;
commands.spawn((
bundle_fps,
GaugesText,
));
}
fn update(
diagnostics: Res<DiagnosticsStore>,
time:Res<Time>,
playervars:Res<player::PlayerVars>,
mut timer: ResMut<FPSUpdateTimer>,
mut query: Query<&mut Text, With<GaugesText>>,
) {
if timer.0.tick(time.delta()).just_finished() {
for mut text in &mut query {
if let Some(fps) = diagnostics.get(&FrameTimeDiagnosticsPlugin::FPS) {
if let Some(value) = fps.smoothed() {
// Update the value of the second section
text.sections[1].value = format!("{value:.0}");
}
}
let oxygen = playervars.oxygen * 100.0;
text.sections[3].value = format!("{oxygen:.1}%");
let adrenaline = playervars.adrenaline * 430.0;
text.sections[5].value = format!("{adrenaline:.1}ppm");
}
}
}
fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
settings: Res<settings::Settings>,
mut query: Query<&mut Visibility, With<GaugesText>>,
) {
if keyboard_input.just_pressed(settings.key_togglehud) {
for mut vis in &mut query {
if *vis == Visibility::Inherited {
*vis = Visibility::Hidden;
} else {
*vis = Visibility::Inherited;
}
}
}
}