diff --git a/src/main.rs b/src/main.rs index 365bab1..0d0ab6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,8 +40,10 @@ const CUBEMAPS: &[(&str, CompressedImageFormats)] = &[ ), ]; +#[derive(Component)] +struct ComponentBGM; #[derive(Resource)] -struct SoundInhale(Handle); +struct SoundBGM(Handle); 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>, + bgm_controller: Query<&AudioSink, With>, mut app_exit_events: ResMut> ) { + 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); }