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, mut commands: Commands, // sound_res: Res, // settings: SettingsRead, assets: Res, ) { let event = trigger.event(); // if !settings.is_sound_on() { // continue; // } let source = match event { PlaySound::Hit => { let version = rand::random::() % 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() }, )); }