1
0

ElectrocutionHUDVisualizerSystem.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Content.Shared.Electrocution;
  2. using Robust.Client.GameObjects;
  3. using Robust.Client.Player;
  4. using Robust.Shared.Player;
  5. namespace Content.Client.Electrocution;
  6. /// <summary>
  7. /// Shows the Electrocution HUD to entities with the ShowElectrocutionHUDComponent.
  8. /// </summary>
  9. public sealed class ElectrocutionHUDVisualizerSystem : VisualizerSystem<ElectrocutionHUDVisualsComponent>
  10. {
  11. [Dependency] private readonly IPlayerManager _playerMan = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<ShowElectrocutionHUDComponent, ComponentInit>(OnInit);
  16. SubscribeLocalEvent<ShowElectrocutionHUDComponent, ComponentShutdown>(OnShutdown);
  17. SubscribeLocalEvent<ShowElectrocutionHUDComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
  18. SubscribeLocalEvent<ShowElectrocutionHUDComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);
  19. }
  20. private void OnPlayerAttached(Entity<ShowElectrocutionHUDComponent> ent, ref LocalPlayerAttachedEvent args)
  21. {
  22. ShowHUD();
  23. }
  24. private void OnPlayerDetached(Entity<ShowElectrocutionHUDComponent> ent, ref LocalPlayerDetachedEvent args)
  25. {
  26. RemoveHUD();
  27. }
  28. private void OnInit(Entity<ShowElectrocutionHUDComponent> ent, ref ComponentInit args)
  29. {
  30. if (_playerMan.LocalEntity == ent)
  31. {
  32. ShowHUD();
  33. }
  34. }
  35. private void OnShutdown(Entity<ShowElectrocutionHUDComponent> ent, ref ComponentShutdown args)
  36. {
  37. if (_playerMan.LocalEntity == ent)
  38. {
  39. RemoveHUD();
  40. }
  41. }
  42. // Show the HUD to the client.
  43. // We have to look for all current entities that can be electrified and toggle the HUD layer on if they are.
  44. private void ShowHUD()
  45. {
  46. var electrifiedQuery = AllEntityQuery<ElectrocutionHUDVisualsComponent, AppearanceComponent, SpriteComponent>();
  47. while (electrifiedQuery.MoveNext(out var uid, out var _, out var appearanceComp, out var spriteComp))
  48. {
  49. if (!AppearanceSystem.TryGetData<bool>(uid, ElectrifiedVisuals.IsElectrified, out var electrified, appearanceComp))
  50. continue;
  51. if (electrified)
  52. spriteComp.LayerSetVisible(ElectrifiedLayers.HUD, true);
  53. else
  54. spriteComp.LayerSetVisible(ElectrifiedLayers.HUD, false);
  55. }
  56. }
  57. // Remove the HUD from the client.
  58. // Find all current entities that can be electrified and hide the HUD layer.
  59. private void RemoveHUD()
  60. {
  61. var electrifiedQuery = AllEntityQuery<ElectrocutionHUDVisualsComponent, AppearanceComponent, SpriteComponent>();
  62. while (electrifiedQuery.MoveNext(out var uid, out var _, out var appearanceComp, out var spriteComp))
  63. {
  64. spriteComp.LayerSetVisible(ElectrifiedLayers.HUD, false);
  65. }
  66. }
  67. // Toggle the HUD layer if an entity becomes (de-)electrified
  68. protected override void OnAppearanceChange(EntityUid uid, ElectrocutionHUDVisualsComponent comp, ref AppearanceChangeEvent args)
  69. {
  70. if (args.Sprite == null)
  71. return;
  72. if (!AppearanceSystem.TryGetData<bool>(uid, ElectrifiedVisuals.IsElectrified, out var electrified, args.Component))
  73. return;
  74. var player = _playerMan.LocalEntity;
  75. if (electrified && HasComp<ShowElectrocutionHUDComponent>(player))
  76. args.Sprite.LayerSetVisible(ElectrifiedLayers.HUD, true);
  77. else
  78. args.Sprite.LayerSetVisible(ElectrifiedLayers.HUD, false);
  79. }
  80. }