/// <summary>
/// The max charges this action has.
/// </summary>
- [DataField, AutoNetworkedField, Access(Other = AccessPermissions.Read)]
+ [DataField, AutoNetworkedField]
public int MaxCharges = 3;
/// <summary>
/// <summary>
/// Adds the specified charges. Does not reset the accumulator.
/// </summary>
+ /// <param name="action">
+ /// The action to add charges to. If it doesn't have <see cref="LimitedChargesComponent"/>, it will be added.
+ /// </param>
+ /// <param name="addCharges">
+ /// The number of charges to add. Can be negative. Resulting charge count is clamped to [0, MaxCharges].
+ /// </param>
public void AddCharges(Entity<LimitedChargesComponent?, AutoRechargeComponent?> action, int addCharges)
{
if (addCharges == 0)
Dirty(action);
}
+ /// <summary>
+ /// Set the number of charges an action has.
+ /// </summary>
+ /// <param name="action">The action in question</param>
+ /// <param name="value">
+ /// The number of charges. Clamped to [0, MaxCharges].
+ /// </param>
+ /// <remarks>
+ /// This method doesn't implicitly add <see cref="LimitedChargesComponent"/>
+ /// unlike some other methods in this system.
+ /// </remarks>
public void SetCharges(Entity<LimitedChargesComponent?> action, int value)
{
- action.Comp ??= EnsureComp<LimitedChargesComponent>(action.Owner);
+ if (!Resolve(action, ref action.Comp))
+ return;
var adjusted = Math.Clamp(value, 0, action.Comp.MaxCharges);
Dirty(action);
}
+ /// <summary>
+ /// Sets the maximum charges of a given action.
+ /// </summary>
+ /// <param name="action">The action being modified.</param>
+ /// <param name="value">The new maximum charges of the action. Clamped to zero.</param>
+ /// <remarks>
+ /// Does not change the current charge count, or adjust the
+ /// accumulator for auto-recharge. It also doesn't implicitly add
+ /// <see cref="LimitedChargesComponent"/> unlike some other methods
+ /// in this system.
+ /// </remarks>
+ public void SetMaxCharges(Entity<LimitedChargesComponent?> action, int value)
+ {
+ if (!Resolve(action, ref action.Comp))
+ return;
+
+ // You can't have negative max charges (even zero is a bit goofy but eh)
+ var adjusted = Math.Max(0, value);
+ if (action.Comp.MaxCharges == adjusted)
+ return;
+
+ action.Comp.MaxCharges = adjusted;
+ Dirty(action);
+ }
+
/// <summary>
/// The next time a charge will be considered to be filled.
/// </summary>
using Content.Shared.Actions;
-using Content.Shared.Actions.Components;
+using Content.Shared.Actions.Components;
+using Content.Shared.Charges.Components;
using Content.Shared.Charges.Systems;
using Content.Shared.DoAfter;
using Content.Shared.Interaction.Events;
{
foreach (var (id, charges) in ent.Comp.SpellActions)
{
- var spell = _actionContainer.AddAction(ent, id);
- if (spell == null)
+ var action = _actionContainer.AddAction(ent, id);
+ if (action is not { } spell)
continue;
// Null means infinite charges.
if (charges is { } count)
- _sharedCharges.SetCharges(spell.Value, count);
- ent.Comp.Spells.Add(spell.Value);
+ {
+ EnsureComp<LimitedChargesComponent>(spell, out var chargeComp);
+ _sharedCharges.SetMaxCharges((spell, chargeComp), count);
+ _sharedCharges.SetCharges((spell, chargeComp), count);
+ }
+
+ ent.Comp.Spells.Add(spell);
}
}
foreach (var (id, charges) in ent.Comp.SpellActions)
{
EntityUid? actionId = null;
- if (_actions.AddAction(args.Args.User, ref actionId, id)
- && charges is { } count) // Null means infinite charges
- _sharedCharges.SetCharges(actionId.Value, count);
+ if (!_actions.AddAction(args.Args.User, ref actionId, id)
+ || charges is not { } count // Null means infinite charges
+ || !TryComp<LimitedChargesComponent>(actionId, out var chargeComp))
+ continue;
+
+ _sharedCharges.SetMaxCharges((actionId.Value, chargeComp), count);
+ _sharedCharges.SetCharges((actionId.Value, chargeComp), count);
}
}