SwapTeleporterSystem.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using Content.Shared.Examine;
  2. using Content.Shared.IdentityManagement;
  3. using Content.Shared.Interaction;
  4. using Content.Shared.Popups;
  5. using Content.Shared.Teleportation.Components;
  6. using Content.Shared.Verbs;
  7. using Content.Shared.Whitelist;
  8. using Robust.Shared.Audio.Systems;
  9. using Robust.Shared.Containers;
  10. using Robust.Shared.Map.Components;
  11. using Robust.Shared.Physics;
  12. using Robust.Shared.Physics.Components;
  13. using Robust.Shared.Timing;
  14. namespace Content.Shared.Teleportation.Systems;
  15. /// <summary>
  16. /// This handles <see cref="SwapTeleporterComponent"/>
  17. /// </summary>
  18. public sealed class SwapTeleporterSystem : EntitySystem
  19. {
  20. [Dependency] private readonly IGameTiming _timing = default!;
  21. [Dependency] private readonly SharedAudioSystem _audio = default!;
  22. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  23. [Dependency] private readonly SharedContainerSystem _container = default!;
  24. [Dependency] private readonly SharedPopupSystem _popup = default!;
  25. [Dependency] private readonly SharedTransformSystem _transform = default!;
  26. [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
  27. private EntityQuery<TransformComponent> _xformQuery;
  28. /// <inheritdoc/>
  29. public override void Initialize()
  30. {
  31. SubscribeLocalEvent<SwapTeleporterComponent, AfterInteractEvent>(OnInteract);
  32. SubscribeLocalEvent<SwapTeleporterComponent, GetVerbsEvent<AlternativeVerb>>(OnGetAltVerb);
  33. SubscribeLocalEvent<SwapTeleporterComponent, ActivateInWorldEvent>(OnActivateInWorld);
  34. SubscribeLocalEvent<SwapTeleporterComponent, ExaminedEvent>(OnExamined);
  35. SubscribeLocalEvent<SwapTeleporterComponent, ComponentShutdown>(OnShutdown);
  36. _xformQuery = GetEntityQuery<TransformComponent>();
  37. }
  38. private void OnInteract(Entity<SwapTeleporterComponent> ent, ref AfterInteractEvent args)
  39. {
  40. var (uid, comp) = ent;
  41. if (args.Target == null || !args.CanReach)
  42. return;
  43. var target = args.Target.Value;
  44. if (!TryComp<SwapTeleporterComponent>(target, out var targetComp))
  45. return;
  46. if (_whitelistSystem.IsWhitelistFail(comp.TeleporterWhitelist, target) ||
  47. _whitelistSystem.IsWhitelistFail(targetComp.TeleporterWhitelist, uid))
  48. {
  49. return;
  50. }
  51. if (comp.LinkedEnt != null)
  52. {
  53. _popup.PopupClient(Loc.GetString("swap-teleporter-popup-link-fail-already"), uid, args.User);
  54. return;
  55. }
  56. if (targetComp.LinkedEnt != null)
  57. {
  58. _popup.PopupClient(Loc.GetString("swap-teleporter-popup-link-fail-already-other"), uid, args.User);
  59. return;
  60. }
  61. comp.LinkedEnt = target;
  62. targetComp.LinkedEnt = uid;
  63. Dirty(uid, comp);
  64. Dirty(target, targetComp);
  65. _appearance.SetData(uid, SwapTeleporterVisuals.Linked, true);
  66. _appearance.SetData(target, SwapTeleporterVisuals.Linked, true);
  67. _popup.PopupClient(Loc.GetString("swap-teleporter-popup-link-create"), uid, args.User);
  68. }
  69. private void OnGetAltVerb(Entity<SwapTeleporterComponent> ent, ref GetVerbsEvent<AlternativeVerb> args)
  70. {
  71. var (uid, comp) = ent;
  72. if (!args.CanAccess || !args.CanInteract || args.Hands == null || comp.TeleportTime != null)
  73. return;
  74. if (!TryComp<SwapTeleporterComponent>(comp.LinkedEnt, out var otherComp) || otherComp.TeleportTime != null)
  75. return;
  76. var user = args.User;
  77. args.Verbs.Add(new AlternativeVerb
  78. {
  79. Text = Loc.GetString("swap-teleporter-verb-destroy-link"),
  80. Priority = 1,
  81. Act = () =>
  82. {
  83. DestroyLink((uid, comp), user);
  84. }
  85. });
  86. }
  87. private void OnActivateInWorld(Entity<SwapTeleporterComponent> ent, ref ActivateInWorldEvent args)
  88. {
  89. if (args.Handled || !args.Complex)
  90. return;
  91. var (uid, comp) = ent;
  92. var user = args.User;
  93. if (comp.TeleportTime != null)
  94. return;
  95. if (comp.LinkedEnt == null)
  96. {
  97. _popup.PopupClient(Loc.GetString("swap-teleporter-popup-teleport-cancel-link"), ent, user);
  98. return;
  99. }
  100. // don't allow teleporting to happen if the linked one is already teleporting
  101. if (!TryComp<SwapTeleporterComponent>(comp.LinkedEnt, out var otherComp)
  102. || otherComp.TeleportTime != null)
  103. {
  104. return;
  105. }
  106. if (_timing.CurTime < comp.NextTeleportUse)
  107. {
  108. _popup.PopupClient(Loc.GetString("swap-teleporter-popup-teleport-cancel-time"), ent, user);
  109. return;
  110. }
  111. _audio.PlayPredicted(comp.TeleportSound, uid, user);
  112. _audio.PlayPredicted(otherComp.TeleportSound, comp.LinkedEnt.Value, user);
  113. comp.NextTeleportUse = _timing.CurTime + comp.Cooldown;
  114. comp.TeleportTime = _timing.CurTime + comp.TeleportDelay;
  115. Dirty(uid, comp);
  116. args.Handled = true;
  117. }
  118. public void DoTeleport(Entity<SwapTeleporterComponent, TransformComponent> ent)
  119. {
  120. var (uid, comp, xform) = ent;
  121. comp.TeleportTime = null;
  122. Dirty(uid, comp);
  123. if (comp.LinkedEnt is not { } linkedEnt)
  124. {
  125. return;
  126. }
  127. var teleEnt = GetTeleportingEntity((uid, xform));
  128. var otherTeleEnt = GetTeleportingEntity((linkedEnt, Transform(linkedEnt)));
  129. _container.TryGetOuterContainer(teleEnt, Transform(teleEnt), out var cont);
  130. _container.TryGetOuterContainer(otherTeleEnt, Transform(otherTeleEnt), out var otherCont);
  131. if (otherCont != null && !_container.CanInsert(teleEnt, otherCont) ||
  132. cont != null && !_container.CanInsert(otherTeleEnt, cont))
  133. {
  134. _popup.PopupEntity(Loc.GetString("swap-teleporter-popup-teleport-fail",
  135. ("entity", Identity.Entity(linkedEnt, EntityManager))),
  136. teleEnt,
  137. teleEnt,
  138. PopupType.MediumCaution);
  139. return;
  140. }
  141. _popup.PopupClient(Loc.GetString("swap-teleporter-popup-teleport-other",
  142. ("entity", Identity.Entity(linkedEnt, EntityManager))),
  143. teleEnt,
  144. otherTeleEnt,
  145. PopupType.MediumCaution);
  146. _transform.SwapPositions(teleEnt, otherTeleEnt);
  147. }
  148. /// <remarks>
  149. /// HYAH -link
  150. /// </remarks>
  151. public void DestroyLink(Entity<SwapTeleporterComponent?> ent, EntityUid? user)
  152. {
  153. if (!Resolve(ent, ref ent.Comp, false))
  154. return;
  155. var linkedNullable = ent.Comp.LinkedEnt;
  156. ent.Comp.LinkedEnt = null;
  157. ent.Comp.TeleportTime = null;
  158. _appearance.SetData(ent, SwapTeleporterVisuals.Linked, false);
  159. Dirty(ent, ent.Comp);
  160. if (user != null)
  161. _popup.PopupClient(Loc.GetString("swap-teleporter-popup-link-destroyed"), ent, user.Value);
  162. else
  163. _popup.PopupEntity(Loc.GetString("swap-teleporter-popup-link-destroyed"), ent);
  164. if (linkedNullable is {} linked)
  165. DestroyLink(linked, user); // the linked one is shown globally
  166. }
  167. private EntityUid GetTeleportingEntity(Entity<TransformComponent> ent)
  168. {
  169. var parent = ent.Comp.ParentUid;
  170. if (HasComp<MapGridComponent>(parent) || HasComp<MapComponent>(parent))
  171. return ent;
  172. if (!_xformQuery.TryGetComponent(parent, out var parentXform) || parentXform.Anchored)
  173. return ent;
  174. if (!TryComp<PhysicsComponent>(parent, out var body) || body.BodyType == BodyType.Static)
  175. return ent;
  176. return GetTeleportingEntity((parent, parentXform));
  177. }
  178. private void OnExamined(Entity<SwapTeleporterComponent> ent, ref ExaminedEvent args)
  179. {
  180. var (_, comp) = ent;
  181. using (args.PushGroup(nameof(SwapTeleporterComponent)))
  182. {
  183. var locale = comp.LinkedEnt == null
  184. ? "swap-teleporter-examine-link-absent"
  185. : "swap-teleporter-examine-link-present";
  186. args.PushMarkup(Loc.GetString(locale));
  187. if (_timing.CurTime < comp.NextTeleportUse)
  188. {
  189. args.PushMarkup(Loc.GetString("swap-teleporter-examine-time-remaining",
  190. ("second", (int) ((comp.NextTeleportUse - _timing.CurTime).TotalSeconds + 0.5f))));
  191. }
  192. }
  193. }
  194. private void OnShutdown(Entity<SwapTeleporterComponent> ent, ref ComponentShutdown args)
  195. {
  196. DestroyLink((ent, ent), null);
  197. }
  198. public override void Update(float frameTime)
  199. {
  200. base.Update(frameTime);
  201. var query = EntityQueryEnumerator<SwapTeleporterComponent, TransformComponent>();
  202. while (query.MoveNext(out var uid, out var comp, out var xform))
  203. {
  204. if (comp.TeleportTime == null)
  205. continue;
  206. if (_timing.CurTime < comp.TeleportTime)
  207. continue;
  208. DoTeleport((uid, comp, xform));
  209. }
  210. }
  211. }