Update PowerChangedEvent (#7503)

* Update PowerChangedEvent

* Comments
This commit is contained in:
Leon Friedrich
2022-04-14 01:50:42 +12:00
committed by GitHub
parent f440006ed9
commit 2777d2ef64
3 changed files with 24 additions and 31 deletions

View File

@@ -1,6 +1,5 @@
using Content.Server.Power.NodeGroups;
using Content.Server.Power.Pow3r;
using Content.Shared.Power;
namespace Content.Server.Power.Components
{
@@ -11,8 +10,6 @@ namespace Content.Server.Power.Components
[RegisterComponent]
public sealed class ApcPowerReceiverComponent : Component
{
[Dependency] private readonly IEntityManager _entMan = default!;
[ViewVariables]
public bool Powered => (MathHelper.CloseToPercent(NetworkLoad.ReceivingPower, Load) || !NeedsPower) && !PowerDisabled;
@@ -36,7 +33,7 @@ namespace Content.Server.Power.Components
{
_needsPower = value;
// Reset this so next tick will do a power update.
LastPowerReceived = float.NaN;
PoweredLastUpdate = null;
}
}
@@ -50,7 +47,7 @@ namespace Content.Server.Power.Components
[DataField("powerDisabled")]
public bool PowerDisabled { get => !NetworkLoad.Enabled; set => NetworkLoad.Enabled = !value; }
public float LastPowerReceived = float.NaN;
public bool? PoweredLastUpdate;
[ViewVariables]
public PowerState.Load NetworkLoad { get; } = new PowerState.Load
@@ -66,16 +63,6 @@ namespace Content.Server.Power.Components
base.OnRemove();
}
public void ApcPowerChanged()
{
_entMan.EventBus.RaiseLocalEvent(Owner, new PowerChangedEvent(Powered, NetworkLoad.ReceivingPower));
if (_entMan.TryGetComponent<AppearanceComponent?>(Owner, out var appearance))
{
appearance.SetData(PowerDeviceVisuals.Powered, Powered);
}
}
}
/// <summary>

View File

@@ -1,15 +1,10 @@
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.Power.Components;
using Content.Server.Power.NodeGroups;
using Content.Server.Power.Pow3r;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Content.Shared.Power;
namespace Content.Server.Power.EntitySystems
{
@@ -241,17 +236,22 @@ namespace Content.Server.Power.EntitySystems
RaiseLocalEvent(new NetworkBatteryPostSync());
// Send events where necessary.
// TODO: Instead of querying ALL power components every tick, and then checking if an event needs to be
// raised, should probably assemble a list of entity Uids during the actual solver steps.
{
var appearanceQuery = GetEntityQuery<AppearanceComponent>();
foreach (var apcReceiver in EntityManager.EntityQuery<ApcPowerReceiverComponent>())
{
var recv = apcReceiver.NetworkLoad.ReceivingPower;
ref var last = ref apcReceiver.LastPowerReceived;
var powered = apcReceiver.Powered;
if (powered == apcReceiver.PoweredLastUpdate)
continue;
if (!MathHelper.CloseToPercent(recv, last))
{
last = recv;
apcReceiver.ApcPowerChanged();
}
apcReceiver.PoweredLastUpdate = powered;
RaiseLocalEvent(apcReceiver.Owner, new PowerChangedEvent(apcReceiver.Powered, apcReceiver.NetworkLoad.ReceivingPower));
if (appearanceQuery.TryGetComponent(apcReceiver.Owner, out var appearance))
appearance.SetData(PowerDeviceVisuals.Powered, powered);
}
foreach (var consumer in EntityManager.EntityQuery<PowerConsumerComponent>())

View File

@@ -1,5 +1,7 @@
using Content.Server.Power.Components;
using Content.Server.Power.Components;
using Content.Shared.Examine;
using Content.Shared.Power;
namespace Content.Server.Power.EntitySystems
{
public sealed class PowerReceiverSystem : EntitySystem
@@ -72,10 +74,14 @@ namespace Content.Server.Power.EntitySystems
}
}
private static void ProviderChanged(ApcPowerReceiverComponent receiver)
private void ProviderChanged(ApcPowerReceiverComponent receiver)
{
receiver.NetworkLoad.LinkedNetwork = default;
receiver.ApcPowerChanged();
RaiseLocalEvent(receiver.Owner, new PowerChangedEvent(receiver.Powered, receiver.NetworkLoad.ReceivingPower));
if (TryComp(receiver.Owner, out AppearanceComponent? appearance))
appearance.SetData(PowerDeviceVisuals.Powered, receiver.Powered);
}
}
}