volume change sound
and simplify volume change logic
This commit is contained in:
@@ -34,6 +34,7 @@ fn on_spawn_sounds(
|
||||
PlaySound::ThrowHit => assets.throw_explosion.clone(),
|
||||
PlaySound::Reloaded => assets.reloaded.clone(),
|
||||
PlaySound::Invalid => assets.invalid.clone(),
|
||||
PlaySound::VolumeChange => assets.volume.clone(),
|
||||
PlaySound::CashHeal => assets.cash_heal.clone(),
|
||||
PlaySound::HeadDrop => assets.head_drop.clone(),
|
||||
PlaySound::HeadCollect => assets.head_collect.clone(),
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::{
|
||||
GameState, HEDZ_GREEN, HEDZ_PURPLE,
|
||||
client::{audio::SoundSettings, control::CharacterInputEnabled},
|
||||
loading_assets::UIAssets,
|
||||
protocol::PlaySound,
|
||||
};
|
||||
use bevy::{color::palettes::css::BLACK, prelude::*};
|
||||
|
||||
@@ -28,8 +29,15 @@ struct PauseMenuSelection(ProgressBar);
|
||||
#[derive(Resource)]
|
||||
struct VolumeChangeCooldown(Timer);
|
||||
|
||||
#[derive(Message)]
|
||||
struct VolumeChangeEvent {
|
||||
direction: f32,
|
||||
just_pressed: bool,
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.init_state::<PauseMenuState>();
|
||||
app.add_message::<VolumeChangeEvent>();
|
||||
|
||||
app.add_systems(Update, open_pause_menu.run_if(in_state(GameState::Playing)));
|
||||
app.add_systems(
|
||||
@@ -38,8 +46,12 @@ pub fn plugin(app: &mut App) {
|
||||
selection_input,
|
||||
selection_changed,
|
||||
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)),
|
||||
);
|
||||
@@ -224,14 +236,11 @@ fn update_volume_display(
|
||||
}
|
||||
}
|
||||
|
||||
fn change_volume_keyboard(
|
||||
selection: Res<PauseMenuSelection>,
|
||||
fn volume_input_keyboard(
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
mut settings: ResMut<SoundSettings>,
|
||||
mut cooldown: ResMut<VolumeChangeCooldown>,
|
||||
time: Res<Time>,
|
||||
mut events: MessageWriter<VolumeChangeEvent>,
|
||||
) {
|
||||
let change = if keyboard.pressed(KeyCode::ArrowLeft) {
|
||||
let direction = if keyboard.pressed(KeyCode::ArrowLeft) {
|
||||
-0.01
|
||||
} else if keyboard.pressed(KeyCode::ArrowRight) {
|
||||
0.01
|
||||
@@ -239,34 +248,17 @@ fn change_volume_keyboard(
|
||||
return;
|
||||
};
|
||||
|
||||
// On first press, apply immediately and reset timer
|
||||
if 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;
|
||||
}
|
||||
}
|
||||
let just_pressed =
|
||||
keyboard.just_pressed(KeyCode::ArrowLeft) || keyboard.just_pressed(KeyCode::ArrowRight);
|
||||
|
||||
match selection.0 {
|
||||
ProgressBar::Music => {
|
||||
settings.music_volume = (settings.music_volume + change).clamp(0.0, 1.0);
|
||||
}
|
||||
ProgressBar::Sound => {
|
||||
settings.sound_volume = (settings.sound_volume + change).clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
events.write(VolumeChangeEvent {
|
||||
direction,
|
||||
just_pressed,
|
||||
});
|
||||
}
|
||||
|
||||
fn change_volume_gamepad(
|
||||
selection: Res<PauseMenuSelection>,
|
||||
gamepads: Query<&Gamepad>,
|
||||
mut settings: ResMut<SoundSettings>,
|
||||
mut cooldown: ResMut<VolumeChangeCooldown>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let Some((change, just_pressed)) = gamepads.iter().find_map(|gamepad| {
|
||||
fn volume_input_gamepad(gamepads: Query<&Gamepad>, mut events: MessageWriter<VolumeChangeEvent>) {
|
||||
let Some((direction, just_pressed)) = gamepads.iter().find_map(|gamepad| {
|
||||
if gamepad.pressed(GamepadButton::DPadLeft) {
|
||||
Some((-0.01, gamepad.just_pressed(GamepadButton::DPadLeft)))
|
||||
} else if gamepad.pressed(GamepadButton::DPadRight) {
|
||||
@@ -278,8 +270,26 @@ fn change_volume_gamepad(
|
||||
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
|
||||
if just_pressed {
|
||||
if event.just_pressed {
|
||||
cooldown.0.reset();
|
||||
} else {
|
||||
cooldown.0.tick(time.delta());
|
||||
@@ -288,12 +298,14 @@ fn change_volume_gamepad(
|
||||
}
|
||||
}
|
||||
|
||||
commands.trigger(PlaySound::VolumeChange);
|
||||
|
||||
match selection.0 {
|
||||
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 => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user