Event for PowerNetBattery CurrentSupply changing (#4570)

I re-used AmbientOnPowered but maybe that was lazy; I was doubtful anything would ever have ApcPowerReceiver and PowerNetBatteryComponent.
This commit is contained in:
metalgearsloth
2021-09-30 10:09:16 +10:00
committed by GitHub
parent accb83b344
commit 8365e8d52c
3 changed files with 50 additions and 0 deletions

View File

@@ -19,6 +19,8 @@ namespace Content.Server.Power.EntitySystems
private readonly HashSet<PowerNet> _powerNetReconnectQueue = new();
private readonly HashSet<ApcNet> _apcNetReconnectQueue = new();
private readonly Dictionary<PowerNetworkBatteryComponent, float> _lastSupply = new();
private readonly BatteryRampPegSolver _solver = new();
public override void Initialize()
@@ -150,6 +152,14 @@ namespace Content.Server.Power.EntitySystems
{
base.Update(frameTime);
// Setup for events.
{
foreach (var powerNetBattery in ComponentManager.EntityQuery<PowerNetworkBatteryComponent>())
{
_lastSupply[powerNetBattery] = powerNetBattery.CurrentSupply;
}
}
// Reconnect networks.
{
foreach (var apcNet in _apcNetReconnectQueue)
@@ -207,6 +217,27 @@ namespace Content.Server.Power.EntitySystems
RaiseLocalEvent(consumer.Owner.Uid, msg);
}
}
foreach (var powerNetBattery in ComponentManager.EntityQuery<PowerNetworkBatteryComponent>())
{
if (!_lastSupply.TryGetValue(powerNetBattery, out var lastPowerSupply))
{
lastPowerSupply = 0f;
}
var currentSupply = powerNetBattery.CurrentSupply;
if (lastPowerSupply == 0f && currentSupply != 0f)
{
RaiseLocalEvent(powerNetBattery.Owner.Uid, new PowerNetBatterySupplyEvent {Supply = true});
}
else if (lastPowerSupply > 0f && currentSupply == 0f)
{
RaiseLocalEvent(powerNetBattery.Owner.Uid, new PowerNetBatterySupplyEvent {Supply = false});
}
}
_lastSupply.Clear();
}
}
@@ -324,6 +355,14 @@ namespace Content.Server.Power.EntitySystems
}
}
/// <summary>
/// Raised whenever a <see cref="PowerNetworkBatteryComponent"/> changes from / to 0 CurrentSupply.
/// </summary>
public sealed class PowerNetBatterySupplyEvent : EntityEventArgs
{
public bool Supply { get; init; }
}
public struct PowerStatistics
{
public int CountNetworks;