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:
@@ -1,4 +1,5 @@
|
|||||||
using Content.Server.Power.Components;
|
using Content.Server.Power.Components;
|
||||||
|
using Content.Server.Power.EntitySystems;
|
||||||
using Content.Shared.Audio;
|
using Content.Shared.Audio;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
|
|
||||||
@@ -10,6 +11,15 @@ namespace Content.Server.Audio
|
|||||||
{
|
{
|
||||||
base.Initialize();
|
base.Initialize();
|
||||||
SubscribeLocalEvent<AmbientOnPoweredComponent, PowerChangedEvent>(HandlePowerChange);
|
SubscribeLocalEvent<AmbientOnPoweredComponent, PowerChangedEvent>(HandlePowerChange);
|
||||||
|
SubscribeLocalEvent<AmbientOnPoweredComponent, PowerNetBatterySupplyEvent>(HandlePowerSupply);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandlePowerSupply(EntityUid uid, AmbientOnPoweredComponent component, PowerNetBatterySupplyEvent args)
|
||||||
|
{
|
||||||
|
if (!ComponentManager.TryGetComponent<AmbientSoundComponent>(uid, out var ambientSound)) return;
|
||||||
|
if (ambientSound.Enabled == args.Supply) return;
|
||||||
|
ambientSound.Enabled = args.Supply;
|
||||||
|
ambientSound.Dirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandlePowerChange(EntityUid uid, AmbientOnPoweredComponent component, PowerChangedEvent args)
|
private void HandlePowerChange(EntityUid uid, AmbientOnPoweredComponent component, PowerChangedEvent args)
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ namespace Content.Server.Power.EntitySystems
|
|||||||
private readonly HashSet<PowerNet> _powerNetReconnectQueue = new();
|
private readonly HashSet<PowerNet> _powerNetReconnectQueue = new();
|
||||||
private readonly HashSet<ApcNet> _apcNetReconnectQueue = new();
|
private readonly HashSet<ApcNet> _apcNetReconnectQueue = new();
|
||||||
|
|
||||||
|
private readonly Dictionary<PowerNetworkBatteryComponent, float> _lastSupply = new();
|
||||||
|
|
||||||
private readonly BatteryRampPegSolver _solver = new();
|
private readonly BatteryRampPegSolver _solver = new();
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
@@ -150,6 +152,14 @@ namespace Content.Server.Power.EntitySystems
|
|||||||
{
|
{
|
||||||
base.Update(frameTime);
|
base.Update(frameTime);
|
||||||
|
|
||||||
|
// Setup for events.
|
||||||
|
{
|
||||||
|
foreach (var powerNetBattery in ComponentManager.EntityQuery<PowerNetworkBatteryComponent>())
|
||||||
|
{
|
||||||
|
_lastSupply[powerNetBattery] = powerNetBattery.CurrentSupply;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Reconnect networks.
|
// Reconnect networks.
|
||||||
{
|
{
|
||||||
foreach (var apcNet in _apcNetReconnectQueue)
|
foreach (var apcNet in _apcNetReconnectQueue)
|
||||||
@@ -207,6 +217,27 @@ namespace Content.Server.Power.EntitySystems
|
|||||||
RaiseLocalEvent(consumer.Owner.Uid, msg);
|
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 struct PowerStatistics
|
||||||
{
|
{
|
||||||
public int CountNetworks;
|
public int CountNetworks;
|
||||||
|
|||||||
@@ -46,6 +46,7 @@
|
|||||||
placement:
|
placement:
|
||||||
mode: SnapgridCenter
|
mode: SnapgridCenter
|
||||||
components:
|
components:
|
||||||
|
- type: AmbientOnPowered
|
||||||
- type: AmbientSound
|
- type: AmbientSound
|
||||||
volume: -15
|
volume: -15
|
||||||
range: 2
|
range: 2
|
||||||
|
|||||||
Reference in New Issue
Block a user