diff --git a/src/audio.rs b/src/audio.rs index c04fca9..65102c1 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -43,7 +43,7 @@ impl Plugin for AudioPlugin { } } -#[derive(Event, Debug, PartialEq)] +#[derive(Event, Debug, PartialEq, Component, Clone)] pub enum AudioEvent { MainMusic, StopMainMusic, @@ -73,7 +73,7 @@ fn play_audio(mut events: Query<&AudioSource, Added>) { fn audio_trigger( mut events: EventReader, - sources: Query<&AudioSource>, + sources: Query<(&AudioSource, &AudioEvent)>, studio: Res, mut commands: Commands, server: Res, @@ -112,26 +112,36 @@ fn audio_trigger( if let Ok(event_description) = studio.0.get_event(event_str.as_str()) { let audio_source = AudioSource::new(event_description); // We are stopping a playing event - if *event == AudioEvent::StopIdle || *event == AudioEvent::StopMainMusic { - info!("Stopping audio {}", event_str); - // TODO: Find and stop playing instances of this audio - sources - .iter() - .filter(|source| { - format!("{:?}", source.event_instance) - == format!("{:?}", audio_source.event_instance) - }) - .for_each(|source| source.stop()); - // we are playing a sound - } else { - info!("Playing audio {}", event_str); - commands.spawn(audio_source); + match event { + // Find and stop playing instances of idle audio + AudioEvent::StopIdle => { + sources + .iter() + .filter(|(_, source_event)| **source_event == AudioEvent::Idle) + .for_each(|(source, _)| { + info!("Stopping audio {}", event_str); + source.stop(); + }); + } + // Find and stop playing instances of main music + AudioEvent::StopMainMusic => { + sources + .iter() + .filter(|(_, source_event)| **source_event == AudioEvent::MainMusic) + .for_each(|(source, _)| { + info!("Stopping audio {}", event_str); + source.stop(); + }); + } + // we are playing a sound + _ => { + info!("Playing audio {}", event_str); + commands.spawn((audio_source, event.clone())); + } } } else { - warn!("Music not found for {:?}", event_str); + warn!("No music set for {:?} in {:?}", event, state); } - } else { - warn!("No music set for {:?} in {:?}", event, state); } }); }