SlipperySystem.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Content.Shared.Administration.Logs;
  2. using Content.Shared.Database;
  3. using Content.Shared.Inventory;
  4. using Robust.Shared.Network;
  5. using Content.Shared.Movement.Components;
  6. using Content.Shared.Movement.Systems;
  7. using Content.Shared.Popups;
  8. using Content.Shared.StatusEffect;
  9. using Content.Shared.StepTrigger.Systems;
  10. using Content.Shared.Stunnable;
  11. using Content.Shared.Throwing;
  12. using JetBrains.Annotations;
  13. using Robust.Shared.Audio.Systems;
  14. using Robust.Shared.Containers;
  15. using Robust.Shared.Physics.Components;
  16. using Robust.Shared.Physics.Systems;
  17. using Robust.Shared.Physics.Events;
  18. using Robust.Shared.Utility;
  19. namespace Content.Shared.Slippery;
  20. [UsedImplicitly]
  21. public sealed class SlipperySystem : EntitySystem
  22. {
  23. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  24. [Dependency] private readonly SharedAudioSystem _audio = default!;
  25. [Dependency] private readonly SharedStunSystem _stun = default!;
  26. [Dependency] private readonly StatusEffectsSystem _statusEffects = default!;
  27. [Dependency] private readonly SharedContainerSystem _container = default!;
  28. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  29. [Dependency] private readonly SpeedModifierContactsSystem _speedModifier = default!;
  30. public override void Initialize()
  31. {
  32. base.Initialize();
  33. SubscribeLocalEvent<SlipperyComponent, StepTriggerAttemptEvent>(HandleAttemptCollide);
  34. SubscribeLocalEvent<SlipperyComponent, StepTriggeredOffEvent>(HandleStepTrigger);
  35. SubscribeLocalEvent<NoSlipComponent, SlipAttemptEvent>(OnNoSlipAttempt);
  36. SubscribeLocalEvent<SlowedOverSlipperyComponent, SlipAttemptEvent>(OnSlowedOverSlipAttempt);
  37. SubscribeLocalEvent<ThrownItemComponent, SlipCausingAttemptEvent>(OnThrownSlipAttempt);
  38. // as long as slip-resistant mice are never added, this should be fine (otherwise a mouse-hat will transfer it's power to the wearer).
  39. SubscribeLocalEvent<NoSlipComponent, InventoryRelayedEvent<SlipAttemptEvent>>((e, c, ev) => OnNoSlipAttempt(e, c, ev.Args));
  40. SubscribeLocalEvent<SlowedOverSlipperyComponent, InventoryRelayedEvent<SlipAttemptEvent>>((e, c, ev) => OnSlowedOverSlipAttempt(e, c, ev.Args));
  41. SubscribeLocalEvent<SlowedOverSlipperyComponent, InventoryRelayedEvent<GetSlowedOverSlipperyModifierEvent>>(OnGetSlowedOverSlipperyModifier);
  42. SubscribeLocalEvent<SlipperyComponent, EndCollideEvent>(OnEntityExit);
  43. }
  44. private void HandleStepTrigger(EntityUid uid, SlipperyComponent component, ref StepTriggeredOffEvent args)
  45. {
  46. TrySlip(uid, component, args.Tripper);
  47. }
  48. private void HandleAttemptCollide(
  49. EntityUid uid,
  50. SlipperyComponent component,
  51. ref StepTriggerAttemptEvent args)
  52. {
  53. args.Continue |= CanSlip(uid, args.Tripper);
  54. }
  55. private static void OnNoSlipAttempt(EntityUid uid, NoSlipComponent component, SlipAttemptEvent args)
  56. {
  57. args.NoSlip = true;
  58. }
  59. private void OnSlowedOverSlipAttempt(EntityUid uid, SlowedOverSlipperyComponent component, SlipAttemptEvent args)
  60. {
  61. args.SlowOverSlippery = true;
  62. }
  63. private void OnThrownSlipAttempt(EntityUid uid, ThrownItemComponent comp, ref SlipCausingAttemptEvent args)
  64. {
  65. args.Cancelled = true;
  66. }
  67. private void OnGetSlowedOverSlipperyModifier(EntityUid uid, SlowedOverSlipperyComponent comp, ref InventoryRelayedEvent<GetSlowedOverSlipperyModifierEvent> args)
  68. {
  69. args.Args.SlowdownModifier *= comp.SlowdownModifier;
  70. }
  71. private void OnEntityExit(EntityUid uid, SlipperyComponent component, ref EndCollideEvent args)
  72. {
  73. if (HasComp<SpeedModifiedByContactComponent>(args.OtherEntity))
  74. _speedModifier.AddModifiedEntity(args.OtherEntity);
  75. }
  76. private bool CanSlip(EntityUid uid, EntityUid toSlip)
  77. {
  78. return !_container.IsEntityInContainer(uid)
  79. && _statusEffects.CanApplyEffect(toSlip, "Stun"); //Should be KnockedDown instead?
  80. }
  81. public void TrySlip(EntityUid uid, SlipperyComponent component, EntityUid other, bool requiresContact = true)
  82. {
  83. if (HasComp<KnockedDownComponent>(other) && !component.SuperSlippery)
  84. return;
  85. var attemptEv = new SlipAttemptEvent();
  86. RaiseLocalEvent(other, attemptEv);
  87. if (attemptEv.SlowOverSlippery)
  88. _speedModifier.AddModifiedEntity(other);
  89. if (attemptEv.NoSlip)
  90. return;
  91. var attemptCausingEv = new SlipCausingAttemptEvent();
  92. RaiseLocalEvent(uid, ref attemptCausingEv);
  93. if (attemptCausingEv.Cancelled)
  94. return;
  95. var ev = new SlipEvent(other);
  96. RaiseLocalEvent(uid, ref ev);
  97. if (TryComp(other, out PhysicsComponent? physics) && !HasComp<SlidingComponent>(other))
  98. {
  99. _physics.SetLinearVelocity(other, physics.LinearVelocity * component.LaunchForwardsMultiplier, body: physics);
  100. if (component.SuperSlippery && requiresContact)
  101. {
  102. var sliding = EnsureComp<SlidingComponent>(other);
  103. sliding.CollidingEntities.Add(uid);
  104. DebugTools.Assert(_physics.GetContactingEntities(other, physics).Contains(uid));
  105. }
  106. }
  107. var playSound = !_statusEffects.HasStatusEffect(other, "KnockedDown");
  108. _stun.TryParalyze(other, TimeSpan.FromSeconds(component.ParalyzeTime), true);
  109. // Preventing from playing the slip sound when you are already knocked down.
  110. if (playSound)
  111. {
  112. _audio.PlayPredicted(component.SlipSound, other, other);
  113. }
  114. _adminLogger.Add(LogType.Slip, LogImpact.Low,
  115. $"{ToPrettyString(other):mob} slipped on collision with {ToPrettyString(uid):entity}");
  116. }
  117. }
  118. /// <summary>
  119. /// Raised on an entity to determine if it can slip or not.
  120. /// </summary>
  121. public sealed class SlipAttemptEvent : EntityEventArgs, IInventoryRelayEvent
  122. {
  123. public bool NoSlip;
  124. public bool SlowOverSlippery;
  125. public SlotFlags TargetSlots { get; } = SlotFlags.FEET;
  126. }
  127. /// <summary>
  128. /// Raised on an entity that is causing the slip event (e.g, the banana peel), to determine if the slip attempt should be cancelled.
  129. /// </summary>
  130. /// <param name="Cancelled">If the slip should be cancelled</param>
  131. [ByRefEvent]
  132. public record struct SlipCausingAttemptEvent (bool Cancelled);
  133. /// Raised on an entity that CAUSED some other entity to slip (e.g., the banana peel).
  134. /// <param name="Slipped">The entity being slipped</param>
  135. [ByRefEvent]
  136. public readonly record struct SlipEvent(EntityUid Slipped);