Move ApcPowerReceiverComponent Powered state to shared (#28206)

* Try syncing powered state to client

For some reason the client is not receiving the ApcPowerReceiverComponentState, so it's not working.

* Fix powered state not syncing to client

The client PowerReceiverSystem was abstract, which prevented it from
running initialize.

* Flip check so that it runs bigger checks first

PowerDisabled skips the others.
NeedsPower skips the receiving check.

* Disallow changing Powered manually

* Move Powered update to PowerReceiverSystem

* Move appearance to event subscription

* Move metadata component to AllEntityQuery

* Cleanup

* Move Powered update back to PowerNetSystem

It's easier to use the EntityQueries and it dosen't need to be updated
anywhere else.

* Put appearance updating back

* Move IsPowered to shared

* Simplify IsPowered

* Cleanup

* Remove duplicate PowerChangedEvent

PowerChangedEvent on ProviderChanged doesn't seem to be needed
PowerChangedEvent gets raised by in update if the power state changes
after a new provider is connected
This commit is contained in:
ShadowCommander
2024-05-29 23:46:22 -07:00
committed by GitHub
parent ce2446a612
commit e2bf127323
8 changed files with 98 additions and 27 deletions

View File

@@ -1,5 +1,6 @@
using Content.Server.Power.NodeGroups;
using Content.Server.Power.Pow3r;
using Content.Shared.Power.Components;
namespace Content.Server.Power.Components
{
@@ -8,11 +9,8 @@ namespace Content.Server.Power.Components
/// so that it can receive power from a <see cref="IApcNet"/>.
/// </summary>
[RegisterComponent]
public sealed partial class ApcPowerReceiverComponent : Component
public sealed partial class ApcPowerReceiverComponent : SharedApcPowerReceiverComponent
{
[ViewVariables]
public bool Powered => (MathHelper.CloseToPercent(NetworkLoad.ReceivingPower, Load) || !NeedsPower) && !PowerDisabled;
/// <summary>
/// Amount of charge this needs from an APC per second to function.
/// </summary>
@@ -33,7 +31,7 @@ namespace Content.Server.Power.Components
{
_needsPower = value;
// Reset this so next tick will do a power update.
PoweredLastUpdate = null;
Recalculate = true;
}
}
@@ -50,7 +48,8 @@ namespace Content.Server.Power.Components
set => NetworkLoad.Enabled = !value;
}
public bool? PoweredLastUpdate;
// TODO Is this needed? It forces a PowerChangedEvent when NeedsPower is toggled even if it changes to the same state.
public bool Recalculate;
[ViewVariables]
public PowerState.Load NetworkLoad { get; } = new PowerState.Load
@@ -66,10 +65,5 @@ namespace Content.Server.Power.Components
/// Does nothing on the client.
/// </summary>
[ByRefEvent]
public readonly record struct PowerChangedEvent(bool Powered, float ReceivingPower)
{
public readonly bool Powered = Powered;
public readonly float ReceivingPower = ReceivingPower;
}
public readonly record struct PowerChangedEvent(bool Powered, float ReceivingPower);
}