generalize cooldown
This commit is contained in:
@@ -3,6 +3,7 @@ use crate::{
|
||||
client::{audio::SoundSettings, control::CharacterInputEnabled},
|
||||
loading_assets::UIAssets,
|
||||
protocol::PlaySound,
|
||||
utils::Cooldown,
|
||||
};
|
||||
use bevy::{color::palettes::css::BLACK, prelude::*};
|
||||
|
||||
@@ -26,9 +27,6 @@ struct VolumeValue(ProgressBar);
|
||||
#[derive(Resource)]
|
||||
struct PauseMenuSelection(ProgressBar);
|
||||
|
||||
#[derive(Resource)]
|
||||
struct VolumeChangeCooldown(Timer);
|
||||
|
||||
#[derive(Message)]
|
||||
struct VolumeChangeEvent {
|
||||
direction: f32,
|
||||
@@ -108,10 +106,6 @@ fn setup(mut commands: Commands, assets: Res<UIAssets>, settings: Res<SoundSetti
|
||||
));
|
||||
|
||||
commands.insert_resource(PauseMenuSelection(ProgressBar::Music));
|
||||
commands.insert_resource(VolumeChangeCooldown(Timer::from_seconds(
|
||||
0.09,
|
||||
TimerMode::Repeating,
|
||||
)));
|
||||
}
|
||||
|
||||
fn spawn_progress(bar: ProgressBar, value: u8, font: Handle<Font>) -> impl Bundle {
|
||||
@@ -280,22 +274,15 @@ fn apply_volume_change(
|
||||
mut commands: Commands,
|
||||
selection: Res<PauseMenuSelection>,
|
||||
mut settings: ResMut<SoundSettings>,
|
||||
mut cooldown: ResMut<VolumeChangeCooldown>,
|
||||
time: Res<Time>,
|
||||
mut cooldown: Cooldown<90>,
|
||||
mut events: MessageReader<VolumeChangeEvent>,
|
||||
) {
|
||||
let Some(event) = events.read().last() else {
|
||||
return;
|
||||
};
|
||||
|
||||
// On first press, apply immediately and reset timer
|
||||
if event.just_pressed {
|
||||
cooldown.0.reset();
|
||||
} else {
|
||||
cooldown.0.tick(time.delta());
|
||||
if !cooldown.0.just_finished() {
|
||||
return;
|
||||
}
|
||||
if !cooldown.ready(event.just_pressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
commands.trigger(PlaySound::VolumeChange);
|
||||
|
||||
Reference in New Issue
Block a user