use bevy_app::{App, Plugin, Update};
use bevy_ecs::{
resource::Resource,
system::{Res, ResMut},
};
use bevy_time::{Stopwatch, Time};
use web_sys::window;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct BackgroundTimerPlugin;
impl Plugin for BackgroundTimerPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(BackgroundTimer::default());
app.add_systems(Update, system_background_timer);
}
}
#[derive(Clone, Debug, PartialEq, Default, Resource)]
pub struct BackgroundTimer(pub Stopwatch);
fn system_background_timer(mut timer: ResMut<BackgroundTimer>, time: Res<Time>) {
match window()
.and_then(|w| w.document())
.is_some_and(|d| d.hidden())
{
true => _ = timer.0.tick(time.delta()),
false => timer.0.reset(),
};
}