make debounce functionality into a reusable ting
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_pkv::prelude::*;
|
||||
|
||||
use crate::client::audio::SoundSettings;
|
||||
|
||||
#[derive(Resource)]
|
||||
struct SaveTimer(Timer);
|
||||
use crate::{client::audio::SoundSettings, utils::Debounce};
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.insert_resource(PkvStore::new("Rustunit", "HEDZ"));
|
||||
app.insert_resource(SaveTimer(Timer::from_seconds(1.0, TimerMode::Once)));
|
||||
|
||||
app.add_systems(Update, persist_settings);
|
||||
app.add_systems(Startup, load_settings);
|
||||
@@ -17,16 +13,13 @@ pub fn plugin(app: &mut App) {
|
||||
fn persist_settings(
|
||||
settings: Res<SoundSettings>,
|
||||
mut pkv: ResMut<PkvStore>,
|
||||
mut timer: ResMut<SaveTimer>,
|
||||
time: Res<Time>,
|
||||
mut debounce: Debounce<1000>,
|
||||
) -> Result {
|
||||
if settings.is_changed() {
|
||||
timer.0.reset();
|
||||
debounce.reset();
|
||||
}
|
||||
|
||||
timer.0.tick(time.delta());
|
||||
|
||||
if timer.0.just_finished() {
|
||||
if debounce.finished() {
|
||||
pkv.set("audio", &*settings)?;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user