SharedNinjaSuitSystem.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using Content.Shared.Actions;
  2. using Content.Shared.Clothing;
  3. using Content.Shared.Clothing.Components;
  4. using Content.Shared.Clothing.EntitySystems;
  5. using Content.Shared.Inventory.Events;
  6. using Content.Shared.Item.ItemToggle;
  7. using Content.Shared.Item.ItemToggle.Components;
  8. using Content.Shared.Ninja.Components;
  9. using Content.Shared.Popups;
  10. using Content.Shared.Timing;
  11. using Robust.Shared.Audio.Systems;
  12. namespace Content.Shared.Ninja.Systems;
  13. /// <summary>
  14. /// Handles (un)equipping and provides some API functions.
  15. /// </summary>
  16. public abstract class SharedNinjaSuitSystem : EntitySystem
  17. {
  18. [Dependency] private readonly ActionContainerSystem _actionContainer = default!;
  19. [Dependency] private readonly SharedAudioSystem _audio = default!;
  20. [Dependency] private readonly ItemToggleSystem _toggle = default!;
  21. [Dependency] protected readonly SharedPopupSystem Popup = default!;
  22. [Dependency] private readonly SharedSpaceNinjaSystem _ninja = default!;
  23. [Dependency] private readonly UseDelaySystem _useDelay = default!;
  24. public override void Initialize()
  25. {
  26. base.Initialize();
  27. SubscribeLocalEvent<NinjaSuitComponent, MapInitEvent>(OnMapInit);
  28. SubscribeLocalEvent<NinjaSuitComponent, ClothingGotEquippedEvent>(OnEquipped);
  29. SubscribeLocalEvent<NinjaSuitComponent, GetItemActionsEvent>(OnGetItemActions);
  30. SubscribeLocalEvent<NinjaSuitComponent, ToggleClothingCheckEvent>(OnCloakCheck);
  31. SubscribeLocalEvent<NinjaSuitComponent, CheckItemCreatorEvent>(OnStarCheck);
  32. SubscribeLocalEvent<NinjaSuitComponent, CreateItemAttemptEvent>(OnCreateStarAttempt);
  33. SubscribeLocalEvent<NinjaSuitComponent, ItemToggleActivateAttemptEvent>(OnActivateAttempt);
  34. SubscribeLocalEvent<NinjaSuitComponent, GotUnequippedEvent>(OnUnequipped);
  35. }
  36. private void OnEquipped(Entity<NinjaSuitComponent> ent, ref ClothingGotEquippedEvent args)
  37. {
  38. var user = args.Wearer;
  39. if (_ninja.NinjaQuery.TryComp(user, out var ninja))
  40. NinjaEquipped(ent, (user, ninja));
  41. }
  42. protected virtual void NinjaEquipped(Entity<NinjaSuitComponent> ent, Entity<SpaceNinjaComponent> user)
  43. {
  44. // mark the user as wearing this suit, used when being attacked among other things
  45. _ninja.AssignSuit(user, ent);
  46. }
  47. private void OnMapInit(Entity<NinjaSuitComponent> ent, ref MapInitEvent args)
  48. {
  49. var (uid, comp) = ent;
  50. _actionContainer.EnsureAction(uid, ref comp.RecallKatanaActionEntity, comp.RecallKatanaAction);
  51. _actionContainer.EnsureAction(uid, ref comp.EmpActionEntity, comp.EmpAction);
  52. Dirty(uid, comp);
  53. }
  54. /// <summary>
  55. /// Add all the actions when a suit is equipped by a ninja.
  56. /// </summary>
  57. private void OnGetItemActions(Entity<NinjaSuitComponent> ent, ref GetItemActionsEvent args)
  58. {
  59. if (!_ninja.IsNinja(args.User))
  60. return;
  61. var comp = ent.Comp;
  62. args.AddAction(ref comp.RecallKatanaActionEntity, comp.RecallKatanaAction);
  63. args.AddAction(ref comp.EmpActionEntity, comp.EmpAction);
  64. }
  65. /// <summary>
  66. /// Only add toggle cloak action when equipped by a ninja.
  67. /// </summary>
  68. private void OnCloakCheck(Entity<NinjaSuitComponent> ent, ref ToggleClothingCheckEvent args)
  69. {
  70. if (!_ninja.IsNinja(args.User))
  71. args.Cancelled = true;
  72. }
  73. private void OnStarCheck(Entity<NinjaSuitComponent> ent, ref CheckItemCreatorEvent args)
  74. {
  75. if (!_ninja.IsNinja(args.User))
  76. args.Cancelled = true;
  77. }
  78. private void OnCreateStarAttempt(Entity<NinjaSuitComponent> ent, ref CreateItemAttemptEvent args)
  79. {
  80. if (CheckDisabled(ent, args.User))
  81. args.Cancelled = true;
  82. }
  83. /// <summary>
  84. /// Call the shared and serverside code for when anyone unequips a suit.
  85. /// </summary>
  86. private void OnUnequipped(Entity<NinjaSuitComponent> ent, ref GotUnequippedEvent args)
  87. {
  88. var user = args.Equipee;
  89. if (_ninja.NinjaQuery.TryComp(user, out var ninja))
  90. UserUnequippedSuit(ent, (user, ninja));
  91. }
  92. /// <summary>
  93. /// Force uncloaks the user and disables suit abilities.
  94. /// </summary>
  95. public void RevealNinja(Entity<NinjaSuitComponent?> ent, EntityUid user, bool disable = true)
  96. {
  97. if (!Resolve(ent, ref ent.Comp))
  98. return;
  99. var uid = ent.Owner;
  100. var comp = ent.Comp;
  101. if (_toggle.TryDeactivate(uid, user) || !disable)
  102. return;
  103. // previously cloaked, disable abilities for a short time
  104. _audio.PlayPredicted(comp.RevealSound, uid, user);
  105. Popup.PopupClient(Loc.GetString("ninja-revealed"), user, user, PopupType.MediumCaution);
  106. _useDelay.TryResetDelay(uid, id: comp.DisableDelayId);
  107. }
  108. private void OnActivateAttempt(Entity<NinjaSuitComponent> ent, ref ItemToggleActivateAttemptEvent args)
  109. {
  110. if (!_ninja.IsNinja(args.User))
  111. {
  112. args.Cancelled = true;
  113. return;
  114. }
  115. if (IsDisabled((ent, ent.Comp, null)))
  116. {
  117. args.Cancelled = true;
  118. args.Popup = Loc.GetString("ninja-suit-cooldown");
  119. }
  120. }
  121. /// <summary>
  122. /// Returns true if the suit is currently disabled
  123. /// </summary>
  124. public bool IsDisabled(Entity<NinjaSuitComponent?, UseDelayComponent?> ent)
  125. {
  126. if (!Resolve(ent, ref ent.Comp1, ref ent.Comp2))
  127. return false;
  128. return _useDelay.IsDelayed((ent, ent.Comp2), ent.Comp1.DisableDelayId);
  129. }
  130. protected bool CheckDisabled(Entity<NinjaSuitComponent> ent, EntityUid user)
  131. {
  132. if (IsDisabled((ent, ent.Comp, null)))
  133. {
  134. Popup.PopupEntity(Loc.GetString("ninja-suit-cooldown"), user, user, PopupType.Medium);
  135. return true;
  136. }
  137. return false;
  138. }
  139. /// <summary>
  140. /// Called when a suit is unequipped, not necessarily by a space ninja.
  141. /// In the future it might be changed to also have explicit deactivation via toggle.
  142. /// </summary>
  143. protected virtual void UserUnequippedSuit(Entity<NinjaSuitComponent> ent, Entity<SpaceNinjaComponent> user)
  144. {
  145. // mark the user as not wearing a suit
  146. _ninja.AssignSuit(user, null);
  147. // disable glove abilities
  148. if (user.Comp.Gloves is {} uid)
  149. _toggle.TryDeactivate(uid, user: user);
  150. }
  151. }