SharedGasPressurePumpSystem.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Content.Shared.Administration.Logs;
  2. using Content.Shared.Atmos.Components;
  3. using Content.Shared.Atmos.Piping;
  4. using Content.Shared.Atmos.Piping.Binary.Components;
  5. using Content.Shared.Atmos.Piping.Components;
  6. using Content.Shared.Database;
  7. using Content.Shared.Examine;
  8. using Content.Shared.Popups;
  9. using Content.Shared.Power;
  10. using Content.Shared.Power.Components;
  11. using Content.Shared.Power.EntitySystems;
  12. using Content.Shared.UserInterface;
  13. namespace Content.Shared.Atmos.EntitySystems;
  14. public abstract class SharedGasPressurePumpSystem : EntitySystem
  15. {
  16. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  17. [Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
  18. [Dependency] private readonly SharedPowerReceiverSystem _receiver = default!;
  19. [Dependency] protected readonly SharedUserInterfaceSystem UserInterfaceSystem = default!;
  20. // TODO: Check enabled for activatableUI
  21. // TODO: Add activatableUI to it.
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<GasPressurePumpComponent, ComponentInit>(OnInit);
  26. SubscribeLocalEvent<GasPressurePumpComponent, PowerChangedEvent>(OnPowerChanged);
  27. SubscribeLocalEvent<GasPressurePumpComponent, GasPressurePumpChangeOutputPressureMessage>(OnOutputPressureChangeMessage);
  28. SubscribeLocalEvent<GasPressurePumpComponent, GasPressurePumpToggleStatusMessage>(OnToggleStatusMessage);
  29. SubscribeLocalEvent<GasPressurePumpComponent, AtmosDeviceDisabledEvent>(OnPumpLeaveAtmosphere);
  30. SubscribeLocalEvent<GasPressurePumpComponent, ExaminedEvent>(OnExamined);
  31. }
  32. private void OnExamined(EntityUid uid, GasPressurePumpComponent pump, ExaminedEvent args)
  33. {
  34. if (!Transform(uid).Anchored)
  35. return;
  36. if (Loc.TryGetString("gas-pressure-pump-system-examined", out var str,
  37. ("statusColor", "lightblue"), // TODO: change with pressure?
  38. ("pressure", pump.TargetPressure)
  39. ))
  40. {
  41. args.PushMarkup(str);
  42. }
  43. }
  44. private void OnInit(EntityUid uid, GasPressurePumpComponent pump, ComponentInit args)
  45. {
  46. UpdateAppearance(uid, pump);
  47. }
  48. private void OnPowerChanged(EntityUid uid, GasPressurePumpComponent component, ref PowerChangedEvent args)
  49. {
  50. UpdateAppearance(uid, component);
  51. }
  52. private void UpdateAppearance(EntityUid uid, GasPressurePumpComponent? pump = null, AppearanceComponent? appearance = null)
  53. {
  54. if (!Resolve(uid, ref pump, ref appearance, false))
  55. return;
  56. var pumpOn = pump.Enabled && _receiver.IsPowered(uid);
  57. Appearance.SetData(uid, PumpVisuals.Enabled, pumpOn, appearance);
  58. }
  59. private void OnToggleStatusMessage(EntityUid uid, GasPressurePumpComponent pump, GasPressurePumpToggleStatusMessage args)
  60. {
  61. pump.Enabled = args.Enabled;
  62. _adminLogger.Add(LogType.AtmosPowerChanged, LogImpact.Medium,
  63. $"{ToPrettyString(args.Actor):player} set the power on {ToPrettyString(uid):device} to {args.Enabled}");
  64. Dirty(uid, pump);
  65. UpdateAppearance(uid, pump);
  66. }
  67. private void OnOutputPressureChangeMessage(EntityUid uid, GasPressurePumpComponent pump, GasPressurePumpChangeOutputPressureMessage args)
  68. {
  69. pump.TargetPressure = Math.Clamp(args.Pressure, 0f, Atmospherics.MaxOutputPressure);
  70. _adminLogger.Add(LogType.AtmosPressureChanged, LogImpact.Medium,
  71. $"{ToPrettyString(args.Actor):player} set the pressure on {ToPrettyString(uid):device} to {args.Pressure}kPa");
  72. Dirty(uid, pump);
  73. }
  74. private void OnPumpLeaveAtmosphere(EntityUid uid, GasPressurePumpComponent pump, ref AtmosDeviceDisabledEvent args)
  75. {
  76. pump.Enabled = false;
  77. Dirty(uid, pump);
  78. UpdateAppearance(uid, pump);
  79. UserInterfaceSystem.CloseUi(uid, GasPressurePumpUiKey.Key);
  80. }
  81. }