Files
tbd-station-14/Content.Server/Chemistry/EntitySystems/SolutionHeaterSystem.cs
metalgearsloth a89d4c750b Power stuff (#31314)
* Power stuff

- Add shared IsPowered
- Add shared ResolveApc
- Move PowerChangedEvent to shared for now
- Add SlimPoweredLight that actually functions how you'd expect a PoweredLight to function it id didn't have a bunch of bloat on it.

* big update

* boing
2024-08-25 22:18:42 +10:00

98 lines
3.2 KiB
C#

using Content.Server.Chemistry.Components;
using Content.Server.Chemistry.Containers.EntitySystems;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Components.SolutionManager;
using Content.Shared.Placeable;
using Content.Shared.Power;
namespace Content.Server.Chemistry.EntitySystems;
public sealed class SolutionHeaterSystem : EntitySystem
{
[Dependency] private readonly PowerReceiverSystem _powerReceiver = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainer = default!;
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SolutionHeaterComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<SolutionHeaterComponent, ItemPlacedEvent>(OnItemPlaced);
SubscribeLocalEvent<SolutionHeaterComponent, ItemRemovedEvent>(OnItemRemoved);
}
private void TurnOn(EntityUid uid)
{
_appearance.SetData(uid, SolutionHeaterVisuals.IsOn, true);
EnsureComp<ActiveSolutionHeaterComponent>(uid);
}
public bool TryTurnOn(EntityUid uid, ItemPlacerComponent? placer = null)
{
if (!Resolve(uid, ref placer))
return false;
if (placer.PlacedEntities.Count <= 0 || !_powerReceiver.IsPowered(uid))
return false;
TurnOn(uid);
return true;
}
public void TurnOff(EntityUid uid)
{
_appearance.SetData(uid, SolutionHeaterVisuals.IsOn, false);
RemComp<ActiveSolutionHeaterComponent>(uid);
}
private void OnPowerChanged(Entity<SolutionHeaterComponent> entity, ref PowerChangedEvent args)
{
var placer = Comp<ItemPlacerComponent>(entity);
if (args.Powered && placer.PlacedEntities.Count > 0)
{
TurnOn(entity);
}
else
{
TurnOff(entity);
}
}
private void OnItemPlaced(Entity<SolutionHeaterComponent> entity, ref ItemPlacedEvent args)
{
TryTurnOn(entity);
}
private void OnItemRemoved(Entity<SolutionHeaterComponent> entity, ref ItemRemovedEvent args)
{
var placer = Comp<ItemPlacerComponent>(entity);
if (placer.PlacedEntities.Count == 0) // Last entity was removed
TurnOff(entity);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<ActiveSolutionHeaterComponent, SolutionHeaterComponent, ItemPlacerComponent>();
while (query.MoveNext(out _, out _, out var heater, out var placer))
{
foreach (var heatingEntity in placer.PlacedEntities)
{
if (!TryComp<SolutionContainerManagerComponent>(heatingEntity, out var container))
continue;
var energy = heater.HeatPerSecond * frameTime;
foreach (var (_, soln) in _solutionContainer.EnumerateSolutions((heatingEntity, container)))
{
_solutionContainer.AddThermalEnergy(soln, energy);
}
}
}
}
}