BatteryWeaponPowerCell tweaks (#33500)
* BatteryWeaponPowerCell tweaks * add update ammo ev & shuttle guns tweaks * MilonPL requested changes * revert changes in OnPowerCellChanged * Add events to get charge info & change current charge --------- Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
This commit is contained in:
@@ -38,4 +38,30 @@ namespace Content.Server.Power.Components
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public readonly record struct ChargeChangedEvent(float Charge, float MaxCharge);
|
||||
|
||||
/// <summary>
|
||||
/// Raised when it is necessary to get information about battery charges.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public sealed class GetChargeEvent : EntityEventArgs
|
||||
{
|
||||
public float CurrentCharge;
|
||||
public float MaxCharge;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised when it is necessary to change the current battery charge to a some value.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public sealed class ChangeChargeEvent : EntityEventArgs
|
||||
{
|
||||
public float OriginalValue;
|
||||
public float ResidualValue;
|
||||
|
||||
public ChangeChargeEvent(float value)
|
||||
{
|
||||
OriginalValue = value;
|
||||
ResidualValue = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ namespace Content.Server.Power.EntitySystems
|
||||
SubscribeLocalEvent<BatteryComponent, RejuvenateEvent>(OnBatteryRejuvenate);
|
||||
SubscribeLocalEvent<BatteryComponent, PriceCalculationEvent>(CalculateBatteryPrice);
|
||||
SubscribeLocalEvent<BatteryComponent, EmpPulseEvent>(OnEmpPulse);
|
||||
SubscribeLocalEvent<BatteryComponent, ChangeChargeEvent>(OnChangeCharge);
|
||||
SubscribeLocalEvent<BatteryComponent, GetChargeEvent>(OnGetCharge);
|
||||
|
||||
SubscribeLocalEvent<NetworkBatteryPreSync>(PreSync);
|
||||
SubscribeLocalEvent<NetworkBatteryPostSync>(PostSync);
|
||||
@@ -116,21 +118,26 @@ namespace Content.Server.Power.EntitySystems
|
||||
TrySetChargeCooldown(uid);
|
||||
}
|
||||
|
||||
private void OnChangeCharge(Entity<BatteryComponent> entity, ref ChangeChargeEvent args)
|
||||
{
|
||||
if (args.ResidualValue == 0)
|
||||
return;
|
||||
|
||||
args.ResidualValue -= ChangeCharge(entity, args.ResidualValue);
|
||||
}
|
||||
|
||||
private void OnGetCharge(Entity<BatteryComponent> entity, ref GetChargeEvent args)
|
||||
{
|
||||
args.CurrentCharge += entity.Comp.CurrentCharge;
|
||||
args.MaxCharge += entity.Comp.MaxCharge;
|
||||
}
|
||||
|
||||
public float UseCharge(EntityUid uid, float value, BatteryComponent? battery = null)
|
||||
{
|
||||
if (value <= 0 || !Resolve(uid, ref battery) || battery.CurrentCharge == 0)
|
||||
if (value <= 0 || !Resolve(uid, ref battery) || battery.CurrentCharge == 0)
|
||||
return 0;
|
||||
|
||||
var newValue = Math.Clamp(0, battery.CurrentCharge - value, battery.MaxCharge);
|
||||
var delta = newValue - battery.CurrentCharge;
|
||||
battery.CurrentCharge = newValue;
|
||||
|
||||
// Apply a cooldown to the entity's self recharge if needed.
|
||||
TrySetChargeCooldown(uid);
|
||||
|
||||
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
return delta;
|
||||
return ChangeCharge(uid, -value, battery);
|
||||
}
|
||||
|
||||
public void SetMaxCharge(EntityUid uid, float value, BatteryComponent? battery = null)
|
||||
@@ -164,6 +171,26 @@ namespace Content.Server.Power.EntitySystems
|
||||
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes the current battery charge by some value
|
||||
/// </summary>
|
||||
public float ChangeCharge(EntityUid uid, float value, BatteryComponent? battery = null)
|
||||
{
|
||||
if (!Resolve(uid, ref battery))
|
||||
return 0;
|
||||
|
||||
var newValue = Math.Clamp(0, battery.CurrentCharge + value, battery.MaxCharge);
|
||||
var delta = newValue - battery.CurrentCharge;
|
||||
battery.CurrentCharge = newValue;
|
||||
|
||||
TrySetChargeCooldown(uid);
|
||||
|
||||
var ev = new ChargeChangedEvent(battery.CurrentCharge, battery.MaxCharge);
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
return delta;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the entity has a self recharge and puts it on cooldown if applicable.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user