play some nice Girls Go Wild by LP on the AR radio

This commit is contained in:
yuni 2024-03-18 03:01:47 +01:00
parent 0a8777a764
commit 9edc352086
2 changed files with 76 additions and 14 deletions

View file

@ -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::<AudioClickEvent>();
app.add_event::<AudioSwitchEvent>();
app.add_event::<ToggleMusicEvent>();
}
}
@ -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<AudioSource>);
#[derive(Component)]
pub struct SoundRadio(Handle<AudioSource>);
#[derive(Resource)]
pub struct SoundClick(Handle<AudioSource>);
@ -38,17 +52,31 @@ pub struct SoundSwitch(Handle<AudioSource>);
pub fn setup(
mut commands: Commands,
settings: Res<settings::Settings>,
asset_server: Res<AssetServer>,
) {
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<ButtonInput<KeyCode>>,
bgm_controller: Query<&AudioSink, With<ComponentBGM>>,
mut evwriter: EventWriter<AudioClickEvent>,
mut evwriter_toggle: EventWriter<ToggleMusicEvent>,
mut evwriter_click: EventWriter<AudioClickEvent>,
mut settings: ResMut<settings::Settings>,
) {
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<ToggleMusicEvent>,
bgm_controller: Query<&AudioSink, With<ComponentBGM>>,
radio_controller: Query<&AudioSink, With<ComponentRadio>>,
settings: Res<settings::Settings>,
) {
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");
}
}
}
}
}
}

View file

@ -213,18 +213,22 @@ fn update(
fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
settings: Res<settings::Settings>,
mut settings: ResMut<settings::Settings>,
mut query: Query<&mut Visibility, With<GaugesText>>,
mut evwriter: EventWriter<audio::AudioSwitchEvent>,
mut evwriter_togglemusic: EventWriter<audio::ToggleMusicEvent>,
) {
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());
}
}
}