added t key to toggle music

This commit is contained in:
yuni 2024-03-16 20:46:42 +01:00
parent c003193723
commit e0c1b2cfb8

View file

@ -40,8 +40,10 @@ const CUBEMAPS: &[(&str, CompressedImageFormats)] = &[
), ),
]; ];
#[derive(Component)]
struct ComponentBGM;
#[derive(Resource)] #[derive(Resource)]
struct SoundInhale(Handle<AudioSource>); struct SoundBGM(Handle<AudioSource>);
fn setup( fn setup(
@ -57,20 +59,21 @@ fn setup(
} }
// Sound // Sound
let inhale_sound = asset_server.load("sounds/wakeup.ogg"); let bgm = SoundBGM(asset_server.load("restricted/FTL - Faster Than Light (2012) OST - 12 - Void (Explore)-edQw2yYXQJM.ogg"));
let res_inhale_sound = SoundInhale(inhale_sound); commands.spawn((
//commands.insert_resource(res_inhale_sound); AudioBundle {
commands.spawn(AudioBundle { source: bgm.0.clone(),
source: res_inhale_sound.0.clone(), settings: PlaybackSettings::LOOP,
// auto-despawn the entity when playback finishes },
ComponentBGM,
));
commands.insert_resource(bgm);
commands.spawn((
AudioBundle {
source: asset_server.load("sounds/wakeup.ogg"),
settings: PlaybackSettings::DESPAWN, settings: PlaybackSettings::DESPAWN,
}); },
commands.spawn(AudioBundle { ));
source: asset_server.load("restricted/FTL - Faster Than Light (2012) OST - 12 - Void (Explore)-edQw2yYXQJM.ogg"),
..default()
});
// camera // camera
commands.spawn(( commands.spawn((
@ -148,8 +151,14 @@ fn asset_loaded(
fn handle_input( fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
bgm_controller: Query<&AudioSink, With<ComponentBGM>>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>> mut app_exit_events: ResMut<Events<bevy::app::AppExit>>
) { ) {
if keyboard_input.just_pressed(KeyCode::KeyT) {
if let Ok(sink) = bgm_controller.get_single() {
sink.toggle()
}
}
if keyboard_input.pressed(KeyCode::KeyQ) { if keyboard_input.pressed(KeyCode::KeyQ) {
app_exit_events.send(bevy::app::AppExit); app_exit_events.send(bevy::app::AppExit);
} }