From 1b4373d56763c9bfe25f5bb51bdec8cbdc827384 Mon Sep 17 00:00:00 2001 From: c4llv07e <38111072+c4llv07e@users.noreply.github.com> Date: Wed, 26 Jul 2023 00:35:19 +0000 Subject: [PATCH] Fix radiation sound not working (#18282) * Fix radiation sound not working Signed-off-by: c4llv07e * Make Geiger sound local Signed-off-by: c4llv07e --------- Signed-off-by: c4llv07e --- .../Radiation/Systems/GeigerSystem.cs | 52 ------------------- .../Radiation/Systems/GeigerSystem.cs | 32 +++++++++++- 2 files changed, 30 insertions(+), 54 deletions(-) diff --git a/Content.Client/Radiation/Systems/GeigerSystem.cs b/Content.Client/Radiation/Systems/GeigerSystem.cs index 600c5860fb..f3adccf0fc 100644 --- a/Content.Client/Radiation/Systems/GeigerSystem.cs +++ b/Content.Client/Radiation/Systems/GeigerSystem.cs @@ -17,7 +17,6 @@ public sealed class GeigerSystem : SharedGeigerSystem public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnAttachedEntityChanged); SubscribeLocalEvent(OnHandleState); SubscribeLocalEvent(OnGetStatusMessage); } @@ -27,8 +26,6 @@ public sealed class GeigerSystem : SharedGeigerSystem if (args.Current is not GeigerComponentState state) return; - UpdateGeigerSound(uid, state.IsEnabled, state.User, state.DangerLevel, false, component); - component.CurrentRadiation = state.CurrentRadiation; component.DangerLevel = state.DangerLevel; component.IsEnabled = state.IsEnabled; @@ -43,53 +40,4 @@ public sealed class GeigerSystem : SharedGeigerSystem args.Controls.Add(new GeigerItemControl(component)); } - - private void OnAttachedEntityChanged(PlayerAttachSysMessage ev) - { - // need to go for each component known to client - // and update their geiger sound - foreach (var geiger in EntityQuery()) - { - ForceUpdateGeigerSound(geiger.Owner, geiger); - } - } - - private void ForceUpdateGeigerSound(EntityUid uid, GeigerComponent? component = null) - { - if (!Resolve(uid, ref component)) - return; - UpdateGeigerSound(uid, component.IsEnabled, component.User, component.DangerLevel, true, component); - } - - private void UpdateGeigerSound(EntityUid uid, bool isEnabled, EntityUid? user, - GeigerDangerLevel dangerLevel, bool force = false, GeigerComponent? component = null) - { - if (!Resolve(uid, ref component)) - return; - - // check if we even need to update sound - if (!force && isEnabled == component.IsEnabled && - user == component.User && dangerLevel == component.DangerLevel) - { - return; - } - - component.Stream?.Stop(); - - if (!isEnabled || user == null) - return; - if (!component.Sounds.TryGetValue(dangerLevel, out var sounds)) - return; - - // check that that local player controls entity that is holding geiger counter - if (_playerManager.LocalPlayer == null) - return; - var attachedEnt = _playerManager.LocalPlayer.Session.AttachedEntity; - if (attachedEnt != user) - return; - - var sound = _audio.GetSound(sounds); - var param = sounds.Params.WithLoop(true).WithVolume(-4f); - component.Stream = _audio.Play(sound, Filter.Local(), uid, false, param); - } } diff --git a/Content.Server/Radiation/Systems/GeigerSystem.cs b/Content.Server/Radiation/Systems/GeigerSystem.cs index 720769cf18..fdbb6e353c 100644 --- a/Content.Server/Radiation/Systems/GeigerSystem.cs +++ b/Content.Server/Radiation/Systems/GeigerSystem.cs @@ -6,6 +6,9 @@ using Content.Shared.Inventory.Events; using Content.Shared.Radiation.Components; using Content.Shared.Radiation.Systems; using Robust.Shared.GameStates; +using Robust.Shared.Player; +using Robust.Server.GameObjects; +using Robust.Server.Player; namespace Content.Server.Radiation.Systems; @@ -13,6 +16,8 @@ public sealed class GeigerSystem : SharedGeigerSystem { [Dependency] private readonly SharedAppearanceSystem _appearance = default!; [Dependency] private readonly RadiationSystem _radiation = default!; + [Dependency] private readonly AudioSystem _audio = default!; + [Dependency] private readonly IPlayerManager _player = default!; private static readonly float ApproxEqual = 0.01f; @@ -107,6 +112,7 @@ public sealed class GeigerSystem : SharedGeigerSystem if (curLevel != newLevel) { UpdateAppearance(uid, component); + UpdateSound(uid, component); } Dirty(component); @@ -119,6 +125,7 @@ public sealed class GeigerSystem : SharedGeigerSystem component.User = user; Dirty(component); + UpdateSound(component.Owner, component); } private void SetEnabled(EntityUid uid, GeigerComponent component, bool isEnabled) @@ -136,6 +143,7 @@ public sealed class GeigerSystem : SharedGeigerSystem _radiation.SetCanReceive(uid, isEnabled); UpdateAppearance(uid, component); + UpdateSound(uid, component); Dirty(component); } @@ -149,6 +157,28 @@ public sealed class GeigerSystem : SharedGeigerSystem _appearance.SetData(uid, GeigerVisuals.DangerLevel, component.DangerLevel, appearance); } + private void UpdateSound(EntityUid uid, GeigerComponent? component = null) + { + if (!Resolve(uid, ref component, false)) + return; + + component.Stream?.Stop(); + + if (!component.Sounds.TryGetValue(component.DangerLevel, out var sounds)) + return; + + if (component.User == null) + return; + + if (!_player.TryGetSessionByEntity(component.User.Value, out var session)) + return; + + var sound = _audio.GetSound(sounds); + var param = sounds.Params.WithLoop(true).WithVolume(-4f); + + component.Stream = _audio.PlayGlobal(sound, session, param); + } + public static GeigerDangerLevel RadsToLevel(float rads) { return rads switch @@ -161,5 +191,3 @@ public sealed class GeigerSystem : SharedGeigerSystem }; } } - -