1
0

SolutionHeaterSystem.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Content.Server.Chemistry.Components;
  2. using Content.Server.Power.Components;
  3. using Content.Server.Power.EntitySystems;
  4. using Content.Shared.Chemistry.EntitySystems;
  5. using Content.Shared.Chemistry;
  6. using Content.Shared.Chemistry.Components.SolutionManager;
  7. using Content.Shared.Placeable;
  8. using Content.Shared.Power;
  9. namespace Content.Server.Chemistry.EntitySystems;
  10. public sealed class SolutionHeaterSystem : EntitySystem
  11. {
  12. [Dependency] private readonly PowerReceiverSystem _powerReceiver = default!;
  13. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  14. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
  15. /// <inheritdoc/>
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<SolutionHeaterComponent, PowerChangedEvent>(OnPowerChanged);
  20. SubscribeLocalEvent<SolutionHeaterComponent, ItemPlacedEvent>(OnItemPlaced);
  21. SubscribeLocalEvent<SolutionHeaterComponent, ItemRemovedEvent>(OnItemRemoved);
  22. }
  23. private void TurnOn(EntityUid uid)
  24. {
  25. _appearance.SetData(uid, SolutionHeaterVisuals.IsOn, true);
  26. EnsureComp<ActiveSolutionHeaterComponent>(uid);
  27. }
  28. public bool TryTurnOn(EntityUid uid, ItemPlacerComponent? placer = null)
  29. {
  30. if (!Resolve(uid, ref placer))
  31. return false;
  32. if (placer.PlacedEntities.Count <= 0 || !_powerReceiver.IsPowered(uid))
  33. return false;
  34. TurnOn(uid);
  35. return true;
  36. }
  37. public void TurnOff(EntityUid uid)
  38. {
  39. _appearance.SetData(uid, SolutionHeaterVisuals.IsOn, false);
  40. RemComp<ActiveSolutionHeaterComponent>(uid);
  41. }
  42. private void OnPowerChanged(Entity<SolutionHeaterComponent> entity, ref PowerChangedEvent args)
  43. {
  44. var placer = Comp<ItemPlacerComponent>(entity);
  45. if (args.Powered && placer.PlacedEntities.Count > 0)
  46. {
  47. TurnOn(entity);
  48. }
  49. else
  50. {
  51. TurnOff(entity);
  52. }
  53. }
  54. private void OnItemPlaced(Entity<SolutionHeaterComponent> entity, ref ItemPlacedEvent args)
  55. {
  56. TryTurnOn(entity);
  57. }
  58. private void OnItemRemoved(Entity<SolutionHeaterComponent> entity, ref ItemRemovedEvent args)
  59. {
  60. var placer = Comp<ItemPlacerComponent>(entity);
  61. if (placer.PlacedEntities.Count == 0) // Last entity was removed
  62. TurnOff(entity);
  63. }
  64. public override void Update(float frameTime)
  65. {
  66. base.Update(frameTime);
  67. var query = EntityQueryEnumerator<ActiveSolutionHeaterComponent, SolutionHeaterComponent, ItemPlacerComponent>();
  68. while (query.MoveNext(out _, out _, out var heater, out var placer))
  69. {
  70. foreach (var heatingEntity in placer.PlacedEntities)
  71. {
  72. if (!TryComp<SolutionContainerManagerComponent>(heatingEntity, out var container))
  73. continue;
  74. var energy = heater.HeatPerSecond * frameTime;
  75. foreach (var (_, soln) in _solutionContainer.EnumerateSolutions((heatingEntity, container)))
  76. {
  77. _solutionContainer.AddThermalEnergy(soln, energy);
  78. }
  79. }
  80. }
  81. }
  82. }