76 lines
1.9 KiB
Rust
76 lines
1.9 KiB
Rust
use crate::loading_assets::AudioAssets;
|
|
use bevy::prelude::*;
|
|
|
|
#[derive(Event, Clone, Debug)]
|
|
pub enum PlaySound {
|
|
Hit,
|
|
KeyCollect,
|
|
Gun,
|
|
Throw,
|
|
ThrowHit,
|
|
Gate,
|
|
CashCollect,
|
|
Selection,
|
|
Invalid,
|
|
Reloaded,
|
|
Backpack { open: bool },
|
|
Head(String),
|
|
}
|
|
|
|
pub fn plugin(app: &mut App) {
|
|
app.add_observer(spawn_sounds);
|
|
}
|
|
|
|
fn spawn_sounds(
|
|
trigger: Trigger<PlaySound>,
|
|
mut commands: Commands,
|
|
// sound_res: Res<AudioAssets>,
|
|
// settings: SettingsRead,
|
|
assets: Res<AudioAssets>,
|
|
) {
|
|
let event = trigger.event();
|
|
|
|
// if !settings.is_sound_on() {
|
|
// continue;
|
|
// }
|
|
|
|
let source = match event {
|
|
PlaySound::Hit => {
|
|
let version = rand::random::<u8>() % 3;
|
|
assets.hit[version as usize].clone()
|
|
}
|
|
PlaySound::KeyCollect => assets.key_collect.clone(),
|
|
PlaySound::Gun => assets.gun.clone(),
|
|
PlaySound::Gate => assets.gate.clone(),
|
|
PlaySound::CashCollect => assets.cash.clone(),
|
|
PlaySound::Selection => assets.selection.clone(),
|
|
PlaySound::Throw => assets.throw.clone(),
|
|
PlaySound::ThrowHit => assets.throw_explosion.clone(),
|
|
PlaySound::Reloaded => assets.reloaded.clone(),
|
|
PlaySound::Invalid => assets.invalid.clone(),
|
|
PlaySound::Backpack { open } => {
|
|
if *open {
|
|
assets.backpack_open.clone()
|
|
} else {
|
|
assets.backpack_close.clone()
|
|
}
|
|
}
|
|
PlaySound::Head(name) => {
|
|
let filename = format!("{}.ogg", name);
|
|
assets
|
|
.head
|
|
.get(filename.as_str())
|
|
.unwrap_or_else(|| panic!("invalid head '{}'", filename))
|
|
.clone()
|
|
}
|
|
};
|
|
|
|
commands.spawn((
|
|
AudioPlayer::new(source),
|
|
PlaybackSettings {
|
|
mode: bevy::audio::PlaybackMode::Despawn,
|
|
..Default::default()
|
|
},
|
|
));
|
|
}
|