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,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>())