ReformSystem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Content.Shared.Species.Components;
  2. using Content.Shared.Actions;
  3. using Content.Shared.DoAfter;
  4. using Content.Shared.Popups;
  5. using Content.Shared.Stunnable;
  6. using Content.Shared.Mind;
  7. using Content.Shared.Zombies;
  8. using Robust.Shared.Network;
  9. using Robust.Shared.Prototypes;
  10. using Robust.Shared.Serialization;
  11. using Robust.Shared.Timing;
  12. namespace Content.Shared.Species;
  13. public sealed partial class ReformSystem : EntitySystem
  14. {
  15. [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
  16. [Dependency] private readonly INetManager _netMan = default!;
  17. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  18. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  19. [Dependency] private readonly IPrototypeManager _protoManager = default!;
  20. [Dependency] private readonly SharedStunSystem _stunSystem = default!;
  21. [Dependency] private readonly IGameTiming _gameTiming = default!;
  22. [Dependency] private readonly SharedMindSystem _mindSystem = default!;
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. SubscribeLocalEvent<ReformComponent, MapInitEvent>(OnMapInit);
  27. SubscribeLocalEvent<ReformComponent, ComponentShutdown>(OnCompRemove);
  28. SubscribeLocalEvent<ReformComponent, ReformEvent>(OnReform);
  29. SubscribeLocalEvent<ReformComponent, ReformDoAfterEvent>(OnDoAfter);
  30. SubscribeLocalEvent<ReformComponent, EntityZombifiedEvent>(OnZombified);
  31. }
  32. private void OnMapInit(EntityUid uid, ReformComponent comp, MapInitEvent args)
  33. {
  34. // When the map is initialized, give them the action
  35. if (comp.ActionPrototype != default && !_protoManager.TryIndex<EntityPrototype>(comp.ActionPrototype, out var actionProto))
  36. return;
  37. _actionsSystem.AddAction(uid, ref comp.ActionEntity, out var reformAction, comp.ActionPrototype);
  38. // See if the action should start with a delay, and give it that starting delay if so.
  39. if (comp.StartDelayed && reformAction != null && reformAction.UseDelay != null)
  40. {
  41. var start = _gameTiming.CurTime;
  42. var end = _gameTiming.CurTime + reformAction.UseDelay.Value;
  43. _actionsSystem.SetCooldown(comp.ActionEntity!.Value, start, end);
  44. }
  45. }
  46. private void OnCompRemove(EntityUid uid, ReformComponent comp, ComponentShutdown args)
  47. {
  48. _actionsSystem.RemoveAction(uid, comp.ActionEntity);
  49. }
  50. private void OnReform(EntityUid uid, ReformComponent comp, ReformEvent args)
  51. {
  52. // Stun them when they use the action for the amount of reform time.
  53. if (comp.ShouldStun)
  54. _stunSystem.TryStun(uid, TimeSpan.FromSeconds(comp.ReformTime), true);
  55. _popupSystem.PopupClient(Loc.GetString(comp.PopupText, ("name", uid)), uid, uid);
  56. // Create a doafter & start it
  57. var doAfter = new DoAfterArgs(EntityManager, uid, comp.ReformTime, new ReformDoAfterEvent(), uid)
  58. {
  59. BreakOnMove = true,
  60. BlockDuplicate = true,
  61. BreakOnDamage = true,
  62. CancelDuplicate = true,
  63. RequireCanInteract = false,
  64. };
  65. _doAfterSystem.TryStartDoAfter(doAfter);
  66. args.Handled = true;
  67. }
  68. private void OnDoAfter(EntityUid uid, ReformComponent comp, ReformDoAfterEvent args)
  69. {
  70. if (args.Cancelled || args.Handled || comp.Deleted)
  71. return;
  72. if (_netMan.IsClient)
  73. return;
  74. // Spawn a new entity
  75. // This is, to an extent, taken from polymorph. I don't use polymorph for various reasons- most notably that this is permanent.
  76. var child = Spawn(comp.ReformPrototype, Transform(uid).Coordinates);
  77. // This transfers the mind to the new entity
  78. if (_mindSystem.TryGetMind(uid, out var mindId, out var mind))
  79. _mindSystem.TransferTo(mindId, child, mind: mind);
  80. // Delete the old entity
  81. QueueDel(uid);
  82. }
  83. private void OnZombified(EntityUid uid, ReformComponent comp, ref EntityZombifiedEvent args)
  84. {
  85. _actionsSystem.RemoveAction(uid, comp.ActionEntity); // Zombies can't reform
  86. }
  87. public sealed partial class ReformEvent : InstantActionEvent { }
  88. [Serializable, NetSerializable]
  89. public sealed partial class ReformDoAfterEvent : SimpleDoAfterEvent { }
  90. }