diff --git a/MLEM.Data/DataTextureAtlas.cs b/MLEM.Data/DataTextureAtlas.cs index 277bd4e..2c64a6c 100644 --- a/MLEM.Data/DataTextureAtlas.cs +++ b/MLEM.Data/DataTextureAtlas.cs @@ -5,7 +5,6 @@ using System.Text.RegularExpressions; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; -using MLEM.Extensions; using MLEM.Textures; namespace MLEM.Data { diff --git a/MLEM/Misc/SoundEffectInfo.cs b/MLEM/Misc/SoundEffectInfo.cs index 1a4d649..4ec3632 100644 --- a/MLEM/Misc/SoundEffectInfo.cs +++ b/MLEM/Misc/SoundEffectInfo.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; namespace MLEM.Misc { /// /// A sound effect info is a wrapper around that additionally stores , and information. - /// Additionally, a can be created using and a 3D sound can be played using . + /// Additionally, a can be created using . /// public class SoundEffectInfo { @@ -64,99 +59,4 @@ namespace MLEM.Misc { } } - - /// - /// A simple class that handles automatically removing and disposing objects once they are done playing to free up the audio source for new sounds. - /// Additionally, a callback can be registered that is invoked when the finishes playing. - /// Note that an object of this class can be added to a using . - /// - public class SoundEffectInstanceHandler : GameComponent, IEnumerable { - - private readonly List playingSounds = new List(); - - /// - /// Creates a new sound effect instance handler with the given settings - /// - /// The game instance - 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. - /// This should be called each update frame. - /// - public void Update() { - for (var i = this.playingSounds.Count - 1; i >= 0; i--) { - var entry = this.playingSounds[i]; - if (entry.Instance.IsDisposed || entry.Instance.State == SoundState.Stopped) { - entry.Instance.Stop(true); - entry.OnStopped?.Invoke(entry.Instance); - this.playingSounds.RemoveAt(i); - } - } - } - - /// - /// Adds a new to this handler. - /// This also starts playing the instance. - /// - /// The instance to add - /// The function that should be invoked when this instance stops playing, defaults to null - /// The passed instance, for chaining - public SoundEffectInstance Add(SoundEffectInstance instance, Action onStopped = null) { - this.playingSounds.Add(new Entry(instance, onStopped)); - instance.Play(); - return instance; - } - - /// - /// Adds a new to this handler. - /// This also starts playing the created instance. - /// - /// The info for which to add a - /// The function that should be invoked when this instance stops playing, defaults to null - /// The newly created - public SoundEffectInstance Add(SoundEffectInfo info, Action onStopped = null) { - return this.Add(info.CreateInstance(), onStopped); - } - - /// - /// Adds a new to this handler. - /// This also starts playing the created instance. - /// - /// The sound for which to add a - /// The function that should be invoked when this instance stops playing, defaults to null - /// The newly created - public SoundEffectInstance Add(SoundEffect effect, Action onStopped = null) { - return this.Add(effect.CreateInstance(), onStopped); - } - - /// - public IEnumerator GetEnumerator() { - foreach (var sound in this.playingSounds) - yield return sound.Instance; - } - - IEnumerator IEnumerable.GetEnumerator() { - return this.GetEnumerator(); - } - - private readonly struct Entry { - - public readonly SoundEffectInstance Instance; - public readonly Action OnStopped; - - public Entry(SoundEffectInstance instance, Action onStopped) { - this.Instance = instance; - this.OnStopped = onStopped; - } - - } - - } } \ No newline at end of file diff --git a/MLEM/Misc/SoundEffectInstanceHandler.cs b/MLEM/Misc/SoundEffectInstanceHandler.cs new file mode 100644 index 0000000..553f2d6 --- /dev/null +++ b/MLEM/Misc/SoundEffectInstanceHandler.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Audio; + +namespace MLEM.Misc { + /// + /// A simple class that handles automatically removing and disposing objects once they are done playing to free up the audio source for new sounds. + /// Additionally, a callback can be registered that is invoked when the finishes playing. + /// Note that an object of this class can be added to a using . + /// + public class SoundEffectInstanceHandler : GameComponent, IEnumerable { + + private readonly List playingSounds = new List(); + + /// + /// Creates a new sound effect instance handler with the given settings + /// + /// The game instance + 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. + /// This should be called each update frame. + /// + public void Update() { + for (var i = this.playingSounds.Count - 1; i >= 0; i--) { + var entry = this.playingSounds[i]; + if (entry.Instance.IsDisposed || entry.Instance.State == SoundState.Stopped) { + entry.Instance.Stop(true); + entry.OnStopped?.Invoke(entry.Instance); + this.playingSounds.RemoveAt(i); + } + } + } + + /// + /// Pauses all of the sound effect instances that are currently playing + /// + public void Pause() { + foreach (var entry in this.playingSounds) + entry.Instance.Pause(); + } + + /// + /// Resumes all of the sound effect instances in this handler + /// + public void Resume() { + foreach (var entry in this.playingSounds) + 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. + /// + /// The instance to add + /// The function that should be invoked when this instance stops playing, defaults to null + /// 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)); + instance.Play(); + return instance; + } + + /// + /// Adds a new to this handler. + /// This also starts playing the created instance. + /// + /// The info for which to add a + /// The function that should be invoked when this instance stops playing, defaults to null + /// An optional audio emitter with which 3d sound can be applied + /// The newly created + public SoundEffectInstance Add(SoundEffectInfo info, Action onStopped = null, AudioEmitter emitter = null) { + return this.Add(info.CreateInstance(), onStopped, emitter); + } + + /// + /// Adds a new to this handler. + /// This also starts playing the created instance. + /// + /// The sound for which to add a + /// The function that should be invoked when this instance stops playing, defaults to null + /// An optional audio emitter with which 3d sound can be applied + /// The newly created + public SoundEffectInstance Add(SoundEffect effect, Action onStopped = null, AudioEmitter emitter = null) { + return this.Add(effect.CreateInstance(), onStopped, emitter); + } + + /// + public IEnumerator GetEnumerator() { + foreach (var sound in this.playingSounds) + yield return sound.Instance; + } + + IEnumerator IEnumerable.GetEnumerator() { + return this.GetEnumerator(); + } + + private readonly struct Entry { + + public readonly SoundEffectInstance Instance; + public readonly Action OnStopped; + public readonly AudioEmitter Emitter; + + public Entry(SoundEffectInstance instance, Action onStopped, AudioEmitter emitter) { + this.Instance = instance; + this.OnStopped = onStopped; + this.Emitter = emitter; + } + + } + + } +} \ No newline at end of file