outfly/src/audio.rs

40 lines
996 B
Rust
Raw Normal View History

2024-03-16 19:53:57 +00:00
use bevy::prelude::*;
#[derive(Component)]
pub struct ComponentBGM;
#[derive(Resource)]
struct SoundBGM(Handle<AudioSource>);
pub fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
let bgm = SoundBGM(asset_server.load("restricted/FTL - Faster Than Light (2012) OST - 12 - Void (Explore)-edQw2yYXQJM.ogg"));
commands.spawn((
AudioBundle {
source: bgm.0.clone(),
settings: PlaybackSettings::LOOP,
},
ComponentBGM,
));
commands.insert_resource(bgm);
commands.spawn((
AudioBundle {
source: asset_server.load("sounds/wakeup.ogg"),
settings: PlaybackSettings::DESPAWN,
},
));
}
pub fn toggle_bgm(
keyboard_input: Res<ButtonInput<KeyCode>>,
bgm_controller: Query<&AudioSink, With<ComponentBGM>>,
) {
if keyboard_input.just_pressed(KeyCode::KeyT) {
if let Ok(sink) = bgm_controller.get_single() {
sink.toggle()
}
}
}