From 130deb4338b8e045a485e42fbfe4194bb765c905 Mon Sep 17 00:00:00 2001 From: Aleksa Pavlovic Date: Sat, 9 Dec 2023 18:40:55 +0100 Subject: [PATCH] add in-game ping display --- src/components.rs | 3 ++ src/lib.rs | 9 ++--- src/resources.rs | 7 ++-- src/systems.rs | 45 ++++++++++++++++++++++--- src/types.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++- src/utils.rs | 64 ++++++++++++++++++++++++++++++++++- 6 files changed, 200 insertions(+), 13 deletions(-) diff --git a/src/components.rs b/src/components.rs index 93974f2..b2de89f 100644 --- a/src/components.rs +++ b/src/components.rs @@ -19,6 +19,9 @@ pub struct HUDRoot; #[derive(Component)] pub struct GameTimerDisplay; +#[derive(Component)] +pub struct NetworkStatsDisplay; + #[derive(Component)] pub struct PlayerPortraitDisplay; diff --git a/src/lib.rs b/src/lib.rs index 1620118..a6dfe99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ mod web; use bevy::{ecs as bevy_ecs, prelude::*}; use bevy_ggrs::prelude::*; +use types::Cooldown; #[cfg(target_arch = "wasm32")] use wasm_bindgen::prelude::*; @@ -72,10 +73,10 @@ pub fn run() { .init_resource::() .init_resource::() .add_state::() - .insert_resource(NetworkStatsTimer(Timer::from_seconds( - 2.0, - TimerMode::Repeating, - ))) + .insert_resource(NetworkStatsCooldown { + cooldown: Cooldown::from_seconds(1.0), + print_cooldown: 0, + }) .add_systems(Update, print_network_stats_system) .add_systems( OnEnter(AppState::Lobby), diff --git a/src/resources.rs b/src/resources.rs index 2e0bd9f..8784d99 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -5,11 +5,14 @@ use rand::{rngs::StdRng, seq::IteratorRandom, Rng}; use crate::{ components::Position, constants::COLORS, - types::{Direction, ICEServerConfig, PlayerID, PostFreezeAction}, + types::{Cooldown, Direction, ICEServerConfig, PlayerID, PostFreezeAction}, }; #[derive(Resource)] -pub struct NetworkStatsTimer(pub Timer); +pub struct NetworkStatsCooldown { + pub cooldown: Cooldown, + pub print_cooldown: usize, +} #[derive(Resource)] pub struct Fonts { diff --git a/src/systems.rs b/src/systems.rs index 59db101..350c9e1 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -41,19 +41,54 @@ use crate::{ pub fn print_network_stats_system( time: Res