Files
crystall-punk-14/Content.Client/Electrocution/ElectrocutionHUDVisualizerSystem.cs

90 lines
3.3 KiB
C#
Raw Normal View History

2024-11-22 00:43:02 +01:00
using Content.Shared.Electrocution;
using Robust.Client.GameObjects;
using Robust.Client.Player;
using Robust.Shared.Player;
namespace Content.Client.Electrocution;
/// <summary>
2024-11-22 23:02:59 +01:00
/// Shows the Electrocution HUD to entities with the ShowElectrocutionHUDComponent.
2024-11-22 00:43:02 +01:00
/// </summary>
2024-11-22 23:39:05 +01:00
public sealed class ElectrocutionHUDVisualizerSystem : VisualizerSystem<ElectrocutionHUDVisualsComponent>
2024-11-22 00:43:02 +01:00
{
[Dependency] private readonly IPlayerManager _playerMan = default!;
[Dependency] private readonly SpriteSystem _sprite = default!;
2024-11-22 00:43:02 +01:00
public override void Initialize()
{
2024-11-22 23:02:59 +01:00
base.Initialize();
2024-11-22 00:43:02 +01:00
2024-11-22 23:02:59 +01:00
SubscribeLocalEvent<ShowElectrocutionHUDComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<ShowElectrocutionHUDComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<ShowElectrocutionHUDComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<ShowElectrocutionHUDComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);
2024-11-22 00:43:02 +01:00
}
2024-11-22 23:02:59 +01:00
private void OnPlayerAttached(Entity<ShowElectrocutionHUDComponent> ent, ref LocalPlayerAttachedEvent args)
2024-11-22 00:43:02 +01:00
{
2024-11-22 23:02:59 +01:00
ShowHUD();
2024-11-22 00:43:02 +01:00
}
2024-11-22 23:02:59 +01:00
private void OnPlayerDetached(Entity<ShowElectrocutionHUDComponent> ent, ref LocalPlayerDetachedEvent args)
2024-11-22 00:43:02 +01:00
{
2024-11-22 23:02:59 +01:00
RemoveHUD();
2024-11-22 00:43:02 +01:00
}
2024-11-22 23:02:59 +01:00
private void OnInit(Entity<ShowElectrocutionHUDComponent> ent, ref ComponentInit args)
2024-11-22 00:43:02 +01:00
{
if (_playerMan.LocalEntity == ent)
{
2024-11-22 23:02:59 +01:00
ShowHUD();
2024-11-22 00:43:02 +01:00
}
}
2024-11-22 23:02:59 +01:00
private void OnShutdown(Entity<ShowElectrocutionHUDComponent> ent, ref ComponentShutdown args)
2024-11-22 00:43:02 +01:00
{
if (_playerMan.LocalEntity == ent)
{
2024-11-22 23:02:59 +01:00
RemoveHUD();
2024-11-22 00:43:02 +01:00
}
}
2024-11-22 23:02:59 +01:00
// Show the HUD to the client.
// We have to look for all current entities that can be electrified and toggle the HUD layer on if they are.
private void ShowHUD()
2024-11-22 00:43:02 +01:00
{
2024-11-22 23:02:59 +01:00
var electrifiedQuery = AllEntityQuery<ElectrocutionHUDVisualsComponent, AppearanceComponent, SpriteComponent>();
while (electrifiedQuery.MoveNext(out var uid, out _, out var appearanceComp, out var spriteComp))
2024-11-22 00:43:02 +01:00
{
2024-11-22 23:02:59 +01:00
if (!AppearanceSystem.TryGetData<bool>(uid, ElectrifiedVisuals.IsElectrified, out var electrified, appearanceComp))
2024-11-22 00:43:02 +01:00
continue;
_sprite.LayerSetVisible((uid, spriteComp), ElectrifiedLayers.HUD, electrified);
2024-11-22 00:43:02 +01:00
}
}
2024-11-22 23:02:59 +01:00
// Remove the HUD from the client.
// Find all current entities that can be electrified and hide the HUD layer.
private void RemoveHUD()
2024-11-22 00:43:02 +01:00
{
2024-11-22 23:02:59 +01:00
var electrifiedQuery = AllEntityQuery<ElectrocutionHUDVisualsComponent, AppearanceComponent, SpriteComponent>();
while (electrifiedQuery.MoveNext(out var uid, out _, out _, out var spriteComp))
2024-11-22 00:43:02 +01:00
{
_sprite.LayerSetVisible((uid, spriteComp), ElectrifiedLayers.HUD, false);
2024-11-22 00:43:02 +01:00
}
}
2024-11-22 23:02:59 +01:00
// Toggle the HUD layer if an entity becomes (de-)electrified
protected override void OnAppearanceChange(EntityUid uid, ElectrocutionHUDVisualsComponent comp, ref AppearanceChangeEvent args)
2024-11-22 00:43:02 +01:00
{
if (args.Sprite == null)
return;
2024-11-22 23:02:59 +01:00
if (!AppearanceSystem.TryGetData<bool>(uid, ElectrifiedVisuals.IsElectrified, out var electrified, args.Component))
2024-11-22 00:43:02 +01:00
return;
var player = _playerMan.LocalEntity;
_sprite.LayerSetVisible((uid, args.Sprite), ElectrifiedLayers.HUD, electrified && HasComp<ShowElectrocutionHUDComponent>(player));
2024-11-22 00:43:02 +01:00
}
}