diff --git a/MLEM/Misc/SoundEffectInstanceHandler.cs b/MLEM/Misc/SoundEffectInstanceHandler.cs index 553f2d6..21ec057 100644 --- a/MLEM/Misc/SoundEffectInstanceHandler.cs +++ b/MLEM/Misc/SoundEffectInstanceHandler.cs @@ -13,6 +13,7 @@ namespace MLEM.Misc { public class SoundEffectInstanceHandler : GameComponent, IEnumerable { private readonly List playingSounds = new List(); + private AudioListener[] listeners; /// /// Creates a new sound effect instance handler with the given settings @@ -21,13 +22,14 @@ namespace MLEM.Misc { public SoundEffectInstanceHandler(Game game) : base(game) { } - /// + /// public override void Update(GameTime gameTime) { this.Update(); } /// /// Updates this sound effect handler and manages all of the objects in it. + /// If has been called, all sounds will additionally be updated in 3D space. /// This should be called each update frame. /// public void Update() { @@ -37,10 +39,20 @@ namespace MLEM.Misc { entry.Instance.Stop(true); entry.OnStopped?.Invoke(entry.Instance); this.playingSounds.RemoveAt(i); + } else { + entry.TryApply3D(this.listeners); } } } + /// + /// Sets the collection objects that should be listening to the sounds in this handler in 3D space. + /// If there are one or more listeners, this handler applies 3d effects to all sound effect instances that have been added to this handler along with an in automatically. + /// + public void SetListeners(params AudioListener[] listeners) { + this.listeners = listeners; + } + /// /// Pauses all of the sound effect instances that are currently playing /// @@ -57,17 +69,6 @@ namespace MLEM.Misc { entry.Instance.Resume(); } - /// - /// Applies 3d effects to all sound effect instances that have been added to this handler along with an - /// - /// The audio listener that 3d sound should be applied for - public void Apply3D(AudioListener listener) { - foreach (var entry in this.playingSounds) { - if (entry.Emitter != null) - entry.Instance.Apply3D(listener, entry.Emitter); - } - } - /// /// Adds a new to this handler. /// This also starts playing the instance. @@ -77,7 +78,9 @@ namespace MLEM.Misc { /// An optional audio emitter with which 3d sound can be applied /// The passed instance, for chaining public SoundEffectInstance Add(SoundEffectInstance instance, Action onStopped = null, AudioEmitter emitter = null) { - this.playingSounds.Add(new Entry(instance, onStopped, emitter)); + var entry = new Entry(instance, onStopped, emitter); + entry.TryApply3D(this.listeners); + this.playingSounds.Add(entry); instance.Play(); return instance; } @@ -128,6 +131,11 @@ namespace MLEM.Misc { this.Emitter = emitter; } + public void TryApply3D(AudioListener[] listeners) { + if (listeners != null && listeners.Length > 0) + this.Instance.Apply3D(listeners, this.Emitter); + } + } }