Skip to content
Snippets Groups Projects
Verified Commit 18181ce5 authored by Louis's avatar Louis :fire:
Browse files

Don't restart track if it matches current track

parent 37d04a75
No related branches found
Tags v0.5.1
No related merge requests found
[package]
name = "micro_musicbox"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
license = "Apache-2.0"
authors = ["Louis Capitanchik <louis@microhacks.co.uk>"]
......@@ -19,10 +19,10 @@ wav = ["bevy_kira_audio/wav"]
ogg = ["bevy_kira_audio/ogg"]
[dependencies]
bevy = { version = "0.9.0", default-features = false }
bevy = { version = "0.9.1", default-features = false }
bevy_kira_audio = { version = "0.13.0", default-features = false }
serde = { version = "1", optional = true }
[dev_dependencies]
bevy = "0.9.0"
log = "0.4"
\ No newline at end of file
bevy = "0.9.1"
log = "0.4"
......@@ -39,7 +39,11 @@ pub struct MusicBox<'w, 's, T: SuppliesAudio> {
#[derive(Debug, Default, Resource)]
pub struct MusicBoxState {
pub active_music: Option<Handle<AudioInstance>>,
/// The name used to start the currently active music track
pub active_music_name: Option<String>,
pub active_ambiance: Option<Handle<AudioInstance>>,
/// The name used to start the currently active ambiance track
pub active_ambiance_name: Option<String>,
}
impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
......@@ -54,6 +58,10 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
name: Name,
fade: AudioTween,
) -> Option<Handle<AudioInstance>> {
if self.state.active_music_name == Some(name.to_string()) {
return self.state.active_music.as_ref().map(|f| f.clone_weak());
}
self.fade_out_music(fade.clone());
self.fade_in_music(name, fade)
}
......@@ -71,6 +79,10 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
in_tween: AudioTween,
out_tween: AudioTween,
) -> Option<Handle<AudioInstance>> {
if self.state.active_music_name == Some(name.to_string()) {
return self.state.active_music.as_ref().map(|f| f.clone_weak());
}
self.fade_out_music(out_tween);
self.fade_in_music(name, in_tween)
}
......@@ -82,6 +94,10 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
///
/// A handle for the newly started audio instance, or `None` if the track was not found
pub fn play_music<Name: ToString>(&mut self, name: Name) -> Option<Handle<AudioInstance>> {
if self.state.active_music_name == Some(name.to_string()) {
return self.state.active_music.as_ref().map(|f| f.clone_weak());
}
self.stop_music();
self.fade_in_music(name, AudioTween::default())
}
......@@ -94,8 +110,14 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
/// Stop playing music on the music channel. The supplied tween will be used to fade out the track
/// before it ends
pub fn fade_out_music(&mut self, fade: AudioTween) {
let handle = std::mem::replace(&mut self.state.active_music, None)
self.state.active_music_name = None;
let handle = self
.state
.active_music
.take()
.and_then(|handle| self.audio_instances.get_mut(&handle));
if let Some(current) = handle {
current.stop(fade);
}
......@@ -112,7 +134,13 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
name: Name,
fade: AudioTween,
) -> Option<Handle<AudioInstance>> {
match self.map_tracks(name) {
let name = name.to_string();
if self.state.active_music_name.as_ref() == Some(&name) {
return self.state.active_music.as_ref().map(|f| f.clone_weak());
}
match self.map_tracks(&name) {
TrackType::WithIntro(_, track) | TrackType::Single(track) => {
let next = self
.channels
......@@ -121,6 +149,8 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
.fade_in(fade)
.looped()
.handle();
self.state.active_music_name = Some(name.clone());
self.state.active_music = Some(next.clone());
Some(next)
......@@ -180,8 +210,12 @@ impl<'w, 's, T: SuppliesAudio> MusicBox<'w, 's, T> {
/// Stop playing ambiance on the ambiance channel. The supplied tween will be used to fade out the track
/// before it ends
pub fn fade_out_ambiance(&mut self, fade: AudioTween) {
let handle = std::mem::replace(&mut self.state.active_ambiance, None)
let handle = self
.state
.active_ambiance
.take()
.and_then(|handle| self.audio_instances.get_mut(&handle));
if let Some(current) = handle {
current.stop(fade);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment