1
0

RadioImplantSystem.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Content.Server.Radio.Components;
  2. using Content.Shared.Implants;
  3. using Content.Shared.Implants.Components;
  4. using Robust.Shared.Containers;
  5. namespace Content.Server.Implants;
  6. public sealed class RadioImplantSystem : EntitySystem
  7. {
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeLocalEvent<RadioImplantComponent, ImplantImplantedEvent>(OnImplantImplanted);
  12. SubscribeLocalEvent<RadioImplantComponent, EntGotRemovedFromContainerMessage>(OnRemove);
  13. }
  14. /// <summary>
  15. /// If implanted with a radio implant, installs the necessary intrinsic radio components
  16. /// </summary>
  17. private void OnImplantImplanted(Entity<RadioImplantComponent> ent, ref ImplantImplantedEvent args)
  18. {
  19. if (args.Implanted == null)
  20. return;
  21. var activeRadio = EnsureComp<ActiveRadioComponent>(args.Implanted.Value);
  22. foreach (var channel in ent.Comp.RadioChannels)
  23. {
  24. if (activeRadio.Channels.Add(channel))
  25. ent.Comp.ActiveAddedChannels.Add(channel);
  26. }
  27. EnsureComp<IntrinsicRadioReceiverComponent>(args.Implanted.Value);
  28. var intrinsicRadioTransmitter = EnsureComp<IntrinsicRadioTransmitterComponent>(args.Implanted.Value);
  29. foreach (var channel in ent.Comp.RadioChannels)
  30. {
  31. if (intrinsicRadioTransmitter.Channels.Add(channel))
  32. ent.Comp.TransmitterAddedChannels.Add(channel);
  33. }
  34. }
  35. /// <summary>
  36. /// Removes intrinsic radio components once the Radio Implant is removed
  37. /// </summary>
  38. private void OnRemove(Entity<RadioImplantComponent> ent, ref EntGotRemovedFromContainerMessage args)
  39. {
  40. if (TryComp<ActiveRadioComponent>(args.Container.Owner, out var activeRadioComponent))
  41. {
  42. foreach (var channel in ent.Comp.ActiveAddedChannels)
  43. {
  44. activeRadioComponent.Channels.Remove(channel);
  45. }
  46. ent.Comp.ActiveAddedChannels.Clear();
  47. if (activeRadioComponent.Channels.Count == 0)
  48. {
  49. RemCompDeferred<ActiveRadioComponent>(args.Container.Owner);
  50. }
  51. }
  52. if (!TryComp<IntrinsicRadioTransmitterComponent>(args.Container.Owner, out var radioTransmitterComponent))
  53. return;
  54. foreach (var channel in ent.Comp.TransmitterAddedChannels)
  55. {
  56. radioTransmitterComponent.Channels.Remove(channel);
  57. }
  58. ent.Comp.TransmitterAddedChannels.Clear();
  59. if (radioTransmitterComponent.Channels.Count == 0 || activeRadioComponent?.Channels.Count == 0)
  60. {
  61. RemCompDeferred<IntrinsicRadioTransmitterComponent>(args.Container.Owner);
  62. }
  63. }
  64. }