Refactor SoundComponent for stopping sounds.

This commit is contained in:
FL-OZ
2020-05-01 17:17:05 -05:00
parent a5fa184765
commit a65d60dc2c

View File

@@ -1,5 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Content.Shared.GameObjects.Components.Sound; using Content.Shared.GameObjects.Components.Sound;
using Microsoft.DiaSymReader;
using Robust.Client.GameObjects.EntitySystems; using Robust.Client.GameObjects.EntitySystems;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
@@ -15,7 +17,7 @@ namespace Content.Client.GameObjects.Components.Sound
[RegisterComponent] [RegisterComponent]
public class SoundComponent : SharedSoundComponent public class SoundComponent : SharedSoundComponent
{ {
private readonly List<ScheduledSound> _schedules = new List<ScheduledSound>(); private readonly Dictionary<ScheduledSound, IPlayingAudioStream> _audioStreams = new Dictionary<ScheduledSound, IPlayingAudioStream>();
private AudioSystem _audioSystem; private AudioSystem _audioSystem;
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IRobustRandom _random; [Dependency] private readonly IRobustRandom _random;
@@ -23,26 +25,27 @@ namespace Content.Client.GameObjects.Components.Sound
public override void StopAllSounds() public override void StopAllSounds()
{ {
foreach (var schedule in _schedules) foreach (var kvp in _audioStreams)
{ {
schedule.Play = false; kvp.Key.Play = false;
kvp.Value.Stop();
} }
_schedules.Clear(); _audioStreams.Clear();
} }
public override void StopScheduledSound(string filename) public override void StopScheduledSound(string filename)
{ {
foreach (var schedule in _schedules.ToArray()) foreach (var kvp in _audioStreams)
{ {
if (schedule.Filename != filename) continue; if (kvp.Key.Filename != filename) continue;
schedule.Play = false; kvp.Key.Play = false;
_schedules.Remove(schedule); kvp.Value.Stop();
_audioStreams.Remove(kvp.Key);
} }
} }
public override void AddScheduledSound(ScheduledSound schedule) public override void AddScheduledSound(ScheduledSound schedule)
{ {
_schedules.Add(schedule);
Play(schedule); Play(schedule);
} }
@@ -54,16 +57,11 @@ namespace Content.Client.GameObjects.Components.Sound
{ {
if (!schedule.Play) return; // We make sure this hasn't changed. if (!schedule.Play) return; // We make sure this hasn't changed.
if (_audioSystem == null) _audioSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>(); if (_audioSystem == null) _audioSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>();
_audioSystem.Play(schedule.Filename, Owner, schedule.AudioParams); _audioStreams.Add(schedule,_audioSystem.Play(schedule.Filename, Owner, schedule.AudioParams));
if (schedule.Times == 0) if (schedule.Times == 0) return;
{
_schedules.Remove(schedule);
return;
}
if (schedule.Times > 0) if (schedule.Times > 0) schedule.Times--;
schedule.Times--;
Play(schedule); Play(schedule);
}); });