TransferMindOnGibSystem.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Linq;
  2. using Content.Server.Body.Components;
  3. using Content.Shared.Mind;
  4. using Content.Shared.Mind.Components;
  5. using Content.Shared.Tag;
  6. using Robust.Shared.Random;
  7. namespace Content.Server.Mind;
  8. /// <summary>
  9. /// This handles transfering a target's mind
  10. /// to a different entity when they gib.
  11. /// used for skeletons.
  12. /// </summary>
  13. public sealed class TransferMindOnGibSystem : EntitySystem
  14. {
  15. [Dependency] private readonly IRobustRandom _random = default!;
  16. [Dependency] private readonly TagSystem _tag = default!;
  17. [Dependency] private readonly SharedMindSystem _mindSystem = default!;
  18. /// <inheritdoc/>
  19. public override void Initialize()
  20. {
  21. SubscribeLocalEvent<TransferMindOnGibComponent, BeingGibbedEvent>(OnGib);
  22. }
  23. private void OnGib(EntityUid uid, TransferMindOnGibComponent component, BeingGibbedEvent args)
  24. {
  25. if (!_mindSystem.TryGetMind(uid, out var mindId, out var mind))
  26. return;
  27. var validParts = args.GibbedParts.Where(p => _tag.HasTag(p, component.TargetTag)).ToHashSet();
  28. if (!validParts.Any())
  29. return;
  30. var ent = _random.Pick(validParts);
  31. _mindSystem.TransferTo(mindId, ent, mind: mind);
  32. }
  33. }