92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using Content.Server.Power.Components;
|
|
using Content.Shared.PowerCell;
|
|
using Content.Shared.PowerCell.Components;
|
|
|
|
namespace Content.Server.PowerCell;
|
|
|
|
public sealed partial class PowerCellSystem
|
|
{
|
|
/*
|
|
* Handles PowerCellDraw
|
|
*/
|
|
|
|
private static readonly TimeSpan Delay = TimeSpan.FromSeconds(1);
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
var query = EntityQueryEnumerator<PowerCellDrawComponent, PowerCellSlotComponent>();
|
|
|
|
while (query.MoveNext(out var uid, out var comp, out var slot))
|
|
{
|
|
if (!comp.Drawing)
|
|
continue;
|
|
|
|
if (Timing.CurTime < comp.NextUpdateTime)
|
|
continue;
|
|
|
|
comp.NextUpdateTime += Delay;
|
|
|
|
if (!TryGetBatteryFromSlot(uid, out var batteryEnt, out var battery, slot))
|
|
continue;
|
|
|
|
if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate, battery))
|
|
continue;
|
|
|
|
comp.Drawing = false;
|
|
var ev = new PowerCellSlotEmptyEvent();
|
|
RaiseLocalEvent(uid, ref ev);
|
|
}
|
|
}
|
|
|
|
private void OnUnpaused(EntityUid uid, PowerCellDrawComponent component, ref EntityUnpausedEvent args)
|
|
{
|
|
component.NextUpdateTime += args.PausedTime;
|
|
}
|
|
|
|
private void OnDrawChargeChanged(EntityUid uid, PowerCellDrawComponent component, ref ChargeChangedEvent args)
|
|
{
|
|
// Update the bools for client prediction.
|
|
bool canDraw;
|
|
bool canUse;
|
|
|
|
if (component.UseRate > 0f)
|
|
{
|
|
canUse = args.Charge > component.UseRate;
|
|
}
|
|
else
|
|
{
|
|
canUse = true;
|
|
}
|
|
|
|
if (component.DrawRate > 0f)
|
|
{
|
|
canDraw = args.Charge > 0f;
|
|
}
|
|
else
|
|
{
|
|
canDraw = true;
|
|
}
|
|
|
|
if (canUse != component.CanUse || canDraw != component.CanDraw)
|
|
{
|
|
component.CanDraw = canDraw;
|
|
component.CanUse = canUse;
|
|
Dirty(component);
|
|
}
|
|
}
|
|
|
|
private void OnDrawCellChanged(EntityUid uid, PowerCellDrawComponent component, PowerCellChangedEvent args)
|
|
{
|
|
var canDraw = !args.Ejected && HasCharge(uid, float.MinValue);
|
|
var canUse = !args.Ejected && HasActivatableCharge(uid, component);
|
|
|
|
if (canUse != component.CanUse || canDraw != component.CanDraw)
|
|
{
|
|
component.CanDraw = canDraw;
|
|
component.CanUse = canUse;
|
|
Dirty(component);
|
|
}
|
|
}
|
|
}
|