Files
tbd-station-14/Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryDischargerComponent.cs
DrSmugleaf b9196d0a10 Add a test that puts all components on an entity and checks for no exceptions (#1815)
* Add test that puts all components on an entity and checks for no exceptions

Also fix all the exceptions that happened because of this

* Add comments to the test

* Fix nullable errors

* Fix more nullable errors

* More nullable error fixes

* Unignore basic actor component

* Fix more nullable errors

* NULLABLE ERROR

* Add string interpolation

* Merge if checks

* Remove redundant pragma warning disable 649

* Address reviews

* Remove null wrappers around TryGetComponent

* Merge conflict fixes

* APC battery component error fix

* Fix power test

* Fix atmos mapgrid usages
2020-08-22 22:29:20 +02:00

75 lines
2.3 KiB
C#

using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
/// <summary>
/// Uses charge from a <see cref="BatteryComponent"/> to supply power via a <see cref="PowerSupplierComponent"/>.
/// </summary>
[RegisterComponent]
public class BatteryDischargerComponent : Component
{
public override string Name => "BatteryDischarger";
[ViewVariables]
private BatteryComponent _battery;
[ViewVariables]
private PowerSupplierComponent _supplier;
[ViewVariables(VVAccess.ReadWrite)]
public int ActiveSupplyRate { get => _activeSupplyRate; set => SetActiveSupplyRate(value); }
private int _activeSupplyRate;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _activeSupplyRate, "activeSupplyRate", 50);
}
public override void Initialize()
{
base.Initialize();
_battery = Owner.EnsureComponent<BatteryComponent>();
_supplier = Owner.EnsureComponent<PowerSupplierComponent>();
UpdateSupplyRate();
}
public void Update(float frameTime)
{
//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;
UpdateSupplyRate();
}
private void UpdateSupplyRate()
{
if (_battery.BatteryState == BatteryState.Empty)
{
SetSupplierSupplyRate(0);
}
else
{
SetSupplierSupplyRate(ActiveSupplyRate);
}
}
private void SetSupplierSupplyRate(int newSupplierSupplyRate)
{
if (_supplier.SupplyRate != newSupplierSupplyRate)
{
_supplier.SupplyRate = newSupplierSupplyRate;
}
}
private void SetActiveSupplyRate(int newEnabledSupplyRate)
{
_activeSupplyRate = newEnabledSupplyRate;
UpdateSupplyRate();
}
}
}