InteractionPopupSystem.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using Content.Shared.Bed.Sleep;
  2. using Content.Shared.IdentityManagement;
  3. using Content.Shared.Interaction.Components;
  4. using Content.Shared.Interaction.Events;
  5. using Content.Shared.Mobs.Components;
  6. using Content.Shared.Mobs.Systems;
  7. using Content.Shared.Popups;
  8. using Robust.Shared.Audio;
  9. using Robust.Shared.Audio.Systems;
  10. using Robust.Shared.Network;
  11. using Robust.Shared.Player;
  12. using Robust.Shared.Random;
  13. using Robust.Shared.Timing;
  14. namespace Content.Shared.Interaction;
  15. public sealed class InteractionPopupSystem : EntitySystem
  16. {
  17. [Dependency] private readonly IGameTiming _gameTiming = default!;
  18. [Dependency] private readonly IRobustRandom _random = default!;
  19. [Dependency] private readonly MobStateSystem _mobStateSystem = default!;
  20. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  21. [Dependency] private readonly SharedAudioSystem _audio = default!;
  22. [Dependency] private readonly SharedTransformSystem _transform = default!;
  23. [Dependency] private readonly INetManager _netMan = default!;
  24. public override void Initialize()
  25. {
  26. base.Initialize();
  27. SubscribeLocalEvent<InteractionPopupComponent, InteractHandEvent>(OnInteractHand);
  28. SubscribeLocalEvent<InteractionPopupComponent, ActivateInWorldEvent>(OnActivateInWorld);
  29. }
  30. private void OnActivateInWorld(EntityUid uid, InteractionPopupComponent component, ActivateInWorldEvent args)
  31. {
  32. if (!args.Complex)
  33. return;
  34. if (!component.OnActivate)
  35. return;
  36. SharedInteract(uid, component, args, args.Target, args.User);
  37. }
  38. private void OnInteractHand(EntityUid uid, InteractionPopupComponent component, InteractHandEvent args)
  39. {
  40. SharedInteract(uid, component, args, args.Target, args.User);
  41. }
  42. private void SharedInteract(
  43. EntityUid uid,
  44. InteractionPopupComponent component,
  45. HandledEntityEventArgs args,
  46. EntityUid target,
  47. EntityUid user)
  48. {
  49. if (args.Handled || user == target)
  50. return;
  51. //Handling does nothing and this thing annoyingly plays way too often.
  52. // HUH? What does this comment even mean?
  53. if (HasComp<SleepingComponent>(uid))
  54. return;
  55. if (TryComp<MobStateComponent>(uid, out var state)
  56. && !_mobStateSystem.IsAlive(uid, state))
  57. {
  58. return;
  59. }
  60. args.Handled = true;
  61. var curTime = _gameTiming.CurTime;
  62. if (curTime < component.LastInteractTime + component.InteractDelay)
  63. return;
  64. component.LastInteractTime = curTime;
  65. // TODO: Should be an attempt event
  66. // TODO: Need to handle pausing with an accumulator.
  67. var msg = ""; // Stores the text to be shown in the popup message
  68. SoundSpecifier? sfx = null; // Stores the filepath of the sound to be played
  69. var predict = component.SuccessChance is 0 or 1
  70. && component.InteractSuccessSpawn == null
  71. && component.InteractFailureSpawn == null;
  72. if (_netMan.IsClient && !predict)
  73. return;
  74. if (_random.Prob(component.SuccessChance))
  75. {
  76. if (component.InteractSuccessString != null)
  77. msg = Loc.GetString(component.InteractSuccessString, ("target", Identity.Entity(uid, EntityManager))); // Success message (localized).
  78. if (component.InteractSuccessSound != null)
  79. sfx = component.InteractSuccessSound;
  80. if (component.InteractSuccessSpawn != null)
  81. Spawn(component.InteractSuccessSpawn, _transform.GetMapCoordinates(uid));
  82. var ev = new InteractionSuccessEvent(user);
  83. RaiseLocalEvent(target, ref ev);
  84. }
  85. else
  86. {
  87. if (component.InteractFailureString != null)
  88. msg = Loc.GetString(component.InteractFailureString, ("target", Identity.Entity(uid, EntityManager))); // Failure message (localized).
  89. if (component.InteractFailureSound != null)
  90. sfx = component.InteractFailureSound;
  91. if (component.InteractFailureSpawn != null)
  92. Spawn(component.InteractFailureSpawn, _transform.GetMapCoordinates(uid));
  93. var ev = new InteractionFailureEvent(user);
  94. RaiseLocalEvent(target, ref ev);
  95. }
  96. if (!string.IsNullOrEmpty(component.MessagePerceivedByOthers))
  97. {
  98. var msgOthers = Loc.GetString(component.MessagePerceivedByOthers,
  99. ("user", Identity.Entity(user, EntityManager)), ("target", Identity.Entity(uid, EntityManager)));
  100. _popupSystem.PopupEntity(msgOthers, uid, Filter.PvsExcept(user, entityManager: EntityManager), true);
  101. }
  102. if (!predict)
  103. {
  104. _popupSystem.PopupEntity(msg, uid, user);
  105. if (component.SoundPerceivedByOthers)
  106. _audio.PlayPvs(sfx, target);
  107. else
  108. _audio.PlayEntity(sfx, Filter.Entities(user, target), target, false);
  109. return;
  110. }
  111. _popupSystem.PopupClient(msg, uid, user);
  112. if (sfx == null)
  113. return;
  114. if (component.SoundPerceivedByOthers)
  115. {
  116. _audio.PlayPredicted(sfx, target, user);
  117. return;
  118. }
  119. if (_netMan.IsClient)
  120. {
  121. if (_gameTiming.IsFirstTimePredicted)
  122. _audio.PlayEntity(sfx, Filter.Local(), target, true);
  123. }
  124. else
  125. {
  126. _audio.PlayEntity(sfx, Filter.Empty().FromEntities(target), target, false);
  127. }
  128. }
  129. /// <summary>
  130. /// Sets <see cref="InteractionPopupComponent.InteractSuccessString"/>.
  131. /// </summary>
  132. /// <para>
  133. /// This field is not networked automatically, so this method must be called on both sides of the network.
  134. /// </para>
  135. public void SetInteractSuccessString(Entity<InteractionPopupComponent> ent, string str)
  136. {
  137. ent.Comp.InteractSuccessString = str;
  138. }
  139. /// <summary>
  140. /// Sets <see cref="InteractionPopupComponent.InteractFailureString"/>.
  141. /// </summary>
  142. /// <para>
  143. /// This field is not networked automatically, so this method must be called on both sides of the network.
  144. /// </para>
  145. public void SetInteractFailureString(Entity<InteractionPopupComponent> ent, string str)
  146. {
  147. ent.Comp.InteractFailureString = str;
  148. }
  149. }