mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-29 15:58:33 +01:00
made listeners auto-update in SoundEffectInstanceHandler
This commit is contained in:
parent
c85666502c
commit
182f8eff7a
1 changed files with 21 additions and 13 deletions
|
@ -13,6 +13,7 @@ namespace MLEM.Misc {
|
||||||
public class SoundEffectInstanceHandler : GameComponent, IEnumerable<SoundEffectInstance> {
|
public class SoundEffectInstanceHandler : GameComponent, IEnumerable<SoundEffectInstance> {
|
||||||
|
|
||||||
private readonly List<Entry> playingSounds = new List<Entry>();
|
private readonly List<Entry> playingSounds = new List<Entry>();
|
||||||
|
private AudioListener[] listeners;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new sound effect instance handler with the given settings
|
/// 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 SoundEffectInstanceHandler(Game game) : base(game) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc cref="Update()"/>
|
||||||
public override void Update(GameTime gameTime) {
|
public override void Update(GameTime gameTime) {
|
||||||
this.Update();
|
this.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates this sound effect handler and manages all of the <see cref="SoundEffectInstance"/> objects in it.
|
/// Updates this sound effect handler and manages all of the <see cref="SoundEffectInstance"/> objects in it.
|
||||||
|
/// If <see cref="SetListeners"/> has been called, all sounds will additionally be updated in 3D space.
|
||||||
/// This should be called each update frame.
|
/// This should be called each update frame.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Update() {
|
public void Update() {
|
||||||
|
@ -37,10 +39,20 @@ namespace MLEM.Misc {
|
||||||
entry.Instance.Stop(true);
|
entry.Instance.Stop(true);
|
||||||
entry.OnStopped?.Invoke(entry.Instance);
|
entry.OnStopped?.Invoke(entry.Instance);
|
||||||
this.playingSounds.RemoveAt(i);
|
this.playingSounds.RemoveAt(i);
|
||||||
|
} else {
|
||||||
|
entry.TryApply3D(this.listeners);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the collection <see cref="AudioListener"/> 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 <see cref="AudioEmitter"/> in <see cref="Update(Microsoft.Xna.Framework.GameTime)"/> automatically.
|
||||||
|
/// </summary>
|
||||||
|
public void SetListeners(params AudioListener[] listeners) {
|
||||||
|
this.listeners = listeners;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pauses all of the sound effect instances that are currently playing
|
/// Pauses all of the sound effect instances that are currently playing
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -57,17 +69,6 @@ namespace MLEM.Misc {
|
||||||
entry.Instance.Resume();
|
entry.Instance.Resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Applies 3d effects to all sound effect instances that have been added to this handler along with an <see cref="AudioEmitter"/>
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="listener">The audio listener that 3d sound should be applied for</param>
|
|
||||||
public void Apply3D(AudioListener listener) {
|
|
||||||
foreach (var entry in this.playingSounds) {
|
|
||||||
if (entry.Emitter != null)
|
|
||||||
entry.Instance.Apply3D(listener, entry.Emitter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a new <see cref="SoundEffectInstance"/> to this handler.
|
/// Adds a new <see cref="SoundEffectInstance"/> to this handler.
|
||||||
/// This also starts playing the instance.
|
/// This also starts playing the instance.
|
||||||
|
@ -77,7 +78,9 @@ namespace MLEM.Misc {
|
||||||
/// <param name="emitter">An optional audio emitter with which 3d sound can be applied</param>
|
/// <param name="emitter">An optional audio emitter with which 3d sound can be applied</param>
|
||||||
/// <returns>The passed instance, for chaining</returns>
|
/// <returns>The passed instance, for chaining</returns>
|
||||||
public SoundEffectInstance Add(SoundEffectInstance instance, Action<SoundEffectInstance> onStopped = null, AudioEmitter emitter = null) {
|
public SoundEffectInstance Add(SoundEffectInstance instance, Action<SoundEffectInstance> 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();
|
instance.Play();
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
@ -128,6 +131,11 @@ namespace MLEM.Misc {
|
||||||
this.Emitter = emitter;
|
this.Emitter = emitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void TryApply3D(AudioListener[] listeners) {
|
||||||
|
if (listeners != null && listeners.Length > 0)
|
||||||
|
this.Instance.Apply3D(listeners, this.Emitter);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue