From 496c4ece71e4e4cb85804fe1a4d40a40a8f93d33 Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 18 Mar 2024 01:52:41 +0100 Subject: [PATCH] add a nice click sound --- src/audio.rs | 25 +++++++++++++++++++++++++ src/hud.rs | 4 +++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/audio.rs b/src/audio.rs index 1c8c801..b95259a 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -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::(); } } +#[derive(Event)] +pub struct AudioClickEvent(); + #[derive(Component)] pub struct ComponentBGM; @@ -18,6 +23,9 @@ pub struct ComponentThrusterSound; #[derive(Resource)] struct SoundBGM(Handle); +#[derive(Resource)] +pub struct SoundClick(Handle); + pub fn setup( mut commands: Commands, asset_server: Res, @@ -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, + sound: Res, +) { + if !events.is_empty() { + events.clear(); + commands.spawn(AudioBundle { + source: sound.0.clone(), + // auto-despawn the entity when playback finishes + settings: PlaybackSettings::DESPAWN, + }); + } +} diff --git a/src/hud.rs b/src/hud.rs index ee60291..ce8129b 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -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>, settings: Res, mut query: Query<&mut Visibility, With>, + mut ev_click: EventWriter, ) { 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()); } } }