Power cells
This commit is contained in:
@@ -11,12 +11,11 @@ using YamlDotNet.RepresentationModel;
|
||||
namespace Content.Server.GameObjects.Components.Power
|
||||
{
|
||||
/// <summary>
|
||||
/// Feeds energy from the powernet and may have the ability to supply back into it
|
||||
/// Stores electrical energy. Used by power cells and SMESes.
|
||||
/// </summary>
|
||||
public class PowerStorageComponent : Component
|
||||
public abstract class PowerStorageComponent : Component
|
||||
{
|
||||
public override string Name => "PowerStorage";
|
||||
|
||||
[ViewVariables]
|
||||
public ChargeState LastChargeState { get; private set; } = ChargeState.Still;
|
||||
public DateTime LastChargeStateChange { get; private set; }
|
||||
|
||||
@@ -31,9 +30,15 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
/// <summary>
|
||||
/// Energy the battery is currently storing.
|
||||
/// In Joules.
|
||||
/// In most cases you should use <see cref="DeductCharge"/> and <see cref="AddCharge"/> to modify this.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public float Charge => _charge;
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public virtual float Charge
|
||||
{
|
||||
get => _charge;
|
||||
set => _charge = value;
|
||||
}
|
||||
|
||||
private float _charge = 0;
|
||||
|
||||
/// <summary>
|
||||
@@ -55,26 +60,6 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
[ViewVariables]
|
||||
public bool Full => Charge >= Capacity;
|
||||
|
||||
private bool _chargepowernet = false;
|
||||
|
||||
/// <summary>
|
||||
/// Do we distribute power into the powernet from our stores if the powernet requires it?
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool ChargePowernet
|
||||
{
|
||||
get => _chargepowernet;
|
||||
set
|
||||
{
|
||||
_chargepowernet = value;
|
||||
if (Owner.TryGetComponent(out PowerNodeComponent node))
|
||||
{
|
||||
if (node.Parent != null)
|
||||
node.Parent.UpdateStorageType(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
@@ -83,46 +68,14 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
serializer.DataField(ref _charge, "charge", 0);
|
||||
serializer.DataField(ref _chargeRate, "chargerate", 1000);
|
||||
serializer.DataField(ref _distributionRate, "distributionrate", 1000);
|
||||
serializer.DataField(ref _chargepowernet, "chargepowernet", false);
|
||||
}
|
||||
|
||||
public override void OnAdd()
|
||||
{
|
||||
base.OnAdd();
|
||||
|
||||
if (!Owner.TryGetComponent(out PowerNodeComponent node))
|
||||
{
|
||||
Owner.AddComponent<PowerNodeComponent>();
|
||||
node = Owner.GetComponent<PowerNodeComponent>();
|
||||
}
|
||||
node.OnPowernetConnect += PowernetConnect;
|
||||
node.OnPowernetDisconnect += PowernetDisconnect;
|
||||
node.OnPowernetRegenerate += PowernetRegenerate;
|
||||
}
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
if (Owner.TryGetComponent(out PowerNodeComponent node))
|
||||
{
|
||||
if (node.Parent != null)
|
||||
{
|
||||
node.Parent.RemovePowerStorage(this);
|
||||
}
|
||||
|
||||
node.OnPowernetConnect -= PowernetConnect;
|
||||
node.OnPowernetDisconnect -= PowernetDisconnect;
|
||||
node.OnPowernetRegenerate -= PowernetRegenerate;
|
||||
}
|
||||
|
||||
base.OnRemove();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the storage can supply the amount of charge directly requested
|
||||
/// </summary>
|
||||
public bool CanDeductCharge(float todeduct)
|
||||
public bool CanDeductCharge(float toDeduct)
|
||||
{
|
||||
if (Charge > todeduct)
|
||||
if (Charge > toDeduct)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@@ -130,14 +83,14 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
/// <summary>
|
||||
/// Deducts the requested charge from the energy storage
|
||||
/// </summary>
|
||||
public void DeductCharge(float todeduct)
|
||||
public virtual void DeductCharge(float toDeduct)
|
||||
{
|
||||
_charge = Math.Max(0, Charge - todeduct);
|
||||
_charge = Math.Max(0, Charge - toDeduct);
|
||||
LastChargeState = ChargeState.Discharging;
|
||||
LastChargeStateChange = DateTime.Now;
|
||||
}
|
||||
|
||||
public void AddCharge(float charge)
|
||||
public virtual void AddCharge(float charge)
|
||||
{
|
||||
_charge = Math.Min(Capacity, Charge + charge);
|
||||
LastChargeState = ChargeState.Charging;
|
||||
@@ -160,6 +113,24 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
return Math.Min(DistributionRate * frameTime, Charge);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to deduct a wattage over a certain amount of time.
|
||||
/// </summary>
|
||||
/// <param name="wattage">The wattage of the power drain.</param>
|
||||
/// <param name="frameTime">The amount of time in this "frame".</param>
|
||||
/// <returns>True if the amount of energy was deducted, false.</returns>
|
||||
public bool TryDeductWattage(float wattage, float frameTime)
|
||||
{
|
||||
var avail = AvailableCharge(frameTime);
|
||||
if (avail < wattage * frameTime)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DeductCharge(wattage * frameTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
public ChargeState GetChargeState()
|
||||
{
|
||||
return GetChargeState(TimeSpan.FromSeconds(1));
|
||||
@@ -182,35 +153,5 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
}
|
||||
AddCharge(RequestCharge(frameTime));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Node has become anchored to a powernet
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="eventarg"></param>
|
||||
private void PowernetConnect(object sender, PowernetEventArgs eventarg)
|
||||
{
|
||||
eventarg.Powernet.AddPowerStorage(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Node has had its powernet regenerated
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="eventarg"></param>
|
||||
private void PowernetRegenerate(object sender, PowernetEventArgs eventarg)
|
||||
{
|
||||
eventarg.Powernet.AddPowerStorage(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Node has become unanchored from a powernet
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="eventarg"></param>
|
||||
private void PowernetDisconnect(object sender, PowernetEventArgs eventarg)
|
||||
{
|
||||
eventarg.Powernet.RemovePowerStorage(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user