FatExtractorSystem.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Linq;
  3. using Content.Server.Nutrition.Components;
  4. using Content.Server.Power.Components;
  5. using Content.Server.Power.EntitySystems;
  6. using Content.Server.Storage.Components;
  7. using Content.Shared.Emag.Components;
  8. using Content.Shared.Emag.Systems;
  9. using Content.Shared.Nutrition.Components;
  10. using Content.Shared.Nutrition.EntitySystems;
  11. using Content.Shared.Power;
  12. using Content.Shared.Storage.Components;
  13. using Robust.Shared.Audio.Systems;
  14. using Robust.Shared.Timing;
  15. namespace Content.Server.Nutrition.EntitySystems;
  16. /// <summary>
  17. /// This handles logic and interactions relating to <see cref="FatExtractorComponent"/>
  18. /// </summary>
  19. public sealed class FatExtractorSystem : EntitySystem
  20. {
  21. [Dependency] private readonly IGameTiming _timing = default!;
  22. [Dependency] private readonly EmagSystem _emag = default!;
  23. [Dependency] private readonly HungerSystem _hunger = default!;
  24. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  25. [Dependency] private readonly SharedAudioSystem _audio = default!;
  26. /// <inheritdoc/>
  27. public override void Initialize()
  28. {
  29. SubscribeLocalEvent<FatExtractorComponent, GotEmaggedEvent>(OnGotEmagged);
  30. SubscribeLocalEvent<FatExtractorComponent, StorageAfterCloseEvent>(OnClosed);
  31. SubscribeLocalEvent<FatExtractorComponent, StorageAfterOpenEvent>(OnOpen);
  32. SubscribeLocalEvent<FatExtractorComponent, PowerChangedEvent>(OnPowerChanged);
  33. }
  34. private void OnGotEmagged(EntityUid uid, FatExtractorComponent component, ref GotEmaggedEvent args)
  35. {
  36. if (!_emag.CompareFlag(args.Type, EmagType.Interaction))
  37. return;
  38. if (_emag.CheckFlag(uid, EmagType.Interaction))
  39. return;
  40. args.Handled = true;
  41. }
  42. private void OnClosed(EntityUid uid, FatExtractorComponent component, ref StorageAfterCloseEvent args)
  43. {
  44. StartProcessing(uid, component);
  45. }
  46. private void OnOpen(EntityUid uid, FatExtractorComponent component, ref StorageAfterOpenEvent args)
  47. {
  48. StopProcessing(uid, component);
  49. }
  50. private void OnPowerChanged(EntityUid uid, FatExtractorComponent component, ref PowerChangedEvent args)
  51. {
  52. if (!args.Powered)
  53. StopProcessing(uid, component);
  54. }
  55. public void StartProcessing(EntityUid uid, FatExtractorComponent? component = null, EntityStorageComponent? storage = null)
  56. {
  57. if (!Resolve(uid, ref component, ref storage))
  58. return;
  59. if (component.Processing)
  60. return;
  61. if (!this.IsPowered(uid, EntityManager))
  62. return;
  63. if (!TryGetValidOccupant(uid, out _, component, storage))
  64. return;
  65. component.Processing = true;
  66. _appearance.SetData(uid, FatExtractorVisuals.Processing, true);
  67. component.Stream = _audio.PlayPvs(component.ProcessSound, uid)?.Entity;
  68. component.NextUpdate = _timing.CurTime + component.UpdateTime;
  69. }
  70. public void StopProcessing(EntityUid uid, FatExtractorComponent? component = null)
  71. {
  72. if (!Resolve(uid, ref component))
  73. return;
  74. if (!component.Processing)
  75. return;
  76. component.Processing = false;
  77. _appearance.SetData(uid, FatExtractorVisuals.Processing, false);
  78. component.Stream = _audio.Stop(component.Stream);
  79. }
  80. public bool TryGetValidOccupant(EntityUid uid, [NotNullWhen(true)] out EntityUid? occupant, FatExtractorComponent? component = null, EntityStorageComponent? storage = null)
  81. {
  82. occupant = null;
  83. if (!Resolve(uid, ref component, ref storage))
  84. return false;
  85. occupant = storage.Contents.ContainedEntities.FirstOrDefault();
  86. if (!TryComp<HungerComponent>(occupant, out var hunger))
  87. return false;
  88. if (_hunger.GetHunger(hunger) < component.NutritionPerSecond)
  89. return false;
  90. if (hunger.CurrentThreshold < component.MinHungerThreshold && !_emag.CheckFlag(uid, EmagType.Interaction))
  91. return false;
  92. return true;
  93. }
  94. public override void Update(float frameTime)
  95. {
  96. base.Update(frameTime);
  97. var query = EntityQueryEnumerator<FatExtractorComponent, EntityStorageComponent>();
  98. while (query.MoveNext(out var uid, out var fat, out var storage))
  99. {
  100. if (TryGetValidOccupant(uid, out var occupant, fat, storage))
  101. {
  102. if (!fat.Processing)
  103. StartProcessing(uid, fat, storage);
  104. }
  105. else
  106. {
  107. StopProcessing(uid, fat);
  108. continue;
  109. }
  110. if (!fat.Processing)
  111. continue;
  112. if (_timing.CurTime < fat.NextUpdate)
  113. continue;
  114. fat.NextUpdate += fat.UpdateTime;
  115. _hunger.ModifyHunger(occupant.Value, -fat.NutritionPerSecond);
  116. fat.NutrientAccumulator += fat.NutritionPerSecond;
  117. if (fat.NutrientAccumulator >= fat.NutrientPerMeat)
  118. {
  119. fat.NutrientAccumulator -= fat.NutrientPerMeat;
  120. Spawn(fat.MeatPrototype, Transform(uid).Coordinates);
  121. }
  122. }
  123. }
  124. }