From 8a5b7f51463433b8f06245b58658485f1a378d4c Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Fri, 5 Feb 2021 07:23:36 +0100 Subject: [PATCH] Add AudioSystem extension methods for the client and server (#1532) --- .../GameObjects/EntitySystems/AudioSystem.cs | 72 +++++++++++++++++++ .../GameObjects/EntitySystems/AudioSystem.cs | 56 +++++++++++++-- 2 files changed, 124 insertions(+), 4 deletions(-) diff --git a/Robust.Client/GameObjects/EntitySystems/AudioSystem.cs b/Robust.Client/GameObjects/EntitySystems/AudioSystem.cs index 832434e852..375167ee0e 100644 --- a/Robust.Client/GameObjects/EntitySystems/AudioSystem.cs +++ b/Robust.Client/GameObjects/EntitySystems/AudioSystem.cs @@ -356,4 +356,76 @@ namespace Robust.Client.GameObjects.EntitySystems event Action PlaybackDone; } + + public static class AudioSystemExtensions + { + + /// + /// Play an audio file following an entity. + /// + /// The resource path to the OGG Vorbis file to play. + /// The entity "emitting" the audio. + /// + /// A pre-fetched instance of to use, can be null. + public static IPlayingAudioStream? Play( + this IEntity entity, + string filename, + AudioParams? audioParams, + AudioSystem? audioSystem = null) + { + audioSystem ??= EntitySystem.Get(); + return audioSystem.Play(filename, entity, audioParams); + } + + /// + /// Play an audio stream following an entity. + /// + /// The audio stream to play. + /// The entity "emitting" the audio. + /// + /// A pre-fetched instance of to use, can be null. + public static IPlayingAudioStream? Play( + this IEntity entity, + AudioStream stream, + AudioParams? audioParams = null, + AudioSystem? audioSystem = null) + { + audioSystem ??= EntitySystem.Get(); + return audioSystem.Play(stream, entity, audioParams); + } + + /// + /// Play an audio file at a static position. + /// + /// The resource path to the OGG Vorbis file to play. + /// The coordinates at which to play the audio. + /// + /// A pre-fetched instance of to use, can be null. + public static IPlayingAudioStream? Play( + this EntityCoordinates coordinates, + string filename, + AudioParams? audioParams = null, + AudioSystem? audioSystem = null) + { + audioSystem ??= EntitySystem.Get(); + return audioSystem.Play(filename, coordinates, audioParams); + } + + /// + /// Play an audio stream at a static position. + /// + /// The audio stream to play. + /// The coordinates at which to play the audio. + /// + /// A pre-fetched instance of to use, can be null. + public static IPlayingAudioStream? Play( + this EntityCoordinates coordinates, + AudioStream stream, + AudioParams? audioParams = null, + AudioSystem? audioSystem = null) + { + audioSystem ??= EntitySystem.Get(); + return audioSystem.Play(stream, coordinates, audioParams); + } + } } diff --git a/Robust.Server/GameObjects/EntitySystems/AudioSystem.cs b/Robust.Server/GameObjects/EntitySystems/AudioSystem.cs index 7a33ac2a51..e21fb3164f 100644 --- a/Robust.Server/GameObjects/EntitySystems/AudioSystem.cs +++ b/Robust.Server/GameObjects/EntitySystems/AudioSystem.cs @@ -7,6 +7,7 @@ using Robust.Shared.GameObjects.Systems; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; +using static Robust.Server.GameObjects.EntitySystems.AudioSystem; namespace Robust.Server.GameObjects.EntitySystems { @@ -43,13 +44,15 @@ namespace Robust.Server.GameObjects.EntitySystems Identifier = id }; - if(sessions == null) + if (sessions == null) RaiseNetworkEvent(msg); else + { foreach (var session in sessions) { RaiseNetworkEvent(msg, session.ConnectedClient); } + } } private uint CacheIdentifier() @@ -90,7 +93,7 @@ namespace Robust.Server.GameObjects.EntitySystems players.RemoveAt(i); continue; } - + RaiseNetworkEvent(msg, player.ConnectedClient); } @@ -135,7 +138,7 @@ namespace Robust.Server.GameObjects.EntitySystems players.RemoveAt(i); continue; } - + RaiseNetworkEvent(msg, player.ConnectedClient); } @@ -177,7 +180,7 @@ namespace Robust.Server.GameObjects.EntitySystems players.RemoveAt(i); continue; } - + RaiseNetworkEvent(msg, player.ConnectedClient); } @@ -223,4 +226,49 @@ namespace Robust.Server.GameObjects.EntitySystems #endregion } + + public static class AudioSystemExtensions + { + /// + /// Play an audio file following an entity. + /// + /// The resource path to the OGG Vorbis file to play. + /// The entity "emitting" the audio. + /// + /// The max range at which the audio will be heard. Less than or equal to 0 to send to every player. + /// Sessions that won't receive the audio message. + /// A pre-fetched instance of to use, can be null. + public static void PlaySoundFrom( + this IEntity entity, + string filename, + AudioParams? audioParams = null, + int range = AudioDistanceRange, + IPlayerSession? excludedSession = null, + AudioSystem? audioSystem = null) + { + audioSystem ??= EntitySystem.Get(); + audioSystem.PlayFromEntity(filename, entity, audioParams, range, excludedSession); + } + + /// + /// Play an audio file at a static position. + /// + /// The resource path to the OGG Vorbis file to play. + /// The coordinates at which to play the audio. + /// + /// The max range at which the audio will be heard. Less than or equal to 0 to send to every player. + /// Session that won't receive the audio message. + /// A pre-fetched instance of to use, can be null. + public static void PlaySoundFrom( + this EntityCoordinates coordinates, + string filename, + AudioParams? audioParams = null, + int range = AudioDistanceRange, + IPlayerSession? excludedSession = null, + AudioSystem? audioSystem = null) + { + audioSystem ??= EntitySystem.Get(); + audioSystem.PlayAtCoords(filename, coordinates, audioParams, range, excludedSession); + } + } }