91 lines
2.5 KiB
Rust
91 lines
2.5 KiB
Rust
use crate::{global_observer, loading_assets::AudioAssets};
|
|
use bevy::prelude::*;
|
|
|
|
#[derive(Event, Clone, Debug)]
|
|
pub enum PlaySound {
|
|
Hit,
|
|
KeyCollect,
|
|
Gun,
|
|
Throw,
|
|
ThrowHit,
|
|
Gate,
|
|
CashCollect,
|
|
HeadCollect,
|
|
SecretHeadCollect,
|
|
HeadDrop,
|
|
Selection,
|
|
Invalid,
|
|
MissileExplosion,
|
|
Reloaded,
|
|
CashHeal,
|
|
Crossbow,
|
|
Beaming,
|
|
Backpack { open: bool },
|
|
Head(String),
|
|
}
|
|
|
|
pub fn plugin(app: &mut App) {
|
|
global_observer!(app, on_spawn_sounds);
|
|
}
|
|
|
|
fn on_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::Crossbow => assets.crossbow.clone(),
|
|
PlaySound::Gate => assets.gate.clone(),
|
|
PlaySound::CashCollect => assets.cash_collect.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::CashHeal => assets.cash_heal.clone(),
|
|
PlaySound::HeadDrop => assets.head_drop.clone(),
|
|
PlaySound::HeadCollect => assets.head_collect.clone(),
|
|
PlaySound::SecretHeadCollect => assets.secret_head_collect.clone(),
|
|
PlaySound::MissileExplosion => assets.missile_explosion.clone(),
|
|
PlaySound::Beaming => assets.beaming.clone(),
|
|
PlaySound::Backpack { open } => {
|
|
if *open {
|
|
assets.backpack_open.clone()
|
|
} else {
|
|
assets.backpack_close.clone()
|
|
}
|
|
}
|
|
PlaySound::Head(name) => {
|
|
let filename = format!("{name}.ogg");
|
|
assets
|
|
.head
|
|
.get(filename.as_str())
|
|
.unwrap_or_else(|| panic!("invalid head '{filename}'"))
|
|
.clone()
|
|
}
|
|
};
|
|
|
|
commands.spawn((
|
|
Name::new("sfx"),
|
|
AudioPlayer::new(source),
|
|
PlaybackSettings {
|
|
mode: bevy::audio::PlaybackMode::Despawn,
|
|
..Default::default()
|
|
},
|
|
));
|
|
}
|