outfly/src/audio.rs

55 lines
1.4 KiB
Rust

use bevy::prelude::*;
use bevy::audio::PlaybackMode;
#[derive(Component)]
pub struct ComponentBGM;
#[derive(Component)]
pub struct ComponentThrusterSound;
#[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,
},
));
commands.spawn((
ComponentThrusterSound,
AudioBundle {
source: asset_server.load("restricted/loopingthrust-95548.ogg"),
settings: PlaybackSettings {
mode: PlaybackMode::Loop,
paused: true,
..default()
},
},
));
}
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()
}
}
}