Some Power nullable & ComponentDependency (#3050)

* Power nullability

* AME nullability

* re-adds EnsureComponents

* conflict fix

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/RadiationCollectorComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryStorageComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryStorageComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryDischargerComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* Update Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryDischargerComponent.cs

Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>

* removes duplicate component assignment

Co-authored-by: py01 <pyronetics01@gmail.com>
Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com>
This commit is contained in:
collinlunn
2021-02-01 10:19:43 -06:00
committed by GitHub
parent 044040effe
commit b51b8cb182
18 changed files with 94 additions and 62 deletions

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
#nullable enable
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{

View File

@@ -1,6 +1,9 @@
using Robust.Shared.GameObjects;
#nullable enable
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using System;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
@@ -13,10 +16,10 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public override string Name => "BatteryDischarger";
[ViewVariables]
private BatteryComponent _battery;
[ComponentDependency] private BatteryComponent? _battery = default!;
[ViewVariables]
private PowerSupplierComponent _supplier;
[ComponentDependency] private PowerSupplierComponent? _supplier = default!;
[ViewVariables(VVAccess.ReadWrite)]
public int ActiveSupplyRate { get => _activeSupplyRate; set => SetActiveSupplyRate(value); }
@@ -31,14 +34,16 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public override void Initialize()
{
base.Initialize();
_battery = Owner.EnsureComponent<BatteryComponent>();
_supplier = Owner.EnsureComponent<PowerSupplierComponent>();
Owner.EnsureComponentWarn<BatteryComponent>();
Owner.EnsureComponentWarn<PowerSupplierComponent>();
UpdateSupplyRate();
}
public void Update(float frameTime)
{
if (_battery == null)
return;
//Simplified implementation - if the battery is empty, and charge is being added to the battery
//at a lower rate that this is using it, the charge is used without creating power supply.
_battery.CurrentCharge -= ActiveSupplyRate * frameTime;
@@ -47,6 +52,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void UpdateSupplyRate()
{
if (_battery == null)
return;
if (_battery.BatteryState == BatteryState.Empty)
{
SetSupplierSupplyRate(0);
@@ -59,6 +67,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void SetSupplierSupplyRate(int newSupplierSupplyRate)
{
if (_supplier == null)
return;
if (_supplier.SupplyRate != newSupplierSupplyRate)
{
_supplier.SupplyRate = newSupplierSupplyRate;

View File

@@ -1,4 +1,6 @@
using Robust.Shared.GameObjects;
#nullable enable
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -17,10 +19,12 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private int _activeDrawRate;
[ViewVariables]
private BatteryComponent _battery;
[ComponentDependency] private BatteryComponent? _battery = default!;
[ViewVariables]
public PowerConsumerComponent Consumer { get; private set; }
public PowerConsumerComponent? Consumer => _consumer;
[ComponentDependency] private PowerConsumerComponent? _consumer = default!;
public override void ExposeData(ObjectSerializer serializer)
{
@@ -31,21 +35,26 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public override void Initialize()
{
base.Initialize();
_battery = Owner.EnsureComponent<BatteryComponent>();
Consumer = Owner.EnsureComponent<PowerConsumerComponent>();
Owner.EnsureComponentWarn<BatteryComponent>();
Owner.EnsureComponentWarn<PowerConsumerComponent>();
UpdateDrawRate();
}
public void Update(float frameTime)
{
if (_consumer == null || _battery == null)
return;
//Simplified implementation - If a frame adds more power to a partially full battery than it can hold, the power is lost.
_battery.CurrentCharge += Consumer.ReceivedPower * frameTime;
_battery.CurrentCharge += _consumer.ReceivedPower * frameTime;
UpdateDrawRate();
}
private void UpdateDrawRate()
{
if (_battery == null)
return;
if (_battery.BatteryState == BatteryState.Full)
{
SetConsumerDraw(0);
@@ -58,9 +67,12 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void SetConsumerDraw(int newConsumerDrawRate)
{
if (Consumer.DrawRate != newConsumerDrawRate)
if (_consumer == null)
return;
if (_consumer.DrawRate != newConsumerDrawRate)
{
Consumer.DrawRate = newConsumerDrawRate;
_consumer.DrawRate = newConsumerDrawRate;
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
#nullable enable
using System.Collections.Generic;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Diagnostics;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;
@@ -34,7 +35,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public int ReceivedPower { get => _receivedPower; set => SetReceivedPower(value); }
private int _receivedPower;
public event EventHandler<ReceivedPowerChangedEventArgs> OnReceivedPowerChanged;
public event EventHandler<ReceivedPowerChangedEventArgs>? OnReceivedPowerChanged;
public override void ExposeData(ObjectSerializer serializer)
{

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
#nullable enable
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;

View File

@@ -1,20 +1,18 @@
#nullable enable
using System;
using System.Threading;
using Content.Server.Utility;
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.Components.Doors;
using Content.Shared.GameObjects.Components.Singularity;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Timer = Robust.Shared.Timers.Timer;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
@@ -27,19 +25,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private bool _enabled;
private TimeSpan _coolDownEnd;
private PhysicsComponent _collidableComponent;
[ComponentDependency] private readonly PhysicsComponent? _collidableComponent = default!;
public override void Initialize()
{
base.Initialize();
if (!Owner.TryGetComponent(out _collidableComponent))
{
Logger.Error("RadiationCollectorComponent created with no CollidableComponent");
return;
}
}
public override void HandleMessage(ComponentMessage message, IComponent component)
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
@@ -52,7 +40,8 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void OnAnchoredChanged()
{
if(_collidableComponent.Anchored) Owner.SnapToGrid();
if(_collidableComponent != null && _collidableComponent.Anchored)
Owner.SnapToGrid();
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
@@ -99,7 +88,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
protected void SetAppearance(RadiationCollectorVisualState state)
{
if (Owner.TryGetComponent(out AppearanceComponent appearance))
if (Owner.TryGetComponent<AppearanceComponent>(out var appearance))
{
appearance.SetData(RadiationCollectorVisuals.VisualState, state);
}

View File

@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
@@ -62,7 +63,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
private void UpdateSupply()
{
if (Owner.TryGetComponent(out PowerSupplierComponent supplier))
if (Owner.TryGetComponent<PowerSupplierComponent>(out var supplier))
{
supplier.SupplyRate = (int) (_maxSupply * _coverage);
}
@@ -72,7 +73,7 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
base.Initialize();
Owner.EnsureComponentWarn(out PowerSupplierComponent _);
Owner.EnsureComponentWarn<PowerSupplierComponent>();
UpdateSupply();
}
@@ -86,7 +87,9 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
public void OnBreak(BreakageEventArgs args)
{
var sprite = Owner.GetComponent<SpriteComponent>();
if (!Owner.TryGetComponent<SpriteComponent>(out var sprite))
return;
sprite.LayerSetState(0, "broken");
MaxSupply = 0;
}