outfly/src/audio.rs

113 lines
3.2 KiB
Rust
Raw Normal View History

2024-03-16 19:53:57 +00:00
use bevy::prelude::*;
2024-03-16 23:41:06 +00:00
use bevy::audio::PlaybackMode;
2024-03-16 19:53:57 +00:00
2024-03-18 01:15:44 +00:00
//const ASSET_CLICK: &str = "tmp/analog-appliance-button-10-185285.mp3.ogg";
//const ASSET_CLICK: &str = "tmp/analog-appliance-button-1-185276.ogg";
//const ASSET_CLICK: &str = "tmp/click-button-140881.mp3.ogg";
//const ASSET_CLICK: &str = "tmp/keyboard-chblue-146642.mp3.ogg";
const ASSET_CLICK: &str = "tmp/keyboard-chcl-146641.mp3.ogg";
//const ASSET_CLICK: &str = "tmp/typosonic-typing-192811.mp3.ogg";
const ASSET_SWITCH: &str = "tmp/typosonic-typing-192811-crop.ogg";
2024-03-17 23:04:23 +00:00
pub struct AudioPlugin;
impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Update, toggle_bgm);
2024-03-18 01:15:44 +00:00
app.add_systems(PostUpdate, play_sfx);
2024-03-18 00:52:41 +00:00
app.add_event::<AudioClickEvent>();
2024-03-18 01:15:44 +00:00
app.add_event::<AudioSwitchEvent>();
2024-03-17 23:04:23 +00:00
}
}
2024-03-18 00:52:41 +00:00
#[derive(Event)]
pub struct AudioClickEvent();
2024-03-18 01:15:44 +00:00
#[derive(Event)]
pub struct AudioSwitchEvent();
2024-03-16 19:53:57 +00:00
#[derive(Component)]
pub struct ComponentBGM;
2024-03-16 23:41:06 +00:00
#[derive(Component)]
pub struct ComponentThrusterSound;
2024-03-16 19:53:57 +00:00
#[derive(Resource)]
struct SoundBGM(Handle<AudioSource>);
2024-03-18 00:52:41 +00:00
#[derive(Resource)]
pub struct SoundClick(Handle<AudioSource>);
2024-03-18 01:15:44 +00:00
#[derive(Resource)]
pub struct SoundSwitch(Handle<AudioSource>);
2024-03-16 19:53:57 +00:00
pub fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
let bgm = SoundBGM(asset_server.load("tmp/FTL - Faster Than Light (2012) OST - 12 - Void (Explore)-edQw2yYXQJM.ogg"));
2024-03-16 19:53:57 +00:00
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,
},
));
2024-03-16 23:41:06 +00:00
commands.spawn((
ComponentThrusterSound,
AudioBundle {
source: asset_server.load("tmp/loopingthrust-95548.ogg"),
2024-03-16 23:41:06 +00:00
settings: PlaybackSettings {
mode: PlaybackMode::Loop,
paused: true,
..default()
},
},
));
2024-03-18 01:15:44 +00:00
commands.insert_resource(SoundClick(asset_server.load(ASSET_CLICK)));
commands.insert_resource(SoundSwitch(asset_server.load(ASSET_SWITCH)));
2024-03-16 19:53:57 +00:00
}
pub fn toggle_bgm(
keyboard_input: Res<ButtonInput<KeyCode>>,
bgm_controller: Query<&AudioSink, With<ComponentBGM>>,
2024-03-18 01:15:44 +00:00
mut evwriter: EventWriter<AudioClickEvent>,
2024-03-16 19:53:57 +00:00
) {
if keyboard_input.just_pressed(KeyCode::KeyT) {
if let Ok(sink) = bgm_controller.get_single() {
2024-03-18 01:15:44 +00:00
sink.toggle();
evwriter.send(AudioClickEvent());
2024-03-16 19:53:57 +00:00
}
}
}
2024-03-18 00:52:41 +00:00
2024-03-18 01:15:44 +00:00
pub fn play_sfx(
2024-03-18 00:52:41 +00:00
mut commands: Commands,
2024-03-18 01:15:44 +00:00
mut events_click: EventReader<AudioClickEvent>,
mut events_switch: EventReader<AudioSwitchEvent>,
sound_click: Res<SoundClick>,
sound_switch: Res<SoundSwitch>,
2024-03-18 00:52:41 +00:00
) {
2024-03-18 01:15:44 +00:00
if !events_click.is_empty() {
events_click.clear();
commands.spawn(AudioBundle {
source: sound_click.0.clone(),
settings: PlaybackSettings::DESPAWN,
});
}
if !events_switch.is_empty() {
events_switch.clear();
2024-03-18 00:52:41 +00:00
commands.spawn(AudioBundle {
2024-03-18 01:15:44 +00:00
source: sound_switch.0.clone(),
2024-03-18 00:52:41 +00:00
settings: PlaybackSettings::DESPAWN,
});
}
}