From 49414989bd1b880fc279e6133170471bcc0945a0 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 18 Mar 2024 00:36:56 +0100 Subject: [PATCH] add system log to hud --- src/hud.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/hud.rs b/src/hud.rs index fa81b75..6d5da43 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -1,15 +1,18 @@ use crate::{settings, actor}; use bevy::prelude::*; use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; +use std::collections::VecDeque; const HUD_REFRESH_TIME: f32 = 0.5; const FONT: &str = "tmp/fonts/NotoSansSC-Thin.ttf"; +const LOG_MAX: usize = 5; pub struct HudPlugin; impl Plugin for HudPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); app.add_systems(Update, (update, handle_input)); + app.insert_resource(Log { logs: VecDeque::with_capacity(LOG_MAX) }); app.insert_resource(FPSUpdateTimer( Timer::from_seconds(HUD_REFRESH_TIME, TimerMode::Repeating))); } @@ -21,11 +24,28 @@ struct GaugesText; #[derive(Resource)] struct FPSUpdateTimer(Timer); +#[derive(Resource)] +struct Log { + logs: VecDeque, +} + +impl Log { + pub fn add(&mut self, message: String) { + if self.logs.len() == LOG_MAX { + self.logs.pop_front(); + } + self.logs.push_back(message); + } +} + fn setup( mut commands: Commands, settings: Res, asset_server: Res, + mut log: ResMut, ) { + log.add("Customer wake-up registered.".to_string()); + log.add("Systems reactivated.".to_string()); let visibility = if settings.hud_active { Visibility::Inherited } else { @@ -104,6 +124,24 @@ fn setup( ..default() } ), + TextSection::new( + "\n\n", + 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(( @@ -115,6 +153,7 @@ fn setup( fn update( diagnostics: Res, time: Res