6
StarHorizon_Public/Content.Shared/_White/Actions/PlasmaCostActionSystem.cs
2025-12-27 15:27:38 +03:00

65 lines
2.2 KiB
C#

using Content.Shared._White.Xenomorphs;
using Content.Shared._White.Xenomorphs.Plasma;
using Content.Shared.Actions;
using Content.Shared.Actions.Events;
using Content.Shared.FixedPoint;
using Content.Shared._White.Xenomorphs.Plasma.Components;
namespace Content.Shared._White.Actions;
public sealed class PlasmaCostActionSystem : EntitySystem
{
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly SharedPlasmaSystem _plasma = default!;
public override void Initialize()
{
SubscribeLocalEvent<PlasmaCostActionComponent, ActionRelayedEvent<PlasmaAmountChangeEvent>>(OnPlasmaAmountChange);
SubscribeLocalEvent<PlasmaCostActionComponent, ActionAttemptEvent>(OnActionAttempt); // Goobstation
}
/// <summary>
/// Checks if the performer has enough plasma for the action.
/// Returns true if the action should proceed, false if it should be blocked.
/// Goobstation
/// </summary>
public bool HasEnoughPlasma(EntityUid performer, FixedPoint2 cost)
{
if (cost <= 0)
return true;
return TryComp<PlasmaVesselComponent>(performer, out var plasmaVessel) &&
plasmaVessel.Plasma >= cost;
}
/// <summary>
/// Deducts plasma from the performer. Call this after confirming the action succeeds.
/// </summary>
public void DeductPlasma(EntityUid performer, FixedPoint2 cost)
{
if (cost > 0)
_plasma.ChangePlasmaAmount(performer, -cost);
}
[Obsolete("Use HasEnoughPlasma and DeductPlasma separately for better control")]
public bool CheckPlasmaCost(EntityUid performer, FixedPoint2 cost)
{
if (!HasEnoughPlasma(performer, cost))
return false;
DeductPlasma(performer, cost);
return true;
}
private void OnPlasmaAmountChange(EntityUid uid, PlasmaCostActionComponent component, ActionRelayedEvent<PlasmaAmountChangeEvent> args)
{
_actions.SetEnabled(uid, component.PlasmaCost <= args.Args.Amount);
}
private void OnActionAttempt(Entity<PlasmaCostActionComponent> ent, ref ActionAttemptEvent args)
{
if (!_plasma.HasPlasma(args.User, ent.Comp.PlasmaCost))
args.Cancelled = true;
}
}