1
0

StandingStateSystem.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. bool fixtureAttempt = true) //stalker changes
  31. {
  32. // TODO: This should actually log missing comps...
  33. if (!Resolve(uid, ref standingState, false))
  34. return false;
  35. // Optional component.
  36. Resolve(uid, ref appearance, ref hands, false);
  37. if (!standingState.Standing)
  38. return true;
  39. // This is just to avoid most callers doing this manually saving boilerplate
  40. // 99% of the time you'll want to drop items but in some scenarios (e.g. buckling) you don't want to.
  41. // We do this BEFORE downing because something like buckle may be blocking downing but we want to drop hand items anyway
  42. // and ultimately this is just to avoid boilerplate in Down callers + keep their behavior consistent.
  43. if (dropHeldItems && hands != null)
  44. {
  45. RaiseLocalEvent(uid, new DropHandItemsEvent(), false);
  46. }
  47. if (!force)
  48. {
  49. var msg = new DownAttemptEvent();
  50. RaiseLocalEvent(uid, msg, false);
  51. if (msg.Cancelled)
  52. return false;
  53. }
  54. standingState.Standing = false;
  55. Dirty(uid, standingState);
  56. // stalker-changes-start
  57. DownedEvent downedEvent = new DownedEvent();
  58. downedEvent.IgnoreLayingBulletPass = fixtureAttempt;
  59. RaiseLocalEvent(uid, downedEvent, false);
  60. //stalker-changes-end
  61. // Seemed like the best place to put it
  62. _appearance.SetData(uid, RotationVisuals.RotationState, RotationState.Horizontal, appearance);
  63. // Change collision masks to allow going under certain entities like flaps and tables
  64. if (TryComp(uid, out FixturesComponent? fixtureComponent) && fixtureAttempt)
  65. {
  66. foreach (var (key, fixture) in fixtureComponent.Fixtures)
  67. {
  68. if ((fixture.CollisionMask & StandingCollisionLayer) == 0)
  69. continue;
  70. standingState.ChangedFixtures.Add(key);
  71. _physics.SetCollisionMask(uid, key, fixture, fixture.CollisionMask & ~StandingCollisionLayer, manager: fixtureComponent);
  72. }
  73. }
  74. // check if component was just added or streamed to client
  75. // if true, no need to play sound - mob was down before player could seen that
  76. if (standingState.LifeStage <= ComponentLifeStage.Starting)
  77. return true;
  78. if (playSound)
  79. {
  80. _audio.PlayPredicted(standingState.DownSound, uid, uid);
  81. }
  82. return true;
  83. }
  84. public bool Stand(EntityUid uid,
  85. StandingStateComponent? standingState = null,
  86. AppearanceComponent? appearance = null,
  87. bool force = false)
  88. {
  89. // TODO: This should actually log missing comps...
  90. if (!Resolve(uid, ref standingState, false))
  91. return false;
  92. // Optional component.
  93. Resolve(uid, ref appearance, false);
  94. if (standingState.Standing)
  95. return true;
  96. if (!force)
  97. {
  98. var msg = new StandAttemptEvent();
  99. RaiseLocalEvent(uid, msg, false);
  100. if (msg.Cancelled)
  101. return false;
  102. }
  103. standingState.Standing = true;
  104. Dirty(uid, standingState);
  105. RaiseLocalEvent(uid, new StoodEvent(), false);
  106. _appearance.SetData(uid, RotationVisuals.RotationState, RotationState.Vertical, appearance);
  107. if (TryComp(uid, out FixturesComponent? fixtureComponent))
  108. {
  109. foreach (var key in standingState.ChangedFixtures)
  110. {
  111. if (fixtureComponent.Fixtures.TryGetValue(key, out var fixture))
  112. _physics.SetCollisionMask(uid, key, fixture, fixture.CollisionMask | StandingCollisionLayer, fixtureComponent);
  113. }
  114. }
  115. standingState.ChangedFixtures.Clear();
  116. return true;
  117. }
  118. }
  119. public sealed class DropHandItemsEvent : EventArgs
  120. {
  121. }
  122. /// <summary>
  123. /// Subscribe if you can potentially block a down attempt.
  124. /// </summary>
  125. public sealed class DownAttemptEvent : CancellableEntityEventArgs
  126. {
  127. }
  128. /// <summary>
  129. /// Subscribe if you can potentially block a stand attempt.
  130. /// </summary>
  131. public sealed class StandAttemptEvent : CancellableEntityEventArgs
  132. {
  133. }
  134. /// <summary>
  135. /// Raised when an entity becomes standing
  136. /// </summary>
  137. public sealed class StoodEvent : EntityEventArgs
  138. {
  139. }
  140. /// <summary>
  141. /// Raised when an entity is not standing
  142. /// </summary>
  143. public sealed class DownedEvent : EntityEventArgs
  144. {
  145. public bool IgnoreLayingBulletPass = true; // stalker-changes
  146. }
  147. }