add a nice click sound

This commit is contained in:
yuni 2024-03-18 01:52:41 +01:00
parent 657279e698
commit 496c4ece71
2 changed files with 28 additions and 1 deletions

View file

@ -6,9 +6,14 @@ impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Update, toggle_bgm);
app.add_systems(PostUpdate, play_click);
app.add_event::<AudioClickEvent>();
}
}
#[derive(Event)]
pub struct AudioClickEvent();
#[derive(Component)]
pub struct ComponentBGM;
@ -18,6 +23,9 @@ pub struct ComponentThrusterSound;
#[derive(Resource)]
struct SoundBGM(Handle<AudioSource>);
#[derive(Resource)]
pub struct SoundClick(Handle<AudioSource>);
pub fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
@ -48,6 +56,8 @@ pub fn setup(
},
},
));
let click_sound_handle = asset_server.load("tmp/analog-appliance-button-1-185276.ogg");
commands.insert_resource(SoundClick(click_sound_handle));
}
pub fn toggle_bgm(
@ -60,3 +70,18 @@ pub fn toggle_bgm(
}
}
}
pub fn play_click(
mut commands: Commands,
mut events: EventReader<AudioClickEvent>,
sound: Res<SoundClick>,
) {
if !events.is_empty() {
events.clear();
commands.spawn(AudioBundle {
source: sound.0.clone(),
// auto-despawn the entity when playback finishes
settings: PlaybackSettings::DESPAWN,
});
}
}

View file

@ -1,4 +1,4 @@
use crate::{settings, actor};
use crate::{settings, actor, audio};
use bevy::prelude::*;
use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
use std::collections::VecDeque;
@ -215,6 +215,7 @@ fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
settings: Res<settings::Settings>,
mut query: Query<&mut Visibility, With<GaugesText>>,
mut ev_click: EventWriter<audio::AudioClickEvent>,
) {
if keyboard_input.just_pressed(settings.key_togglehud) {
for mut vis in &mut query {
@ -223,6 +224,7 @@ fn handle_input(
} else {
*vis = Visibility::Inherited;
}
ev_click.send(audio::AudioClickEvent());
}
}
}