1
0

SharedRevolutionarySystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Content.Shared.IdentityManagement;
  2. using Content.Shared.Mindshield.Components;
  3. using Content.Shared.Popups;
  4. using Content.Shared.Revolutionary.Components;
  5. using Content.Shared.Stunnable;
  6. using Robust.Shared.GameStates;
  7. using Robust.Shared.Player;
  8. using Content.Shared.Antag;
  9. namespace Content.Shared.Revolutionary;
  10. public abstract class SharedRevolutionarySystem : EntitySystem
  11. {
  12. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  13. [Dependency] private readonly SharedStunSystem _sharedStun = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<MindShieldComponent, MapInitEvent>(MindShieldImplanted);
  18. SubscribeLocalEvent<RevolutionaryComponent, ComponentGetStateAttemptEvent>(OnRevCompGetStateAttempt);
  19. SubscribeLocalEvent<HeadRevolutionaryComponent, ComponentGetStateAttemptEvent>(OnRevCompGetStateAttempt);
  20. SubscribeLocalEvent<RevolutionaryComponent, ComponentStartup>(DirtyRevComps);
  21. SubscribeLocalEvent<HeadRevolutionaryComponent, ComponentStartup>(DirtyRevComps);
  22. SubscribeLocalEvent<ShowAntagIconsComponent, ComponentStartup>(DirtyRevComps);
  23. }
  24. /// <summary>
  25. /// When the mindshield is implanted in the rev it will popup saying they were deconverted. In Head Revs it will remove the mindshield component.
  26. /// </summary>
  27. private void MindShieldImplanted(EntityUid uid, MindShieldComponent comp, MapInitEvent init)
  28. {
  29. if (HasComp<HeadRevolutionaryComponent>(uid))
  30. {
  31. RemCompDeferred<MindShieldComponent>(uid);
  32. return;
  33. }
  34. if (HasComp<RevolutionaryComponent>(uid))
  35. {
  36. var stunTime = TimeSpan.FromSeconds(4);
  37. var name = Identity.Entity(uid, EntityManager);
  38. RemComp<RevolutionaryComponent>(uid);
  39. _sharedStun.TryParalyze(uid, stunTime, true);
  40. _popupSystem.PopupEntity(Loc.GetString("rev-break-control", ("name", name)), uid);
  41. }
  42. }
  43. /// <summary>
  44. /// Determines if a HeadRev component should be sent to the client.
  45. /// </summary>
  46. private void OnRevCompGetStateAttempt(EntityUid uid, HeadRevolutionaryComponent comp, ref ComponentGetStateAttemptEvent args)
  47. {
  48. args.Cancelled = !CanGetState(args.Player);
  49. }
  50. /// <summary>
  51. /// Determines if a Rev component should be sent to the client.
  52. /// </summary>
  53. private void OnRevCompGetStateAttempt(EntityUid uid, RevolutionaryComponent comp, ref ComponentGetStateAttemptEvent args)
  54. {
  55. args.Cancelled = !CanGetState(args.Player);
  56. }
  57. /// <summary>
  58. /// The criteria that determine whether a Rev/HeadRev component should be sent to a client.
  59. /// </summary>
  60. /// <param name="player"> The Player the component will be sent to.</param>
  61. /// <returns></returns>
  62. private bool CanGetState(ICommonSession? player)
  63. {
  64. //Apparently this can be null in replays so I am just returning true.
  65. if (player?.AttachedEntity is not {} uid)
  66. return true;
  67. if (HasComp<RevolutionaryComponent>(uid) || HasComp<HeadRevolutionaryComponent>(uid))
  68. return true;
  69. return HasComp<ShowAntagIconsComponent>(uid);
  70. }
  71. /// <summary>
  72. /// Dirties all the Rev components so they are sent to clients.
  73. ///
  74. /// We need to do this because if a rev component was not earlier sent to a client and for example the client
  75. /// becomes a rev then we need to send all the components to it. To my knowledge there is no way to do this on a
  76. /// per client basis so we are just dirtying all the components.
  77. /// </summary>
  78. private void DirtyRevComps<T>(EntityUid someUid, T someComp, ComponentStartup ev)
  79. {
  80. var revComps = AllEntityQuery<RevolutionaryComponent>();
  81. while (revComps.MoveNext(out var uid, out var comp))
  82. {
  83. Dirty(uid, comp);
  84. }
  85. var headRevComps = AllEntityQuery<HeadRevolutionaryComponent>();
  86. while (headRevComps.MoveNext(out var uid, out var comp))
  87. {
  88. Dirty(uid, comp);
  89. }
  90. }
  91. }