From 9edc35208614772d2ba950156c85a3dd79ba5a6f Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 18 Mar 2024 03:01:47 +0100 Subject: [PATCH] play some nice Girls Go Wild by LP on the AR radio --- src/audio.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++-------- src/hud.rs | 6 +++- 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index aa93ba1..fd1dee1 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -1,8 +1,11 @@ use bevy::prelude::*; use bevy::audio::PlaybackMode; +use crate::settings; const ASSET_CLICK: &str = "sounds/click-button-140881-crop.ogg"; const ASSET_SWITCH: &str = "sounds/typosonic-typing-192811-crop.ogg"; +const ASSET_RADIO: &str = "tmp/LP - Girls Go Wild (Official Music Video)-M7XRN0oHGIM.ogg"; +const ASSET_BGM: &str = "tmp/FTL - Faster Than Light (2012) OST - 12 - Void (Explore)-edQw2yYXQJM.ogg"; pub struct AudioPlugin; impl Plugin for AudioPlugin { @@ -10,8 +13,10 @@ impl Plugin for AudioPlugin { app.add_systems(Startup, setup); app.add_systems(Update, toggle_bgm); app.add_systems(PostUpdate, play_sfx); + app.add_systems(PostUpdate, update_music); app.add_event::(); app.add_event::(); + app.add_event::(); } } @@ -21,15 +26,24 @@ pub struct AudioClickEvent(); #[derive(Event)] pub struct AudioSwitchEvent(); +#[derive(Event)] +pub struct ToggleMusicEvent(); + #[derive(Component)] pub struct ComponentBGM; +#[derive(Component)] +pub struct ComponentRadio; + #[derive(Component)] pub struct ComponentThrusterSound; -#[derive(Resource)] +#[derive(Component)] struct SoundBGM(Handle); +#[derive(Component)] +pub struct SoundRadio(Handle); + #[derive(Resource)] pub struct SoundClick(Handle); @@ -38,17 +52,31 @@ pub struct SoundSwitch(Handle); pub fn setup( mut commands: Commands, + settings: Res, asset_server: Res, ) { - let bgm = SoundBGM(asset_server.load("tmp/FTL - Faster Than Light (2012) OST - 12 - Void (Explore)-edQw2yYXQJM.ogg")); commands.spawn(( - AudioBundle { - source: bgm.0.clone(), - settings: PlaybackSettings::LOOP, - }, ComponentBGM, + AudioBundle { + source: SoundBGM(asset_server.load(ASSET_BGM)).0.clone(), + settings: PlaybackSettings { + mode: PlaybackMode::Loop, + paused: settings.hud_active, + ..default() + }, + }, + )); + commands.spawn(( + ComponentRadio, + AudioBundle { + source: SoundBGM(asset_server.load(ASSET_RADIO)).0.clone(), + settings: PlaybackSettings { + mode: PlaybackMode::Loop, + paused: !settings.hud_active, + ..default() + }, + }, )); - commands.insert_resource(bgm); commands.spawn(( AudioBundle { source: asset_server.load("sounds/wakeup.ogg"), @@ -72,14 +100,14 @@ pub fn setup( pub fn toggle_bgm( keyboard_input: Res>, - bgm_controller: Query<&AudioSink, With>, - mut evwriter: EventWriter, + mut evwriter_toggle: EventWriter, + mut evwriter_click: EventWriter, + mut settings: ResMut, ) { if keyboard_input.just_pressed(KeyCode::KeyT) { - if let Ok(sink) = bgm_controller.get_single() { - sink.toggle(); - evwriter.send(AudioClickEvent()); - } + settings.mute_music ^= true; + evwriter_click.send(AudioClickEvent()); + evwriter_toggle.send(ToggleMusicEvent()); } } @@ -105,3 +133,33 @@ pub fn play_sfx( }); } } + +pub fn update_music( + mut events: EventReader, + bgm_controller: Query<&AudioSink, With>, + radio_controller: Query<&AudioSink, With>, + settings: Res, +) { + if !events.is_empty() { + events.clear(); + if let Ok(bgm_sink) = bgm_controller.get_single() { + if let Ok(radio_sink) = radio_controller.get_single() { + if settings.mute_music { + radio_sink.pause(); + bgm_sink.pause(); + } + else { + if settings.hud_active { + radio_sink.play(); + bgm_sink.pause(); + println!("pausing"); + } else { + radio_sink.pause(); + bgm_sink.play(); + println!("playing"); + } + } + } + } + } +} diff --git a/src/hud.rs b/src/hud.rs index dd039dd..ae93afb 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -213,18 +213,22 @@ fn update( fn handle_input( keyboard_input: Res>, - settings: Res, + mut settings: ResMut, mut query: Query<&mut Visibility, With>, mut evwriter: EventWriter, + mut evwriter_togglemusic: EventWriter, ) { if keyboard_input.just_pressed(settings.key_togglehud) { for mut vis in &mut query { if *vis == Visibility::Inherited { *vis = Visibility::Hidden; + settings.hud_active = false; } else { *vis = Visibility::Inherited; + settings.hud_active = true; } evwriter.send(audio::AudioSwitchEvent()); + evwriter_togglemusic.send(audio::ToggleMusicEvent()); } } }