Geiger counter (#12082)
This commit is contained in:
95
Content.Client/Radiation/Systems/GeigerSystem.cs
Normal file
95
Content.Client/Radiation/Systems/GeigerSystem.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using Content.Client.Items;
|
||||
using Content.Client.Radiation.UI;
|
||||
using Content.Shared.Radiation.Components;
|
||||
using Content.Shared.Radiation.Systems;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Client.Radiation.Systems;
|
||||
|
||||
public sealed class GeigerSystem : SharedGeigerSystem
|
||||
{
|
||||
[Dependency] private readonly AudioSystem _audio = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<PlayerAttachSysMessage>(OnAttachedEntityChanged);
|
||||
SubscribeLocalEvent<GeigerComponent, ComponentHandleState>(OnHandleState);
|
||||
SubscribeLocalEvent<GeigerComponent, ItemStatusCollectMessage>(OnGetStatusMessage);
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, GeigerComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
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;
|
||||
component.User = state.User;
|
||||
component.UiUpdateNeeded = true;
|
||||
}
|
||||
|
||||
private void OnGetStatusMessage(EntityUid uid, GeigerComponent component, ItemStatusCollectMessage args)
|
||||
{
|
||||
if (!component.ShowControl)
|
||||
return;
|
||||
|
||||
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<GeigerComponent>())
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
53
Content.Client/Radiation/UI/GeigerItemControl.cs
Normal file
53
Content.Client/Radiation/UI/GeigerItemControl.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Content.Client.Message;
|
||||
using Content.Client.Stylesheets;
|
||||
using Content.Shared.Radiation.Components;
|
||||
using Content.Shared.Radiation.Systems;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Radiation.UI;
|
||||
|
||||
public sealed class GeigerItemControl : Control
|
||||
{
|
||||
private readonly GeigerComponent _component;
|
||||
private readonly RichTextLabel _label;
|
||||
|
||||
public GeigerItemControl(GeigerComponent component)
|
||||
{
|
||||
_component = component;
|
||||
_label = new RichTextLabel { StyleClasses = { StyleNano.StyleClassItemStatus } };
|
||||
AddChild(_label);
|
||||
|
||||
Update();
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
base.FrameUpdate(args);
|
||||
|
||||
if (!_component.UiUpdateNeeded)
|
||||
return;
|
||||
Update();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
string msg;
|
||||
if (_component.IsEnabled)
|
||||
{
|
||||
var color = SharedGeigerSystem.LevelToColor(_component.DangerLevel);
|
||||
var currentRads = _component.CurrentRadiation;
|
||||
var rads = currentRads.ToString("N1");
|
||||
msg = Loc.GetString("geiger-item-control-status",
|
||||
("rads", rads), ("color", color));
|
||||
}
|
||||
else
|
||||
{
|
||||
msg = Loc.GetString("geiger-item-control-disabled");
|
||||
}
|
||||
|
||||
_label.SetMarkup(msg);
|
||||
_component.UiUpdateNeeded = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user