add a second click sound

This commit is contained in:
yuni 2024-03-18 02:15:44 +01:00
parent 496c4ece71
commit cbccd3b109
4 changed files with 40 additions and 15 deletions

View file

@ -1,19 +1,31 @@
use bevy::prelude::*;
use bevy::audio::PlaybackMode;
//const ASSET_CLICK: &str = "tmp/analog-appliance-button-10-185285.mp3.ogg";
//const ASSET_CLICK: &str = "tmp/analog-appliance-button-1-185276.ogg";
//const ASSET_CLICK: &str = "tmp/click-button-140881.mp3.ogg";
//const ASSET_CLICK: &str = "tmp/keyboard-chblue-146642.mp3.ogg";
const ASSET_CLICK: &str = "tmp/keyboard-chcl-146641.mp3.ogg";
//const ASSET_CLICK: &str = "tmp/typosonic-typing-192811.mp3.ogg";
const ASSET_SWITCH: &str = "tmp/typosonic-typing-192811-crop.ogg";
pub struct AudioPlugin;
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_systems(PostUpdate, play_sfx);
app.add_event::<AudioClickEvent>();
app.add_event::<AudioSwitchEvent>();
}
}
#[derive(Event)]
pub struct AudioClickEvent();
#[derive(Event)]
pub struct AudioSwitchEvent();
#[derive(Component)]
pub struct ComponentBGM;
@ -26,6 +38,9 @@ struct SoundBGM(Handle<AudioSource>);
#[derive(Resource)]
pub struct SoundClick(Handle<AudioSource>);
#[derive(Resource)]
pub struct SoundSwitch(Handle<AudioSource>);
pub fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
@ -56,31 +71,41 @@ pub fn setup(
},
},
));
let click_sound_handle = asset_server.load("tmp/analog-appliance-button-1-185276.ogg");
commands.insert_resource(SoundClick(click_sound_handle));
commands.insert_resource(SoundClick(asset_server.load(ASSET_CLICK)));
commands.insert_resource(SoundSwitch(asset_server.load(ASSET_SWITCH)));
}
pub fn toggle_bgm(
keyboard_input: Res<ButtonInput<KeyCode>>,
bgm_controller: Query<&AudioSink, With<ComponentBGM>>,
mut evwriter: EventWriter<AudioClickEvent>,
) {
if keyboard_input.just_pressed(KeyCode::KeyT) {
if let Ok(sink) = bgm_controller.get_single() {
sink.toggle()
sink.toggle();
evwriter.send(AudioClickEvent());
}
}
}
pub fn play_click(
pub fn play_sfx(
mut commands: Commands,
mut events: EventReader<AudioClickEvent>,
sound: Res<SoundClick>,
mut events_click: EventReader<AudioClickEvent>,
mut events_switch: EventReader<AudioSwitchEvent>,
sound_click: Res<SoundClick>,
sound_switch: Res<SoundSwitch>,
) {
if !events.is_empty() {
events.clear();
if !events_click.is_empty() {
events_click.clear();
commands.spawn(AudioBundle {
source: sound.0.clone(),
// auto-despawn the entity when playback finishes
source: sound_click.0.clone(),
settings: PlaybackSettings::DESPAWN,
});
}
if !events_switch.is_empty() {
events_switch.clear();
commands.spawn(AudioBundle {
source: sound_switch.0.clone(),
settings: PlaybackSettings::DESPAWN,
});
}

View file

@ -215,7 +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>,
mut evwriter: EventWriter<audio::AudioSwitchEvent>,
) {
if keyboard_input.just_pressed(settings.key_togglehud) {
for mut vis in &mut query {
@ -224,7 +224,7 @@ fn handle_input(
} else {
*vis = Visibility::Inherited;
}
ev_click.send(audio::AudioClickEvent());
evwriter.send(audio::AudioSwitchEvent());
}
}
}

View file

@ -22,7 +22,7 @@ impl Default for Settings {
font_size_hud: 32.0,
font_size_conversations: 32.0,
key_togglehud: KeyCode::Tab,
hud_active: true,
hud_active: false,
}
}
}

View file

@ -151,7 +151,7 @@ pub fn setup(
EulerRot::ZYX,
0.0,
PI / 1.0,
-PI / 5.0,
-PI / 7.0,
)),
cascade_shadow_config: CascadeShadowConfigBuilder {
first_cascade_far_bound: 7.0,