SharedDisposalUnitSystem.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Shared.Body.Components;
  3. using Content.Shared.Disposal.Components;
  4. using Content.Shared.DoAfter;
  5. using Content.Shared.DragDrop;
  6. using Content.Shared.Emag.Systems;
  7. using Content.Shared.Item;
  8. using Content.Shared.Throwing;
  9. using Content.Shared.Whitelist;
  10. using Robust.Shared.Audio;
  11. using Robust.Shared.Physics.Components;
  12. using Robust.Shared.Physics.Events;
  13. using Robust.Shared.Physics.Systems;
  14. using Robust.Shared.Serialization;
  15. using Robust.Shared.Timing;
  16. namespace Content.Shared.Disposal;
  17. [Serializable, NetSerializable]
  18. public sealed partial class DisposalDoAfterEvent : SimpleDoAfterEvent
  19. {
  20. }
  21. public abstract class SharedDisposalUnitSystem : EntitySystem
  22. {
  23. [Dependency] protected readonly IGameTiming GameTiming = default!;
  24. [Dependency] protected readonly EmagSystem _emag = default!;
  25. [Dependency] protected readonly MetaDataSystem Metadata = default!;
  26. [Dependency] protected readonly SharedJointSystem Joints = default!;
  27. [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
  28. protected static TimeSpan ExitAttemptDelay = TimeSpan.FromSeconds(0.5);
  29. // Percentage
  30. public const float PressurePerSecond = 0.05f;
  31. public abstract bool HasDisposals([NotNullWhen(true)] EntityUid? uid);
  32. public abstract bool ResolveDisposals(EntityUid uid, [NotNullWhen(true)] ref SharedDisposalUnitComponent? component);
  33. /// <summary>
  34. /// Gets the current pressure state of a disposals unit.
  35. /// </summary>
  36. /// <param name="uid"></param>
  37. /// <param name="component"></param>
  38. /// <param name="metadata"></param>
  39. /// <returns></returns>
  40. public DisposalsPressureState GetState(EntityUid uid, SharedDisposalUnitComponent component, MetaDataComponent? metadata = null)
  41. {
  42. var nextPressure = Metadata.GetPauseTime(uid, metadata) + component.NextPressurized - GameTiming.CurTime;
  43. var pressurizeTime = 1f / PressurePerSecond;
  44. var pressurizeDuration = pressurizeTime - component.FlushDelay.TotalSeconds;
  45. if (nextPressure.TotalSeconds > pressurizeDuration)
  46. {
  47. return DisposalsPressureState.Flushed;
  48. }
  49. if (nextPressure > TimeSpan.Zero)
  50. {
  51. return DisposalsPressureState.Pressurizing;
  52. }
  53. return DisposalsPressureState.Ready;
  54. }
  55. public float GetPressure(EntityUid uid, SharedDisposalUnitComponent component, MetaDataComponent? metadata = null)
  56. {
  57. if (!Resolve(uid, ref metadata))
  58. return 0f;
  59. var pauseTime = Metadata.GetPauseTime(uid, metadata);
  60. return MathF.Min(1f,
  61. (float) (GameTiming.CurTime - pauseTime - component.NextPressurized).TotalSeconds / PressurePerSecond);
  62. }
  63. protected void OnPreventCollide(EntityUid uid, SharedDisposalUnitComponent component,
  64. ref PreventCollideEvent args)
  65. {
  66. var otherBody = args.OtherEntity;
  67. // Items dropped shouldn't collide but items thrown should
  68. if (HasComp<ItemComponent>(otherBody) && !HasComp<ThrownItemComponent>(otherBody))
  69. {
  70. args.Cancelled = true;
  71. return;
  72. }
  73. if (component.RecentlyEjected.Contains(otherBody))
  74. {
  75. args.Cancelled = true;
  76. }
  77. }
  78. protected void OnCanDragDropOn(EntityUid uid, SharedDisposalUnitComponent component, ref CanDropTargetEvent args)
  79. {
  80. if (args.Handled)
  81. return;
  82. args.CanDrop = CanInsert(uid, component, args.Dragged);
  83. args.Handled = true;
  84. }
  85. protected void OnEmagged(EntityUid uid, SharedDisposalUnitComponent component, ref GotEmaggedEvent args)
  86. {
  87. if (!_emag.CompareFlag(args.Type, EmagType.Interaction))
  88. return;
  89. if (component.DisablePressure == true)
  90. return;
  91. component.DisablePressure = true;
  92. args.Handled = true;
  93. }
  94. public virtual bool CanInsert(EntityUid uid, SharedDisposalUnitComponent component, EntityUid entity)
  95. {
  96. if (!Transform(uid).Anchored)
  97. return false;
  98. var storable = HasComp<ItemComponent>(entity);
  99. if (!storable && !HasComp<BodyComponent>(entity))
  100. return false;
  101. if (_whitelistSystem.IsBlacklistPass(component.Blacklist, entity) ||
  102. _whitelistSystem.IsWhitelistFail(component.Whitelist, entity))
  103. return false;
  104. if (TryComp<PhysicsComponent>(entity, out var physics) && (physics.CanCollide) || storable)
  105. return true;
  106. else
  107. return false;
  108. }
  109. public abstract void DoInsertDisposalUnit(EntityUid uid, EntityUid toInsert, EntityUid user, SharedDisposalUnitComponent? disposal = null);
  110. [Serializable, NetSerializable]
  111. protected sealed class DisposalUnitComponentState : ComponentState
  112. {
  113. public SoundSpecifier? FlushSound;
  114. public DisposalsPressureState State;
  115. public TimeSpan NextPressurized;
  116. public TimeSpan AutomaticEngageTime;
  117. public TimeSpan? NextFlush;
  118. public bool Powered;
  119. public bool Engaged;
  120. public List<NetEntity> RecentlyEjected;
  121. public DisposalUnitComponentState(SoundSpecifier? flushSound, DisposalsPressureState state, TimeSpan nextPressurized, TimeSpan automaticEngageTime, TimeSpan? nextFlush, bool powered, bool engaged, List<NetEntity> recentlyEjected)
  122. {
  123. FlushSound = flushSound;
  124. State = state;
  125. NextPressurized = nextPressurized;
  126. AutomaticEngageTime = automaticEngageTime;
  127. NextFlush = nextFlush;
  128. Powered = powered;
  129. Engaged = engaged;
  130. RecentlyEjected = recentlyEjected;
  131. }
  132. }
  133. }