* Engine namespace changes.
* Automated remove redundant using statements.
* Simplified Graphics namespace.
* Apparently the container system stores full type names in the map file.😞 This updates those names.
* API Changes to LocalizationManager.LoadCulture.
* Update submodule to v0.3.2
110 lines
3.5 KiB
C#
110 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using Content.Shared.GameObjects.Components.Sound;
|
|
using Content.Shared.Physics;
|
|
using Robust.Client.GameObjects;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Network;
|
|
using Robust.Shared.Players;
|
|
using Robust.Shared.Random;
|
|
using Robust.Shared.Serialization;
|
|
|
|
namespace Content.Client.GameObjects.Components.Sound
|
|
{
|
|
[RegisterComponent]
|
|
public class LoopingSoundComponent : SharedLoopingSoundComponent
|
|
{
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
|
|
|
private readonly Dictionary<ScheduledSound, IPlayingAudioStream> _audioStreams = new();
|
|
private AudioSystem _audioSystem;
|
|
|
|
public override void StopAllSounds()
|
|
{
|
|
foreach (var kvp in _audioStreams)
|
|
{
|
|
kvp.Key.Play = false;
|
|
kvp.Value.Stop();
|
|
}
|
|
_audioStreams.Clear();
|
|
}
|
|
|
|
public override void StopScheduledSound(string filename)
|
|
{
|
|
foreach (var kvp in _audioStreams)
|
|
{
|
|
if (kvp.Key.Filename != filename) continue;
|
|
kvp.Key.Play = false;
|
|
kvp.Value.Stop();
|
|
_audioStreams.Remove(kvp.Key);
|
|
}
|
|
}
|
|
|
|
public override void AddScheduledSound(ScheduledSound schedule)
|
|
{
|
|
Play(schedule);
|
|
}
|
|
|
|
public void Play(ScheduledSound schedule)
|
|
{
|
|
if (!schedule.Play) return;
|
|
|
|
Owner.SpawnTimer((int) schedule.Delay + (_random.Next((int) schedule.RandomDelay)),() =>
|
|
{
|
|
if (!schedule.Play) return; // We make sure this hasn't changed.
|
|
if (_audioSystem == null) _audioSystem = EntitySystem.Get<AudioSystem>();
|
|
|
|
if (!_audioStreams.ContainsKey(schedule))
|
|
{
|
|
_audioStreams.Add(schedule,_audioSystem.Play(schedule.Filename, Owner, schedule.AudioParams));
|
|
}
|
|
else
|
|
{
|
|
_audioStreams[schedule] = _audioSystem.Play(schedule.Filename, Owner, schedule.AudioParams);
|
|
}
|
|
|
|
if (schedule.Times == 0) return;
|
|
|
|
if (schedule.Times > 0) schedule.Times--;
|
|
|
|
Play(schedule);
|
|
});
|
|
}
|
|
|
|
public override void HandleNetworkMessage(ComponentMessage message, INetChannel channel, ICommonSession session = null)
|
|
{
|
|
base.HandleNetworkMessage(message, channel, session);
|
|
switch (message)
|
|
{
|
|
case ScheduledSoundMessage msg:
|
|
AddScheduledSound(msg.Schedule);
|
|
break;
|
|
|
|
case StopSoundScheduleMessage msg:
|
|
StopScheduledSound(msg.Filename);
|
|
break;
|
|
|
|
case StopAllSoundsMessage _:
|
|
StopAllSounds();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
if (EntitySystem.TryGet(out _audioSystem)) _audioSystem.OcclusionCollisionMask = (int) CollisionGroup.Impassable;
|
|
}
|
|
|
|
public override void ExposeData(ObjectSerializer serializer)
|
|
{
|
|
base.ExposeData(serializer);
|
|
|
|
serializer.DataReadFunction(
|
|
"schedules",
|
|
new List<ScheduledSound>(),
|
|
schedules => schedules.ForEach(AddScheduledSound));
|
|
}
|
|
}
|
|
}
|