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)]
struct SoundInhale(Handle<AudioSource>);
struct SoundBGM(Handle<AudioSource>);
fn setup(
@ -57,20 +59,21 @@ fn setup(
}
// Sound
let inhale_sound = asset_server.load("sounds/wakeup.ogg");
let res_inhale_sound = SoundInhale(inhale_sound);
//commands.insert_resource(res_inhale_sound);
commands.spawn(AudioBundle {
source: res_inhale_sound.0.clone(),
// auto-despawn the entity when playback finishes
settings: PlaybackSettings::DESPAWN,
});
commands.spawn(AudioBundle {
source: asset_server.load("restricted/FTL - Faster Than Light (2012) OST - 12 - Void (Explore)-edQw2yYXQJM.ogg"),
..default()
});
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,
},
));
// camera
commands.spawn((
@ -148,8 +151,14 @@ fn asset_loaded(
fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
bgm_controller: Query<&AudioSink, With<ComponentBGM>>,
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) {
app_exit_events.send(bevy::app::AppExit);
}