BodySystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Content.Server.Body.Components;
  2. using Content.Server.Ghost;
  3. using Content.Server.Humanoid;
  4. using Content.Shared.Body.Components;
  5. using Content.Shared.Body.Part;
  6. using Content.Shared.Body.Systems;
  7. using Content.Shared.Humanoid;
  8. using Content.Shared.Mind;
  9. using Content.Shared.Mobs.Systems;
  10. using Content.Shared.Movement.Events;
  11. using Content.Shared.Movement.Systems;
  12. using Robust.Shared.Audio;
  13. using Robust.Shared.Timing;
  14. using System.Numerics;
  15. namespace Content.Server.Body.Systems;
  16. public sealed class BodySystem : SharedBodySystem
  17. {
  18. [Dependency] private readonly GhostSystem _ghostSystem = default!;
  19. [Dependency] private readonly IGameTiming _gameTiming = default!;
  20. [Dependency] private readonly HumanoidAppearanceSystem _humanoidSystem = default!;
  21. [Dependency] private readonly MobStateSystem _mobState = default!;
  22. [Dependency] private readonly SharedMindSystem _mindSystem = default!;
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. SubscribeLocalEvent<BodyComponent, MoveInputEvent>(OnRelayMoveInput);
  27. SubscribeLocalEvent<BodyComponent, ApplyMetabolicMultiplierEvent>(OnApplyMetabolicMultiplier);
  28. }
  29. private void OnRelayMoveInput(Entity<BodyComponent> ent, ref MoveInputEvent args)
  30. {
  31. // If they haven't actually moved then ignore it.
  32. if ((args.Entity.Comp.HeldMoveButtons &
  33. (MoveButtons.Down | MoveButtons.Left | MoveButtons.Up | MoveButtons.Right)) == 0x0)
  34. {
  35. return;
  36. }
  37. if (_mobState.IsDead(ent) && _mindSystem.TryGetMind(ent, out var mindId, out var mind))
  38. {
  39. mind.TimeOfDeath ??= _gameTiming.RealTime;
  40. _ghostSystem.OnGhostAttempt(mindId, canReturnGlobal: true, mind: mind);
  41. }
  42. }
  43. private void OnApplyMetabolicMultiplier(
  44. Entity<BodyComponent> ent,
  45. ref ApplyMetabolicMultiplierEvent args)
  46. {
  47. foreach (var organ in GetBodyOrgans(ent, ent))
  48. {
  49. RaiseLocalEvent(organ.Id, ref args);
  50. }
  51. }
  52. protected override void AddPart(
  53. Entity<BodyComponent?> bodyEnt,
  54. Entity<BodyPartComponent> partEnt,
  55. string slotId)
  56. {
  57. // TODO: Predict this probably.
  58. base.AddPart(bodyEnt, partEnt, slotId);
  59. var layer = partEnt.Comp.ToHumanoidLayers();
  60. if (layer != null)
  61. {
  62. var layers = HumanoidVisualLayersExtension.Sublayers(layer.Value);
  63. _humanoidSystem.SetLayersVisibility(bodyEnt.Owner, layers, visible: true);
  64. }
  65. }
  66. protected override void RemovePart(
  67. Entity<BodyComponent?> bodyEnt,
  68. Entity<BodyPartComponent> partEnt,
  69. string slotId)
  70. {
  71. base.RemovePart(bodyEnt, partEnt, slotId);
  72. if (!TryComp<HumanoidAppearanceComponent>(bodyEnt, out var humanoid))
  73. return;
  74. var layer = partEnt.Comp.ToHumanoidLayers();
  75. if (layer is null)
  76. return;
  77. var layers = HumanoidVisualLayersExtension.Sublayers(layer.Value);
  78. _humanoidSystem.SetLayersVisibility((bodyEnt, humanoid), layers, visible: false);
  79. }
  80. public override HashSet<EntityUid> GibBody(
  81. EntityUid bodyId,
  82. bool gibOrgans = false,
  83. BodyComponent? body = null,
  84. bool launchGibs = true,
  85. Vector2? splatDirection = null,
  86. float splatModifier = 1,
  87. Angle splatCone = default,
  88. SoundSpecifier? gibSoundOverride = null
  89. )
  90. {
  91. if (!Resolve(bodyId, ref body, logMissing: false)
  92. || TerminatingOrDeleted(bodyId)
  93. || EntityManager.IsQueuedForDeletion(bodyId))
  94. {
  95. return new HashSet<EntityUid>();
  96. }
  97. var xform = Transform(bodyId);
  98. if (xform.MapUid is null)
  99. return new HashSet<EntityUid>();
  100. var gibs = base.GibBody(bodyId, gibOrgans, body, launchGibs: launchGibs,
  101. splatDirection: splatDirection, splatModifier: splatModifier, splatCone:splatCone);
  102. var ev = new BeingGibbedEvent(gibs);
  103. RaiseLocalEvent(bodyId, ref ev);
  104. QueueDel(bodyId);
  105. return gibs;
  106. }
  107. }