1
0

GasValveSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Content.Server.Atmos.Piping.Binary.Components;
  2. using Content.Server.NodeContainer;
  3. using Content.Server.NodeContainer.EntitySystems;
  4. using Content.Server.NodeContainer.Nodes;
  5. using Content.Shared.Atmos.Piping;
  6. using Content.Shared.Audio;
  7. using Content.Shared.Examine;
  8. using Content.Shared.Interaction;
  9. using JetBrains.Annotations;
  10. using Robust.Shared.Audio;
  11. using Robust.Shared.Audio.Systems;
  12. using Robust.Shared.Player;
  13. namespace Content.Server.Atmos.Piping.Binary.EntitySystems
  14. {
  15. [UsedImplicitly]
  16. public sealed class GasValveSystem : EntitySystem
  17. {
  18. [Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
  19. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  20. [Dependency] private readonly SharedAudioSystem _audio = default!;
  21. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<GasValveComponent, ComponentStartup>(OnStartup);
  26. SubscribeLocalEvent<GasValveComponent, ActivateInWorldEvent>(OnActivate);
  27. SubscribeLocalEvent<GasValveComponent, ExaminedEvent>(OnExamined);
  28. }
  29. private void OnExamined(Entity<GasValveComponent> ent, ref ExaminedEvent args)
  30. {
  31. var valve = ent.Comp;
  32. if (!Comp<TransformComponent>(ent).Anchored || !args.IsInDetailsRange) // Not anchored? Out of range? No status.
  33. return;
  34. if (Loc.TryGetString("gas-valve-system-examined", out var str,
  35. ("statusColor", valve.Open ? "green" : "orange"),
  36. ("open", valve.Open)))
  37. {
  38. args.PushMarkup(str);
  39. }
  40. }
  41. private void OnStartup(EntityUid uid, GasValveComponent component, ComponentStartup args)
  42. {
  43. // We call set in startup so it sets the appearance, node state, etc.
  44. Set(uid, component, component.Open);
  45. }
  46. private void OnActivate(EntityUid uid, GasValveComponent component, ActivateInWorldEvent args)
  47. {
  48. if (args.Handled || !args.Complex)
  49. return;
  50. Toggle(uid, component);
  51. _audio.PlayPvs(component.ValveSound, uid, AudioParams.Default.WithVariation(0.25f));
  52. args.Handled = true;
  53. }
  54. public void Set(EntityUid uid, GasValveComponent component, bool value)
  55. {
  56. component.Open = value;
  57. if (_nodeContainer.TryGetNodes(uid, component.InletName, component.OutletName, out PipeNode? inlet, out PipeNode? outlet))
  58. {
  59. if (TryComp<AppearanceComponent>(uid, out var appearance))
  60. {
  61. _appearance.SetData(uid, FilterVisuals.Enabled, component.Open, appearance);
  62. }
  63. if (component.Open)
  64. {
  65. inlet.AddAlwaysReachable(outlet);
  66. outlet.AddAlwaysReachable(inlet);
  67. _ambientSoundSystem.SetAmbience(uid, true);
  68. }
  69. else
  70. {
  71. inlet.RemoveAlwaysReachable(outlet);
  72. outlet.RemoveAlwaysReachable(inlet);
  73. _ambientSoundSystem.SetAmbience(uid, false);
  74. }
  75. }
  76. }
  77. public void Toggle(EntityUid uid, GasValveComponent component)
  78. {
  79. Set(uid, component, !component.Open);
  80. }
  81. }
  82. }