2024-03-16 19:53:57 +00:00
|
|
|
use bevy::prelude::*;
|
2024-03-16 23:41:06 +00:00
|
|
|
use bevy::audio::PlaybackMode;
|
2024-03-18 02:01:47 +00:00
|
|
|
use crate::settings;
|
2024-03-16 19:53:57 +00:00
|
|
|
|
2024-03-18 01:24:52 +00:00
|
|
|
const ASSET_CLICK: &str = "sounds/click-button-140881-crop.ogg";
|
|
|
|
const ASSET_SWITCH: &str = "sounds/typosonic-typing-192811-crop.ogg";
|
2024-03-18 02:01:47 +00:00
|
|
|
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";
|
2024-03-18 01:15:44 +00:00
|
|
|
|
2024-03-17 23:04:23 +00:00
|
|
|
pub struct AudioPlugin;
|
|
|
|
impl Plugin for AudioPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
app.add_systems(Startup, setup);
|
|
|
|
app.add_systems(Update, toggle_bgm);
|
2024-03-18 01:15:44 +00:00
|
|
|
app.add_systems(PostUpdate, play_sfx);
|
2024-03-18 02:01:47 +00:00
|
|
|
app.add_systems(PostUpdate, update_music);
|
2024-03-18 00:52:41 +00:00
|
|
|
app.add_event::<AudioClickEvent>();
|
2024-03-18 01:15:44 +00:00
|
|
|
app.add_event::<AudioSwitchEvent>();
|
2024-03-18 02:01:47 +00:00
|
|
|
app.add_event::<ToggleMusicEvent>();
|
2024-03-17 23:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 00:52:41 +00:00
|
|
|
#[derive(Event)]
|
|
|
|
pub struct AudioClickEvent();
|
|
|
|
|
2024-03-18 01:15:44 +00:00
|
|
|
#[derive(Event)]
|
|
|
|
pub struct AudioSwitchEvent();
|
|
|
|
|
2024-03-18 02:01:47 +00:00
|
|
|
#[derive(Event)]
|
|
|
|
pub struct ToggleMusicEvent();
|
|
|
|
|
2024-03-16 19:53:57 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct ComponentBGM;
|
|
|
|
|
2024-03-18 02:01:47 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct ComponentRadio;
|
|
|
|
|
2024-03-16 23:41:06 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct ComponentThrusterSound;
|
|
|
|
|
2024-03-18 02:01:47 +00:00
|
|
|
#[derive(Component)]
|
2024-03-16 19:53:57 +00:00
|
|
|
struct SoundBGM(Handle<AudioSource>);
|
|
|
|
|
2024-03-18 02:01:47 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct SoundRadio(Handle<AudioSource>);
|
|
|
|
|
2024-03-18 00:52:41 +00:00
|
|
|
#[derive(Resource)]
|
|
|
|
pub struct SoundClick(Handle<AudioSource>);
|
|
|
|
|
2024-03-18 01:15:44 +00:00
|
|
|
#[derive(Resource)]
|
|
|
|
pub struct SoundSwitch(Handle<AudioSource>);
|
|
|
|
|
2024-03-16 19:53:57 +00:00
|
|
|
pub fn setup(
|
|
|
|
mut commands: Commands,
|
2024-03-18 02:01:47 +00:00
|
|
|
settings: Res<settings::Settings>,
|
2024-03-16 19:53:57 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
) {
|
|
|
|
commands.spawn((
|
2024-03-18 02:01:47 +00:00
|
|
|
ComponentBGM,
|
2024-03-16 19:53:57 +00:00
|
|
|
AudioBundle {
|
2024-03-18 02:01:47 +00:00
|
|
|
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()
|
|
|
|
},
|
2024-03-16 19:53:57 +00:00
|
|
|
},
|
|
|
|
));
|
|
|
|
commands.spawn((
|
|
|
|
AudioBundle {
|
|
|
|
source: asset_server.load("sounds/wakeup.ogg"),
|
|
|
|
settings: PlaybackSettings::DESPAWN,
|
|
|
|
},
|
|
|
|
));
|
2024-03-16 23:41:06 +00:00
|
|
|
commands.spawn((
|
|
|
|
ComponentThrusterSound,
|
|
|
|
AudioBundle {
|
2024-03-17 19:28:23 +00:00
|
|
|
source: asset_server.load("tmp/loopingthrust-95548.ogg"),
|
2024-03-16 23:41:06 +00:00
|
|
|
settings: PlaybackSettings {
|
|
|
|
mode: PlaybackMode::Loop,
|
|
|
|
paused: true,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
));
|
2024-03-18 01:15:44 +00:00
|
|
|
commands.insert_resource(SoundClick(asset_server.load(ASSET_CLICK)));
|
|
|
|
commands.insert_resource(SoundSwitch(asset_server.load(ASSET_SWITCH)));
|
2024-03-16 19:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn toggle_bgm(
|
|
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
2024-03-18 02:01:47 +00:00
|
|
|
mut evwriter_toggle: EventWriter<ToggleMusicEvent>,
|
|
|
|
mut evwriter_click: EventWriter<AudioClickEvent>,
|
|
|
|
mut settings: ResMut<settings::Settings>,
|
2024-03-16 19:53:57 +00:00
|
|
|
) {
|
|
|
|
if keyboard_input.just_pressed(KeyCode::KeyT) {
|
2024-03-18 02:01:47 +00:00
|
|
|
settings.mute_music ^= true;
|
|
|
|
evwriter_click.send(AudioClickEvent());
|
|
|
|
evwriter_toggle.send(ToggleMusicEvent());
|
2024-03-16 19:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-18 00:52:41 +00:00
|
|
|
|
2024-03-18 01:15:44 +00:00
|
|
|
pub fn play_sfx(
|
2024-03-18 00:52:41 +00:00
|
|
|
mut commands: Commands,
|
2024-03-18 01:15:44 +00:00
|
|
|
mut events_click: EventReader<AudioClickEvent>,
|
|
|
|
mut events_switch: EventReader<AudioSwitchEvent>,
|
|
|
|
sound_click: Res<SoundClick>,
|
|
|
|
sound_switch: Res<SoundSwitch>,
|
2024-03-18 00:52:41 +00:00
|
|
|
) {
|
2024-03-18 01:15:44 +00:00
|
|
|
if !events_click.is_empty() {
|
|
|
|
events_click.clear();
|
|
|
|
commands.spawn(AudioBundle {
|
|
|
|
source: sound_click.0.clone(),
|
|
|
|
settings: PlaybackSettings::DESPAWN,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if !events_switch.is_empty() {
|
|
|
|
events_switch.clear();
|
2024-03-18 00:52:41 +00:00
|
|
|
commands.spawn(AudioBundle {
|
2024-03-18 01:15:44 +00:00
|
|
|
source: sound_switch.0.clone(),
|
2024-03-18 00:52:41 +00:00
|
|
|
settings: PlaybackSettings::DESPAWN,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-03-18 02:01:47 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|