ECS BatteryComponent (#15337)

This commit is contained in:
Leon Friedrich
2023-04-19 22:10:08 +12:00
committed by GitHub
parent e95cd6e412
commit 9513ea66e4
6 changed files with 128 additions and 112 deletions

View File

@@ -1,3 +1,5 @@
using Content.Server.Power.EntitySystems;
namespace Content.Server.Power.Components
{
/// <summary>
@@ -12,17 +14,32 @@ namespace Content.Server.Power.Components
/// <summary>
/// Maximum charge of the battery in joules (ie. watt seconds)
/// </summary>
[ViewVariables(VVAccess.ReadWrite)] public float MaxCharge { get => _maxCharge; set => SetMaxCharge(value); }
[ViewVariables(VVAccess.ReadWrite)]
public float MaxCharge
{
get => _maxCharge;
[Obsolete("Use system method")]
set => _entMan.System<BatterySystem>().SetMaxCharge(Owner, value, this);
}
[DataField("maxCharge")]
private float _maxCharge;
[Access(typeof(BatterySystem))]
public float _maxCharge;
/// <summary>
/// Current charge of the battery in joules (ie. watt seconds)
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float CurrentCharge { get => _currentCharge; set => SetCurrentCharge(value); }
public float CurrentCharge
{
get => Charge;
[Obsolete("Use system method")]
set => _entMan.System<BatterySystem>().SetCharge(Owner, value, this);
}
[DataField("startingCharge")]
private float _currentCharge;
[Access(typeof(BatterySystem))]
public float Charge;
/// <summary>
/// True if the battery is fully charged.
@@ -36,61 +53,14 @@ namespace Content.Server.Power.Components
[ViewVariables(VVAccess.ReadWrite)]
public float PricePerJoule = 0.0001f;
/// <summary>
/// If sufficient charge is avaiable on the battery, use it. Otherwise, don't.
/// </summary>
public virtual bool TryUseCharge(float chargeToUse)
{
if (chargeToUse > CurrentCharge)
{
return false;
}
else
{
CurrentCharge -= chargeToUse;
return true;
}
}
public virtual float UseCharge(float toDeduct)
{
var chargeChangedBy = Math.Min(CurrentCharge, toDeduct);
CurrentCharge -= chargeChangedBy;
return chargeChangedBy;
}
public void FillFrom(BatteryComponent battery)
{
var powerDeficit = MaxCharge - CurrentCharge;
if (battery.TryUseCharge(powerDeficit))
{
CurrentCharge += powerDeficit;
}
else
{
CurrentCharge += battery.CurrentCharge;
battery.CurrentCharge = 0;
}
}
protected virtual void OnChargeChanged()
{
_entMan.EventBus.RaiseLocalEvent(Owner, new ChargeChangedEvent(), false);
}
private void SetMaxCharge(float newMax)
{
_maxCharge = Math.Max(newMax, 0);
_currentCharge = Math.Min(_currentCharge, MaxCharge);
OnChargeChanged();
}
private void SetCurrentCharge(float newChargeAmount)
{
_currentCharge = MathHelper.Clamp(newChargeAmount, 0, MaxCharge);
OnChargeChanged();
}
[Obsolete("Use system method")]
public bool TryUseCharge(float value)
=> _entMan.System<BatterySystem>().TryUseCharge(Owner, value, this);
}
public struct ChargeChangedEvent {}
/// <summary>
/// Raised when a battery's charge or capacity changes (capacity affects relative charge percentage).
/// </summary>
[ByRefEvent]
public record struct ChargeChangedEvent(float Charge, float MaxCharge);
}