SharedSpaceNinjaSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Content.Shared.Clothing.Components;
  2. using Content.Shared.Ninja.Components;
  3. using Content.Shared.Weapons.Melee.Events;
  4. using Content.Shared.Weapons.Ranged.Events;
  5. using Content.Shared.Popups;
  6. using System.Diagnostics.CodeAnalysis;
  7. namespace Content.Shared.Ninja.Systems;
  8. /// <summary>
  9. /// Provides shared ninja API, handles being attacked revealing ninja and stops guns from shooting.
  10. /// </summary>
  11. public abstract class SharedSpaceNinjaSystem : EntitySystem
  12. {
  13. [Dependency] protected readonly SharedNinjaSuitSystem Suit = default!;
  14. [Dependency] protected readonly SharedPopupSystem Popup = default!;
  15. public EntityQuery<SpaceNinjaComponent> NinjaQuery;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. NinjaQuery = GetEntityQuery<SpaceNinjaComponent>();
  20. SubscribeLocalEvent<SpaceNinjaComponent, AttackedEvent>(OnNinjaAttacked);
  21. SubscribeLocalEvent<SpaceNinjaComponent, MeleeAttackEvent>(OnNinjaAttack);
  22. SubscribeLocalEvent<SpaceNinjaComponent, ShotAttemptedEvent>(OnShotAttempted);
  23. }
  24. public bool IsNinja([NotNullWhen(true)] EntityUid? uid)
  25. {
  26. return NinjaQuery.HasComp(uid);
  27. }
  28. /// <summary>
  29. /// Set the ninja's worn suit entity
  30. /// </summary>
  31. public void AssignSuit(Entity<SpaceNinjaComponent> ent, EntityUid? suit)
  32. {
  33. if (ent.Comp.Suit == suit)
  34. return;
  35. ent.Comp.Suit = suit;
  36. Dirty(ent, ent.Comp);
  37. }
  38. /// <summary>
  39. /// Set the ninja's worn gloves entity
  40. /// </summary>
  41. public void AssignGloves(Entity<SpaceNinjaComponent> ent, EntityUid? gloves)
  42. {
  43. if (ent.Comp.Gloves == gloves)
  44. return;
  45. ent.Comp.Gloves = gloves;
  46. Dirty(ent, ent.Comp);
  47. }
  48. /// <summary>
  49. /// Bind a katana entity to a ninja, letting it be recalled and dash.
  50. /// Does nothing if the player is not a ninja or already has a katana bound.
  51. /// </summary>
  52. public void BindKatana(Entity<SpaceNinjaComponent?> ent, EntityUid katana)
  53. {
  54. if (!NinjaQuery.Resolve(ent, ref ent.Comp, false) || ent.Comp.Katana != null)
  55. return;
  56. ent.Comp.Katana = katana;
  57. Dirty(ent, ent.Comp);
  58. }
  59. /// <summary>
  60. /// Gets the user's battery and tries to use some charge from it, returning true if successful.
  61. /// Serverside only.
  62. /// </summary>
  63. public virtual bool TryUseCharge(EntityUid user, float charge)
  64. {
  65. return false;
  66. }
  67. /// <summary>
  68. /// Handle revealing ninja if cloaked when attacked.
  69. /// </summary>
  70. private void OnNinjaAttacked(Entity<SpaceNinjaComponent> ent, ref AttackedEvent args)
  71. {
  72. TryRevealNinja(ent, disable: true);
  73. }
  74. /// <summary>
  75. /// Handle revealing ninja if cloaked when attacking.
  76. /// Only reveals, there is no cooldown.
  77. /// </summary>
  78. private void OnNinjaAttack(Entity<SpaceNinjaComponent> ent, ref MeleeAttackEvent args)
  79. {
  80. TryRevealNinja(ent, disable: false);
  81. }
  82. private void TryRevealNinja(Entity<SpaceNinjaComponent> ent, bool disable)
  83. {
  84. if (ent.Comp.Suit is {} uid && TryComp<NinjaSuitComponent>(ent.Comp.Suit, out var suit))
  85. Suit.RevealNinja((uid, suit), ent, disable: disable);
  86. }
  87. /// <summary>
  88. /// Require ninja to fight with HONOR, no guns!
  89. /// </summary>
  90. private void OnShotAttempted(Entity<SpaceNinjaComponent> ent, ref ShotAttemptedEvent args)
  91. {
  92. Popup.PopupClient(Loc.GetString("gun-disabled"), ent, ent);
  93. args.Cancel();
  94. }
  95. }