StandingStateSystem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using Content.Shared.Hands.Components;
  2. using Content.Shared.Physics;
  3. using Content.Shared.Rotation;
  4. using Robust.Shared.Audio;
  5. using Robust.Shared.Audio.Systems;
  6. using Robust.Shared.Physics;
  7. using Robust.Shared.Physics.Systems;
  8. namespace Content.Shared.Standing
  9. {
  10. public sealed class StandingStateSystem : EntitySystem
  11. {
  12. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  13. [Dependency] private readonly SharedAudioSystem _audio = default!;
  14. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  15. // If StandingCollisionLayer value is ever changed to more than one layer, the logic needs to be edited.
  16. private const int StandingCollisionLayer = (int) CollisionGroup.MidImpassable;
  17. public bool IsDown(EntityUid uid, StandingStateComponent? standingState = null)
  18. {
  19. if (!Resolve(uid, ref standingState, false))
  20. return false;
  21. return !standingState.Standing;
  22. }
  23. public bool Down(EntityUid uid,
  24. bool playSound = true,
  25. bool dropHeldItems = true,
  26. bool force = false,
  27. StandingStateComponent? standingState = null,
  28. AppearanceComponent? appearance = null,
  29. HandsComponent? hands = null)
  30. {
  31. // TODO: This should actually log missing comps...
  32. if (!Resolve(uid, ref standingState, false))
  33. return false;
  34. // Optional component.
  35. Resolve(uid, ref appearance, ref hands, false);
  36. if (!standingState.Standing)
  37. return true;
  38. // This is just to avoid most callers doing this manually saving boilerplate
  39. // 99% of the time you'll want to drop items but in some scenarios (e.g. buckling) you don't want to.
  40. // We do this BEFORE downing because something like buckle may be blocking downing but we want to drop hand items anyway
  41. // and ultimately this is just to avoid boilerplate in Down callers + keep their behavior consistent.
  42. if (dropHeldItems && hands != null)
  43. {
  44. RaiseLocalEvent(uid, new DropHandItemsEvent(), false);
  45. }
  46. if (!force)
  47. {
  48. var msg = new DownAttemptEvent();
  49. RaiseLocalEvent(uid, msg, false);
  50. if (msg.Cancelled)
  51. return false;
  52. }
  53. standingState.Standing = false;
  54. Dirty(uid, standingState);
  55. RaiseLocalEvent(uid, new DownedEvent(), false);
  56. // Seemed like the best place to put it
  57. _appearance.SetData(uid, RotationVisuals.RotationState, RotationState.Horizontal, appearance);
  58. // Change collision masks to allow going under certain entities like flaps and tables
  59. if (TryComp(uid, out FixturesComponent? fixtureComponent))
  60. {
  61. foreach (var (key, fixture) in fixtureComponent.Fixtures)
  62. {
  63. if ((fixture.CollisionMask & StandingCollisionLayer) == 0)
  64. continue;
  65. standingState.ChangedFixtures.Add(key);
  66. _physics.SetCollisionMask(uid, key, fixture, fixture.CollisionMask & ~StandingCollisionLayer, manager: fixtureComponent);
  67. }
  68. }
  69. // check if component was just added or streamed to client
  70. // if true, no need to play sound - mob was down before player could seen that
  71. if (standingState.LifeStage <= ComponentLifeStage.Starting)
  72. return true;
  73. if (playSound)
  74. {
  75. _audio.PlayPredicted(standingState.DownSound, uid, uid);
  76. }
  77. return true;
  78. }
  79. public bool Stand(EntityUid uid,
  80. StandingStateComponent? standingState = null,
  81. AppearanceComponent? appearance = null,
  82. bool force = false)
  83. {
  84. // TODO: This should actually log missing comps...
  85. if (!Resolve(uid, ref standingState, false))
  86. return false;
  87. // Optional component.
  88. Resolve(uid, ref appearance, false);
  89. if (standingState.Standing)
  90. return true;
  91. if (!force)
  92. {
  93. var msg = new StandAttemptEvent();
  94. RaiseLocalEvent(uid, msg, false);
  95. if (msg.Cancelled)
  96. return false;
  97. }
  98. standingState.Standing = true;
  99. Dirty(uid, standingState);
  100. RaiseLocalEvent(uid, new StoodEvent(), false);
  101. _appearance.SetData(uid, RotationVisuals.RotationState, RotationState.Vertical, appearance);
  102. if (TryComp(uid, out FixturesComponent? fixtureComponent))
  103. {
  104. foreach (var key in standingState.ChangedFixtures)
  105. {
  106. if (fixtureComponent.Fixtures.TryGetValue(key, out var fixture))
  107. _physics.SetCollisionMask(uid, key, fixture, fixture.CollisionMask | StandingCollisionLayer, fixtureComponent);
  108. }
  109. }
  110. standingState.ChangedFixtures.Clear();
  111. return true;
  112. }
  113. }
  114. public sealed class DropHandItemsEvent : EventArgs
  115. {
  116. }
  117. /// <summary>
  118. /// Subscribe if you can potentially block a down attempt.
  119. /// </summary>
  120. public sealed class DownAttemptEvent : CancellableEntityEventArgs
  121. {
  122. }
  123. /// <summary>
  124. /// Subscribe if you can potentially block a stand attempt.
  125. /// </summary>
  126. public sealed class StandAttemptEvent : CancellableEntityEventArgs
  127. {
  128. }
  129. /// <summary>
  130. /// Raised when an entity becomes standing
  131. /// </summary>
  132. public sealed class StoodEvent : EntityEventArgs
  133. {
  134. }
  135. /// <summary>
  136. /// Raised when an entity is not standing
  137. /// </summary>
  138. public sealed class DownedEvent : EntityEventArgs
  139. {
  140. }
  141. }