Helper method for get charge level (#41601)

* get-charge-percent

* review
This commit is contained in:
Pok
2025-11-27 19:52:02 +02:00
committed by GitHub
parent 62fbac7a13
commit 8db29b4e87
9 changed files with 30 additions and 15 deletions

View File

@@ -238,12 +238,12 @@ namespace Content.Server.Light.EntitySystems
var appearanceComponent = EntityManager.GetComponentOrNull<AppearanceComponent>(uid);
var fraction = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
if (fraction >= 0.30)
var chargeFraction = _battery.GetChargeLevel(battery.Value.AsNullable());
if (chargeFraction >= 0.30)
{
_appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.FullPower, appearanceComponent);
}
else if (fraction >= 0.10)
else if (chargeFraction >= 0.10)
{
_appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.LowPower, appearanceComponent);
}

View File

@@ -43,8 +43,8 @@ public sealed class JammerSystem : SharedJammerSystem
}
else
{
var percentCharged = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
var chargeLevel = percentCharged switch
var chargeFraction = _battery.GetChargeLevel(battery.Value.AsNullable());
var chargeLevel = chargeFraction switch
{
> 0.50f => RadioJammerChargeLevel.High,
< 0.15f => RadioJammerChargeLevel.Low,

View File

@@ -39,9 +39,9 @@ public sealed partial class BorgSystem
if (now < comp.NextBroadcast)
continue;
var charge = 0f;
var chargeFraction = 0f;
if (_powerCell.TryGetBatteryFromSlot(uid, out var battery))
charge = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
chargeFraction = _battery.GetChargeLevel(battery.Value.AsNullable());
var hpPercent = CalcHP(uid);
@@ -52,7 +52,7 @@ public sealed partial class BorgSystem
comp.Sprite,
comp.Name,
meta.EntityName,
charge,
chargeFraction,
hpPercent,
chassis.ModuleCount,
hasBrain,

View File

@@ -114,7 +114,7 @@ public sealed partial class BorgSystem
return;
}
var chargePercent = (short)MathF.Round(_battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge * 10f);
var chargePercent = (short)MathF.Round(_battery.GetChargeLevel(battery.Value.AsNullable()) * 10f);
// we make sure 0 only shows if they have absolutely no battery.
// also account for floating point imprecision

View File

@@ -36,7 +36,7 @@ public sealed class DamagedSiliconAccentSystem : EntitySystem
}
else if (_powerCell.TryGetBatteryFromSlot(uid, out var battery))
{
currentChargeLevel = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge;
currentChargeLevel = _battery.GetChargeLevel(battery.Value.AsNullable());
}
currentChargeLevel = Math.Clamp(currentChargeLevel, 0.0f, 1.0f);
// Corrupt due to low power (drops characters on longer messages)

View File

@@ -71,8 +71,8 @@ public sealed class ChargerSystem : EntitySystem
if (!_powerCell.TryGetBatteryFromEntityOrSlot(contained, out var battery))
continue;
var chargePercentage = _battery.GetCharge(battery.Value.AsNullable()) / battery.Value.Comp.MaxCharge * 100;
args.PushMarkup(Loc.GetString("charger-content", ("chargePercentage", (int)chargePercentage)));
var chargePercent = _battery.GetChargeLevel(battery.Value.AsNullable()) * 100;
args.PushMarkup(Loc.GetString("charger-content", ("chargePercent", (int)chargePercent)));
}
}
}

View File

@@ -174,6 +174,21 @@ public sealed partial class PredictedBatterySystem
return charge;
}
/// <summary>
/// Gets the fraction of charge remaining (01).
/// </summary>
[PublicAPI]
public float GetChargeLevel(Entity<PredictedBatteryComponent?> ent)
{
if (!Resolve(ent, ref ent.Comp, false))
return 0f;
if (ent.Comp.MaxCharge <= 0f)
return 0f;
return GetCharge(ent) / ent.Comp.MaxCharge;
}
/// <summary>
/// Gets number of remaining uses for the given charge cost.
/// </summary>

View File

@@ -129,8 +129,8 @@ public sealed partial class PowerCellSystem : EntitySystem
private void OnBatteryExamined(Entity<PredictedBatteryComponent> ent, ref ExaminedEvent args)
{
var charge = _battery.GetCharge(ent.AsNullable()) / ent.Comp.MaxCharge * 100;
args.PushMarkup(Loc.GetString("power-cell-component-examine-details", ("currentCharge", $"{charge:F0}")));
var chargePercent = _battery.GetChargeLevel(ent.AsNullable()) * 100;
args.PushMarkup(Loc.GetString("power-cell-component-examine-details", ("currentCharge", $"{chargePercent:F0}")));
}
private void OnDrawRefreshChargeRate(Entity<PowerCellDrawComponent> ent, ref RefreshChargeRateEvent args)

View File

@@ -1,4 +1,4 @@
charger-examine = Charges at [color={$color}]{$chargeRate}W[/color].
charger-component-charge-rate = Charge rate
charger-content = Current charge is at [color=#5E7C16]{$chargePercentage}[/color]%.
charger-content = Current charge is at [color=#5E7C16]{$chargePercent}[/color]%.
charger-empty = There is nothing in the charger.