OrganEffectSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-FileCopyrightText: 2025 Aiden <28298836+Aidenkrz@users.noreply.github.com>
  2. // SPDX-FileCopyrightText: 2025 Piras314 <p1r4s@proton.me>
  3. // SPDX-FileCopyrightText: 2025 gluesniffler <159397573+gluesniffler@users.noreply.github.com>
  4. //
  5. // SPDX-License-Identifier: AGPL-3.0-or-later
  6. // We keep this clone of the other system since I don't know yet if I'll need organ specific functions in the future.
  7. // will delete or refactor as time goes on.
  8. using Content.Shared._Shitmed.Body.Organ;
  9. using Content.Shared.Body.Organ;
  10. using Robust.Shared.Prototypes;
  11. using Robust.Shared.Serialization.Manager;
  12. using Robust.Shared.Timing;
  13. using System.Linq;
  14. using Robust.Shared.Network;
  15. namespace Content.Shared._Shitmed.BodyEffects;
  16. public sealed partial class OrganEffectSystem : EntitySystem
  17. {
  18. [Dependency] private readonly IComponentFactory _compFactory = default!;
  19. [Dependency] private readonly ISerializationManager _serManager = default!;
  20. [Dependency] private readonly IGameTiming _gameTiming = default!;
  21. [Dependency] private readonly INetManager _net = default!;
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<OrganComponent, OrganComponentsModifyEvent>(OnOrganComponentsModify);
  26. }
  27. // While I would love to kill this function, problem is that if we happen to have two parts that add the same
  28. // effect, removing one will remove both of them, since we cant tell what the source of a Component is.
  29. public override void Update(float frameTime)
  30. {
  31. base.Update(frameTime);
  32. var query = EntityQueryEnumerator<OrganEffectComponent, OrganComponent>();
  33. var now = _gameTiming.CurTime;
  34. while (query.MoveNext(out var uid, out var comp, out var part))
  35. {
  36. if (now < comp.NextUpdate
  37. || !comp.Active.Any()
  38. || part.Body is not { } body
  39. || !part.Enabled)
  40. continue;
  41. comp.NextUpdate = now + comp.Delay;
  42. AddComponents(body, uid, comp.Active, comp, false);
  43. }
  44. }
  45. private void OnOrganComponentsModify(Entity<OrganComponent> organEnt,
  46. ref OrganComponentsModifyEvent ev)
  47. {
  48. if (organEnt.Comp.OnAdd != null)
  49. {
  50. if (ev.Add)
  51. AddComponents(ev.Body, organEnt, organEnt.Comp.OnAdd);
  52. else
  53. RemoveComponents(ev.Body, organEnt, organEnt.Comp.OnAdd);
  54. }
  55. if (organEnt.Comp.OnRemove != null)
  56. {
  57. if (ev.Add)
  58. AddComponents(ev.Body, organEnt, organEnt.Comp.OnRemove);
  59. else
  60. RemoveComponents(ev.Body, organEnt, organEnt.Comp.OnRemove);
  61. }
  62. }
  63. private void AddComponents(EntityUid body,
  64. EntityUid part,
  65. ComponentRegistry reg,
  66. OrganEffectComponent? effectComp = null,
  67. bool? removeExisting = true)
  68. {
  69. if (!Resolve(part, ref effectComp, logMissing: false))
  70. return;
  71. EntityManager.AddComponents(body, reg, removeExisting ?? true);
  72. foreach (var (key, comp) in reg)
  73. {
  74. effectComp.Active[key] = comp;
  75. }
  76. }
  77. private void RemoveComponents(EntityUid body,
  78. EntityUid part,
  79. ComponentRegistry reg,
  80. OrganEffectComponent? effectComp = null)
  81. {
  82. if (!Resolve(part, ref effectComp, logMissing: false))
  83. return;
  84. EntityManager.RemoveComponents(body, reg);
  85. foreach (var key in reg.Keys)
  86. {
  87. effectComp.Active.Remove(key);
  88. }
  89. }
  90. }