112 lines
3.9 KiB
C#
112 lines
3.9 KiB
C#
using Content.Server.Chemistry.Components;
|
|
using Content.Server.Chemistry.Containers.EntitySystems;
|
|
using Content.Server.Construction;
|
|
using Content.Server.Power.Components;
|
|
using Content.Server.Power.EntitySystems;
|
|
using Content.Shared.Chemistry;
|
|
using Content.Shared.Chemistry.Components.SolutionManager;
|
|
using Content.Shared.Placeable;
|
|
|
|
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, RefreshPartsEvent>(OnRefreshParts);
|
|
SubscribeLocalEvent<SolutionHeaterComponent, UpgradeExamineEvent>(OnUpgradeExamine);
|
|
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 OnRefreshParts(Entity<SolutionHeaterComponent> entity, ref RefreshPartsEvent args)
|
|
{
|
|
var heatRating = args.PartRatings[entity.Comp.MachinePartHeatMultiplier] - 1;
|
|
|
|
entity.Comp.HeatPerSecond = entity.Comp.BaseHeatPerSecond * MathF.Pow(entity.Comp.PartRatingHeatMultiplier, heatRating);
|
|
}
|
|
|
|
private void OnUpgradeExamine(Entity<SolutionHeaterComponent> entity, ref UpgradeExamineEvent args)
|
|
{
|
|
args.AddPercentageUpgrade("solution-heater-upgrade-heat", entity.Comp.HeatPerSecond / entity.Comp.BaseHeatPerSecond);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|