volume change sound

and simplify volume change logic
This commit is contained in:
2025-12-19 14:31:14 -05:00
parent c34fe0c90c
commit 5278bc9d1f
4 changed files with 52 additions and 36 deletions

View File

@@ -34,6 +34,7 @@ fn on_spawn_sounds(
PlaySound::ThrowHit => assets.throw_explosion.clone(), PlaySound::ThrowHit => assets.throw_explosion.clone(),
PlaySound::Reloaded => assets.reloaded.clone(), PlaySound::Reloaded => assets.reloaded.clone(),
PlaySound::Invalid => assets.invalid.clone(), PlaySound::Invalid => assets.invalid.clone(),
PlaySound::VolumeChange => assets.volume.clone(),
PlaySound::CashHeal => assets.cash_heal.clone(), PlaySound::CashHeal => assets.cash_heal.clone(),
PlaySound::HeadDrop => assets.head_drop.clone(), PlaySound::HeadDrop => assets.head_drop.clone(),
PlaySound::HeadCollect => assets.head_collect.clone(), PlaySound::HeadCollect => assets.head_collect.clone(),

View File

@@ -2,6 +2,7 @@ use crate::{
GameState, HEDZ_GREEN, HEDZ_PURPLE, GameState, HEDZ_GREEN, HEDZ_PURPLE,
client::{audio::SoundSettings, control::CharacterInputEnabled}, client::{audio::SoundSettings, control::CharacterInputEnabled},
loading_assets::UIAssets, loading_assets::UIAssets,
protocol::PlaySound,
}; };
use bevy::{color::palettes::css::BLACK, prelude::*}; use bevy::{color::palettes::css::BLACK, prelude::*};
@@ -28,8 +29,15 @@ struct PauseMenuSelection(ProgressBar);
#[derive(Resource)] #[derive(Resource)]
struct VolumeChangeCooldown(Timer); struct VolumeChangeCooldown(Timer);
#[derive(Message)]
struct VolumeChangeEvent {
direction: f32,
just_pressed: bool,
}
pub fn plugin(app: &mut App) { pub fn plugin(app: &mut App) {
app.init_state::<PauseMenuState>(); app.init_state::<PauseMenuState>();
app.add_message::<VolumeChangeEvent>();
app.add_systems(Update, open_pause_menu.run_if(in_state(GameState::Playing))); app.add_systems(Update, open_pause_menu.run_if(in_state(GameState::Playing)));
app.add_systems( app.add_systems(
@@ -38,8 +46,12 @@ pub fn plugin(app: &mut App) {
selection_input, selection_input,
selection_changed, selection_changed,
update_volume_display, update_volume_display,
change_volume_keyboard, (
change_volume_gamepad, volume_input_keyboard,
volume_input_gamepad,
apply_volume_change,
)
.chain(),
) )
.run_if(in_state(PauseMenuState::Open)), .run_if(in_state(PauseMenuState::Open)),
); );
@@ -224,14 +236,11 @@ fn update_volume_display(
} }
} }
fn change_volume_keyboard( fn volume_input_keyboard(
selection: Res<PauseMenuSelection>,
keyboard: Res<ButtonInput<KeyCode>>, keyboard: Res<ButtonInput<KeyCode>>,
mut settings: ResMut<SoundSettings>, mut events: MessageWriter<VolumeChangeEvent>,
mut cooldown: ResMut<VolumeChangeCooldown>,
time: Res<Time>,
) { ) {
let change = if keyboard.pressed(KeyCode::ArrowLeft) { let direction = if keyboard.pressed(KeyCode::ArrowLeft) {
-0.01 -0.01
} else if keyboard.pressed(KeyCode::ArrowRight) { } else if keyboard.pressed(KeyCode::ArrowRight) {
0.01 0.01
@@ -239,34 +248,17 @@ fn change_volume_keyboard(
return; return;
}; };
// On first press, apply immediately and reset timer let just_pressed =
if keyboard.just_pressed(KeyCode::ArrowLeft) || keyboard.just_pressed(KeyCode::ArrowRight) { keyboard.just_pressed(KeyCode::ArrowLeft) || keyboard.just_pressed(KeyCode::ArrowRight);
cooldown.0.reset();
} else {
cooldown.0.tick(time.delta());
if !cooldown.0.just_finished() {
return;
}
}
match selection.0 { events.write(VolumeChangeEvent {
ProgressBar::Music => { direction,
settings.music_volume = (settings.music_volume + change).clamp(0.0, 1.0); just_pressed,
} });
ProgressBar::Sound => {
settings.sound_volume = (settings.sound_volume + change).clamp(0.0, 1.0);
}
}
} }
fn change_volume_gamepad( fn volume_input_gamepad(gamepads: Query<&Gamepad>, mut events: MessageWriter<VolumeChangeEvent>) {
selection: Res<PauseMenuSelection>, let Some((direction, just_pressed)) = gamepads.iter().find_map(|gamepad| {
gamepads: Query<&Gamepad>,
mut settings: ResMut<SoundSettings>,
mut cooldown: ResMut<VolumeChangeCooldown>,
time: Res<Time>,
) {
let Some((change, just_pressed)) = gamepads.iter().find_map(|gamepad| {
if gamepad.pressed(GamepadButton::DPadLeft) { if gamepad.pressed(GamepadButton::DPadLeft) {
Some((-0.01, gamepad.just_pressed(GamepadButton::DPadLeft))) Some((-0.01, gamepad.just_pressed(GamepadButton::DPadLeft)))
} else if gamepad.pressed(GamepadButton::DPadRight) { } else if gamepad.pressed(GamepadButton::DPadRight) {
@@ -278,8 +270,26 @@ fn change_volume_gamepad(
return; return;
}; };
events.write(VolumeChangeEvent {
direction,
just_pressed,
});
}
fn apply_volume_change(
mut commands: Commands,
selection: Res<PauseMenuSelection>,
mut settings: ResMut<SoundSettings>,
mut cooldown: ResMut<VolumeChangeCooldown>,
time: Res<Time>,
mut events: MessageReader<VolumeChangeEvent>,
) {
let Some(event) = events.read().last() else {
return;
};
// On first press, apply immediately and reset timer // On first press, apply immediately and reset timer
if just_pressed { if event.just_pressed {
cooldown.0.reset(); cooldown.0.reset();
} else { } else {
cooldown.0.tick(time.delta()); cooldown.0.tick(time.delta());
@@ -288,12 +298,14 @@ fn change_volume_gamepad(
} }
} }
commands.trigger(PlaySound::VolumeChange);
match selection.0 { match selection.0 {
ProgressBar::Music => { ProgressBar::Music => {
settings.music_volume = (settings.music_volume + change).clamp(0.0, 1.0); settings.music_volume = (settings.music_volume + event.direction).clamp(0.0, 1.0);
} }
ProgressBar::Sound => { ProgressBar::Sound => {
settings.sound_volume = (settings.sound_volume + change).clamp(0.0, 1.0); settings.sound_volume = (settings.sound_volume + event.direction).clamp(0.0, 1.0);
} }
} }
} }

View File

@@ -20,6 +20,8 @@ pub struct AudioAssets {
#[asset(path = "sfx/ui/selection.ogg")] #[asset(path = "sfx/ui/selection.ogg")]
pub selection: Handle<AudioSource>, pub selection: Handle<AudioSource>,
#[asset(path = "sfx/ui/volume.ogg")]
pub volume: Handle<AudioSource>,
#[asset(path = "sfx/ui/invalid.ogg")] #[asset(path = "sfx/ui/invalid.ogg")]
pub invalid: Handle<AudioSource>, pub invalid: Handle<AudioSource>,
#[asset(path = "sfx/ui/reloaded.ogg")] #[asset(path = "sfx/ui/reloaded.ogg")]

View File

@@ -18,6 +18,7 @@ pub enum PlaySound {
HeadDrop, HeadDrop,
Selection, Selection,
Invalid, Invalid,
VolumeChange,
MissileExplosion, MissileExplosion,
Reloaded, Reloaded,
CashHeal, CashHeal,