DoAfter Refactor (#13225)
Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
This commit is contained in:
@@ -10,7 +10,7 @@ using Robust.Shared.Prototypes;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Server.Popups;
|
||||
using Content.Server.DoAfter;
|
||||
using System.Threading;
|
||||
using Content.Shared.DoAfter;
|
||||
|
||||
namespace Content.Server.Body.Systems;
|
||||
|
||||
@@ -33,8 +33,7 @@ public sealed class InternalsSystem : EntitySystem
|
||||
SubscribeLocalEvent<InternalsComponent, ComponentStartup>(OnInternalsStartup);
|
||||
SubscribeLocalEvent<InternalsComponent, ComponentShutdown>(OnInternalsShutdown);
|
||||
SubscribeLocalEvent<InternalsComponent, GetVerbsEvent<InteractionVerb>>(OnGetInteractionVerbs);
|
||||
SubscribeLocalEvent<InternalsComponent, ToggleOtherInternalsCompleteEvent>(OnToggleOtherInternalsComplete);
|
||||
SubscribeLocalEvent<InternalsComponent, ToggleOtherInternalsCancelledEvent>(OnToggleOtherInternalCanceled);
|
||||
SubscribeLocalEvent<InternalsComponent, DoAfterEvent<InternalsData>>(OnDoAfter);
|
||||
}
|
||||
|
||||
private void OnGetInteractionVerbs(EntityUid uid, InternalsComponent component, GetVerbsEvent<InteractionVerb> args)
|
||||
@@ -59,9 +58,7 @@ public sealed class InternalsSystem : EntitySystem
|
||||
public void ToggleInternals(EntityUid uid, EntityUid user, bool force, InternalsComponent? internals = null)
|
||||
{
|
||||
if (!Resolve(uid, ref internals, false))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Toggle off if they're on
|
||||
if (AreInternalsWorking(internals))
|
||||
@@ -77,7 +74,7 @@ public sealed class InternalsSystem : EntitySystem
|
||||
return;
|
||||
}
|
||||
|
||||
var tank = FindBestGasTank(internals);
|
||||
var tank = FindBestGasTank(uid ,internals);
|
||||
|
||||
if (tank == null)
|
||||
{
|
||||
@@ -85,24 +82,26 @@ public sealed class InternalsSystem : EntitySystem
|
||||
return;
|
||||
}
|
||||
|
||||
var isUser = uid == user;
|
||||
|
||||
var internalsData = new InternalsData();
|
||||
|
||||
if (!force)
|
||||
{
|
||||
// Is the target not you? If yes, use a do-after to give them time to respond.
|
||||
//If no, do a short delay. There's no reason it should be beyond 1 second.
|
||||
var delay = uid != user ? internals.Delay : 1.0f;
|
||||
var delay = !isUser ? internals.Delay : 1.0f;
|
||||
|
||||
internals.CancelToken?.Cancel();
|
||||
internals.CancelToken = new CancellationTokenSource();
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(user, delay, internals.CancelToken.Token, uid)
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(user, delay, target:uid)
|
||||
{
|
||||
BreakOnUserMove = true,
|
||||
BreakOnDamage = true,
|
||||
BreakOnStun = true,
|
||||
BreakOnTargetMove = true,
|
||||
MovementThreshold = 0.1f,
|
||||
TargetFinishedEvent = new ToggleOtherInternalsCompleteEvent(user, tank),
|
||||
TargetCancelledEvent = new ToggleOtherInternalsCancelledEvent(),
|
||||
});
|
||||
RaiseOnUser = isUser,
|
||||
RaiseOnTarget = !isUser
|
||||
}, internalsData);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -110,20 +109,19 @@ public sealed class InternalsSystem : EntitySystem
|
||||
_gasTank.ConnectToInternals(tank);
|
||||
}
|
||||
|
||||
private void OnToggleOtherInternalsComplete(EntityUid uid, InternalsComponent component, ToggleOtherInternalsCompleteEvent ev)
|
||||
private void OnDoAfter(EntityUid uid, InternalsComponent component, DoAfterEvent<InternalsData> args)
|
||||
{
|
||||
component.CancelToken = null;
|
||||
ToggleInternals(uid, ev.User, true, component);
|
||||
}
|
||||
if (args.Cancelled || args.Handled)
|
||||
return;
|
||||
|
||||
private static void OnToggleOtherInternalCanceled(EntityUid uid, InternalsComponent component, ToggleOtherInternalsCancelledEvent ev)
|
||||
{
|
||||
component.CancelToken = null;
|
||||
ToggleInternals(uid, args.Args.User, true, component);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnInternalsStartup(EntityUid uid, InternalsComponent component, ComponentStartup args)
|
||||
{
|
||||
_alerts.ShowAlert(component.Owner, AlertType.Internals, GetSeverity(component));
|
||||
_alerts.ShowAlert(uid, AlertType.Internals, GetSeverity(component));
|
||||
}
|
||||
|
||||
private void OnInternalsShutdown(EntityUid uid, InternalsComponent component, ComponentShutdown args)
|
||||
@@ -138,7 +136,7 @@ public sealed class InternalsSystem : EntitySystem
|
||||
var gasTank = Comp<GasTankComponent>(component.GasTankEntity!.Value);
|
||||
args.Gas = _gasTank.RemoveAirVolume(gasTank, Atmospherics.BreathVolume);
|
||||
// TODO: Should listen to gas tank updates instead I guess?
|
||||
_alerts.ShowAlert(component.Owner, AlertType.Internals, GetSeverity(component));
|
||||
_alerts.ShowAlert(uid, AlertType.Internals, GetSeverity(component));
|
||||
}
|
||||
}
|
||||
public void DisconnectBreathTool(InternalsComponent component)
|
||||
@@ -168,12 +166,11 @@ public sealed class InternalsSystem : EntitySystem
|
||||
|
||||
public void DisconnectTank(InternalsComponent? component)
|
||||
{
|
||||
if (component == null) return;
|
||||
if (component == null)
|
||||
return;
|
||||
|
||||
if (TryComp(component.GasTankEntity, out GasTankComponent? tank))
|
||||
{
|
||||
_gasTank.DisconnectFromInternals(tank);
|
||||
}
|
||||
|
||||
component.GasTankEntity = null;
|
||||
_alerts.ShowAlert(component.Owner, AlertType.Internals, GetSeverity(component));
|
||||
@@ -185,9 +182,7 @@ public sealed class InternalsSystem : EntitySystem
|
||||
return false;
|
||||
|
||||
if (TryComp(component.GasTankEntity, out GasTankComponent? tank))
|
||||
{
|
||||
_gasTank.DisconnectFromInternals(tank);
|
||||
}
|
||||
|
||||
component.GasTankEntity = tankEntity;
|
||||
_alerts.ShowAlert(component.Owner, AlertType.Internals, GetSeverity(component));
|
||||
@@ -203,7 +198,8 @@ public sealed class InternalsSystem : EntitySystem
|
||||
|
||||
private short GetSeverity(InternalsComponent component)
|
||||
{
|
||||
if (component.BreathToolEntity == null || !AreInternalsWorking(component)) return 2;
|
||||
if (component.BreathToolEntity == null || !AreInternalsWorking(component))
|
||||
return 2;
|
||||
|
||||
// If pressure in the tank is below low pressure threshhold, flash warning on internals UI
|
||||
if (TryComp<GasTankComponent>(component.GasTankEntity, out var gasTank) && gasTank.IsLowPressure)
|
||||
@@ -212,7 +208,7 @@ public sealed class InternalsSystem : EntitySystem
|
||||
return 1;
|
||||
}
|
||||
|
||||
public GasTankComponent? FindBestGasTank(InternalsComponent component)
|
||||
public GasTankComponent? FindBestGasTank(EntityUid internalsOwner, InternalsComponent component)
|
||||
{
|
||||
// Prioritise
|
||||
// 1. back equipped tanks
|
||||
@@ -222,14 +218,14 @@ public sealed class InternalsSystem : EntitySystem
|
||||
InventoryComponent? inventory = null;
|
||||
ContainerManagerComponent? containerManager = null;
|
||||
|
||||
if (_inventory.TryGetSlotEntity(component.Owner, "back", out var backEntity, inventory, containerManager) &&
|
||||
if (_inventory.TryGetSlotEntity(internalsOwner, "back", out var backEntity, inventory, containerManager) &&
|
||||
TryComp<GasTankComponent>(backEntity, out var backGasTank) &&
|
||||
_gasTank.CanConnectToInternals(backGasTank))
|
||||
{
|
||||
return backGasTank;
|
||||
}
|
||||
|
||||
if (_inventory.TryGetSlotEntity(component.Owner, "suitstorage", out var entity, inventory, containerManager) &&
|
||||
if (_inventory.TryGetSlotEntity(internalsOwner, "suitstorage", out var entity, inventory, containerManager) &&
|
||||
TryComp<GasTankComponent>(entity, out var gasTank) &&
|
||||
_gasTank.CanConnectToInternals(gasTank))
|
||||
{
|
||||
@@ -238,12 +234,10 @@ public sealed class InternalsSystem : EntitySystem
|
||||
|
||||
var tanks = new List<GasTankComponent>();
|
||||
|
||||
foreach (var hand in _hands.EnumerateHands(component.Owner))
|
||||
foreach (var hand in _hands.EnumerateHands(internalsOwner))
|
||||
{
|
||||
if (TryComp(hand.HeldEntity, out gasTank) && _gasTank.CanConnectToInternals(gasTank))
|
||||
{
|
||||
tanks.Add(gasTank);
|
||||
}
|
||||
}
|
||||
|
||||
if (tanks.Count > 0)
|
||||
@@ -252,16 +246,14 @@ public sealed class InternalsSystem : EntitySystem
|
||||
return tanks[0];
|
||||
}
|
||||
|
||||
if (Resolve(component.Owner, ref inventory, false))
|
||||
if (Resolve(internalsOwner, ref inventory, false))
|
||||
{
|
||||
var enumerator = new InventorySystem.ContainerSlotEnumerator(component.Owner, inventory.TemplateId, _protoManager, _inventory, SlotFlags.POCKET | SlotFlags.BELT);
|
||||
var enumerator = new InventorySystem.ContainerSlotEnumerator(internalsOwner, inventory.TemplateId, _protoManager, _inventory, SlotFlags.POCKET | SlotFlags.BELT);
|
||||
|
||||
while (enumerator.MoveNext(out var container))
|
||||
{
|
||||
if (TryComp(container.ContainedEntity, out gasTank) && _gasTank.CanConnectToInternals(gasTank))
|
||||
{
|
||||
tanks.Add(gasTank);
|
||||
}
|
||||
}
|
||||
|
||||
if (tanks.Count > 0)
|
||||
@@ -273,11 +265,9 @@ public sealed class InternalsSystem : EntitySystem
|
||||
|
||||
return null;
|
||||
}
|
||||
private readonly record struct ToggleOtherInternalsCompleteEvent(EntityUid User, GasTankComponent Tank)
|
||||
{
|
||||
public readonly EntityUid User = User;
|
||||
public readonly GasTankComponent Tank = Tank;
|
||||
}
|
||||
|
||||
private readonly record struct ToggleOtherInternalsCancelledEvent;
|
||||
private record struct InternalsData
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user