61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using Content.Server.Chemistry.Components;
|
|
using Content.Server.Chemistry.Containers.EntitySystems;
|
|
using Content.Shared.Chemistry.Components;
|
|
using Content.Shared.Chemistry.Components.SolutionManager;
|
|
using Content.Shared.FixedPoint;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Content.Server.Chemistry.EntitySystems;
|
|
|
|
public sealed class SolutionRegenerationSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly SolutionContainerSystem _solutionContainer = default!;
|
|
[Dependency] private readonly IGameTiming _timing = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<SolutionRegenerationComponent, EntityUnpausedEvent>(OnUnpaused);
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
|
|
var query = EntityQueryEnumerator<SolutionRegenerationComponent, SolutionContainerManagerComponent>();
|
|
while (query.MoveNext(out var uid, out var regen, out var manager))
|
|
{
|
|
if (_timing.CurTime < regen.NextRegenTime)
|
|
continue;
|
|
|
|
// timer ignores if its full, it's just a fixed cycle
|
|
regen.NextRegenTime = _timing.CurTime + regen.Duration;
|
|
if (_solutionContainer.ResolveSolution((uid, manager), regen.SolutionName, ref regen.Solution, out var solution))
|
|
{
|
|
var amount = FixedPoint2.Min(solution.AvailableVolume, regen.Generated.Volume);
|
|
if (amount <= FixedPoint2.Zero)
|
|
continue;
|
|
|
|
// dont bother cloning and splitting if adding the whole thing
|
|
Solution generated;
|
|
if (amount == regen.Generated.Volume)
|
|
{
|
|
generated = regen.Generated;
|
|
}
|
|
else
|
|
{
|
|
generated = regen.Generated.Clone().SplitSolution(amount);
|
|
}
|
|
|
|
_solutionContainer.TryAddSolution(regen.Solution.Value, generated);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnUnpaused(Entity<SolutionRegenerationComponent> entity, ref EntityUnpausedEvent args)
|
|
{
|
|
entity.Comp.NextRegenTime += args.PausedTime;
|
|
}
|
|
}
|