Files
tbd-station-14/Content.Server/GameObjects/Components/Power/PowerNetComponents/BatteryStorageComponent.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

74 lines
2.2 KiB
C#

using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
{
/// <summary>
/// Takes power via a <see cref="PowerConsumerComponent"/> to charge a <see cref="BatteryComponent"/>.
/// </summary>
[RegisterComponent]
public class BatteryStorageComponent : Component
{
public override string Name => "BatteryStorage";
[ViewVariables(VVAccess.ReadWrite)]
public int ActiveDrawRate { get => _activeDrawRate; set => SetActiveDrawRate(value); }
private int _activeDrawRate;
[ViewVariables]
private BatteryComponent _battery;
[ViewVariables]
public PowerConsumerComponent Consumer { get; private set; }
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _activeDrawRate, "activeDrawRate", 100);
}
public override void Initialize()
{
base.Initialize();
_battery = Owner.EnsureComponent<BatteryComponent>();
Consumer = Owner.EnsureComponent<PowerConsumerComponent>();
UpdateDrawRate();
}
public void Update(float frameTime)
{
//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;
UpdateDrawRate();
}
private void UpdateDrawRate()
{
if (_battery.BatteryState == BatteryState.Full)
{
SetConsumerDraw(0);
}
else
{
SetConsumerDraw(ActiveDrawRate);
}
}
private void SetConsumerDraw(int newConsumerDrawRate)
{
if (Consumer.DrawRate != newConsumerDrawRate)
{
Consumer.DrawRate = newConsumerDrawRate;
}
}
private void SetActiveDrawRate(int newEnabledDrawRate)
{
_activeDrawRate = newEnabledDrawRate;
UpdateDrawRate();
}
}
}