CatStation/Content.Shared/Actions/ConfirmableActionSystem.cs
Whatstone 7dd382c492
Add a confirmation message to the regenerative stasis action (#45506)
* add a confirmation message to the stasis action

* ConfirmableAction: toggled/untoggled bools

* move string

* network fields

---------

Co-authored-by: ScarKy0 <scarky0@onet.eu>
2026-08-21 06:21:03 +00:00

87 lines
2.5 KiB
C#

using Content.Shared.Actions.Components;
using Content.Shared.Actions.Events;
using Content.Shared.Popups;
using Robust.Shared.Timing;
namespace Content.Shared.Actions;
/// <summary>
/// Handles action priming, confirmation and automatic unpriming.
/// </summary>
public sealed partial class ConfirmableActionSystem : EntitySystem
{
[Dependency] private IGameTiming _timing = default!;
[Dependency] private SharedPopupSystem _popup = default!;
[Dependency] private EntityQuery<ActionComponent> _actionQuery = default!;
/// <inheritdoc/>
public override void Update(float frameTime)
{
base.Update(frameTime);
// handle automatic unpriming
var now = _timing.CurTime;
var query = EntityQueryEnumerator<ConfirmableActionComponent>();
while (query.MoveNext(out var uid, out var comp))
{
if (comp.NextUnprime is not {} time)
continue;
if (now >= time)
Unprime((uid, comp));
}
}
[SubscribeLocalEvent]
private void OnAttempt(Entity<ConfirmableActionComponent> ent, ref ActionAttemptEvent args)
{
if (args.Cancelled)
return;
// Check if we should be confirming based on the action's toggle status.
if (_actionQuery.TryComp(ent, out var action))
{
if (action.Toggled && !ent.Comp.ConfirmWhenToggled)
return;
if (!action.Toggled && !ent.Comp.ConfirmWhenUntoggled)
return;
}
// if not primed, prime it and cancel the action
if (ent.Comp.NextConfirm is not { } confirm)
{
Prime(ent, args.User);
args.Cancelled = true;
return;
}
// primed but the delay isnt over, cancel the action
if (_timing.CurTime < confirm)
{
args.Cancelled = true;
return;
}
// primed and delay has passed, let the action go through
Unprime(ent);
}
private void Prime(Entity<ConfirmableActionComponent> ent, EntityUid user)
{
var (uid, comp) = ent;
comp.NextConfirm = _timing.CurTime + comp.ConfirmDelay;
comp.NextUnprime = comp.NextConfirm + comp.PrimeTime;
Dirty(uid, comp);
_popup.PopupEntity(Loc.GetString(comp.Popup), user, user, PopupType.LargeCaution);
}
private void Unprime(Entity<ConfirmableActionComponent> ent)
{
var (uid, comp) = ent;
comp.NextConfirm = null;
comp.NextUnprime = null;
Dirty(uid, comp);
}
}