DoAfter Refactor (#13225)
Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Threading;
|
||||
using Content.Server.Atmos;
|
||||
using Content.Server.Atmos;
|
||||
using Content.Shared.Mech.Components;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
@@ -32,8 +31,6 @@ public sealed class MechComponent : SharedMechComponent
|
||||
[DataField("batteryRemovalDelay")]
|
||||
public float BatteryRemovalDelay = 2;
|
||||
|
||||
public CancellationTokenSource? EntryTokenSource;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not the mech is airtight.
|
||||
/// </summary>
|
||||
|
||||
@@ -46,29 +46,4 @@ public sealed class MechGrabberComponent : Component
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public Container ItemContainer = default!;
|
||||
public CancellationTokenSource? Token;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on the user when the grab is complete.
|
||||
/// </summary>
|
||||
public sealed class MechGrabberGrabFinishedEvent : EntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// The thing that was grabbed.
|
||||
/// </summary>
|
||||
public EntityUid Grabbed;
|
||||
|
||||
public MechGrabberGrabFinishedEvent(EntityUid grabbed)
|
||||
{
|
||||
Grabbed = grabbed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on the user when the grab fails
|
||||
/// </summary>
|
||||
public sealed class MechGrabberGrabCancelledEvent : EntityEventArgs
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ using Content.Server.Interaction;
|
||||
using Content.Server.Mech.Components;
|
||||
using Content.Server.Mech.Equipment.Components;
|
||||
using Content.Server.Mech.Systems;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Construction.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Mech;
|
||||
using Content.Shared.Mech.Equipment.Components;
|
||||
@@ -39,8 +41,7 @@ public sealed class MechGrabberSystem : EntitySystem
|
||||
SubscribeLocalEvent<MechGrabberComponent, AttemptRemoveMechEquipmentEvent>(OnAttemptRemove);
|
||||
|
||||
SubscribeLocalEvent<MechGrabberComponent, InteractNoHandEvent>(OnInteract);
|
||||
SubscribeLocalEvent<MechGrabberComponent, MechGrabberGrabFinishedEvent>(OnGrabFinished);
|
||||
SubscribeLocalEvent<MechGrabberComponent, MechGrabberGrabCancelledEvent>(OnGrabCancelled);
|
||||
SubscribeLocalEvent<MechGrabberComponent, DoAfterEvent>(OnMechGrab);
|
||||
}
|
||||
|
||||
private void OnGrabberMessage(EntityUid uid, MechGrabberComponent component, MechEquipmentUiMessageRelayEvent args)
|
||||
@@ -144,40 +145,37 @@ public sealed class MechGrabberSystem : EntitySystem
|
||||
if (mech.Energy + component.GrabEnergyDelta < 0)
|
||||
return;
|
||||
|
||||
if (component.Token != null)
|
||||
return;
|
||||
|
||||
if (!_interaction.InRangeUnobstructed(args.User, target))
|
||||
return;
|
||||
|
||||
args.Handled = true;
|
||||
component.Token = new();
|
||||
component.AudioStream = _audio.PlayPvs(component.GrabSound, uid);
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(args.User, component.GrabDelay, component.Token.Token, target, uid)
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(args.User, component.GrabDelay, target:target, used:uid)
|
||||
{
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
UsedFinishedEvent = new MechGrabberGrabFinishedEvent(target),
|
||||
UsedCancelledEvent = new MechGrabberGrabCancelledEvent()
|
||||
BreakOnUserMove = true
|
||||
});
|
||||
}
|
||||
|
||||
private void OnGrabFinished(EntityUid uid, MechGrabberComponent component, MechGrabberGrabFinishedEvent args)
|
||||
private void OnMechGrab(EntityUid uid, MechGrabberComponent component, DoAfterEvent args)
|
||||
{
|
||||
component.Token = null;
|
||||
if (args.Cancelled)
|
||||
{
|
||||
component.AudioStream?.Stop();
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.Handled || args.Args.Target == null)
|
||||
return;
|
||||
|
||||
if (!TryComp<MechEquipmentComponent>(uid, out var equipmentComponent) || equipmentComponent.EquipmentOwner == null)
|
||||
return;
|
||||
if (!_mech.TryChangeEnergy(equipmentComponent.EquipmentOwner.Value, component.GrabEnergyDelta))
|
||||
return;
|
||||
|
||||
component.ItemContainer.Insert(args.Grabbed);
|
||||
component.ItemContainer.Insert(args.Args.Target.Value);
|
||||
_mech.UpdateUserInterface(equipmentComponent.EquipmentOwner.Value);
|
||||
}
|
||||
|
||||
private void OnGrabCancelled(EntityUid uid, MechGrabberComponent component, MechGrabberGrabCancelledEvent args)
|
||||
{
|
||||
component.AudioStream?.Stop();
|
||||
component.Token = null;
|
||||
args.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Mech.Components;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Mech.Equipment.Components;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Mech.Systems;
|
||||
|
||||
@@ -20,15 +20,11 @@ public sealed class MechEquipmentSystem : EntitySystem
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<MechEquipmentComponent, AfterInteractEvent>(OnUsed);
|
||||
SubscribeLocalEvent<MechEquipmentComponent, MechEquipmentInstallFinished>(OnFinished);
|
||||
SubscribeLocalEvent<MechEquipmentComponent, MechEquipmentInstallCancelled>(OnCancelled);
|
||||
SubscribeLocalEvent<MechEquipmentComponent, DoAfterEvent<InsertEquipmentEvent>>(OnInsertEquipment);
|
||||
}
|
||||
|
||||
private void OnUsed(EntityUid uid, MechEquipmentComponent component, AfterInteractEvent args)
|
||||
{
|
||||
if (component.TokenSource != null)
|
||||
return;
|
||||
|
||||
if (args.Handled || !args.CanReach || args.Target == null)
|
||||
return;
|
||||
|
||||
@@ -50,26 +46,30 @@ public sealed class MechEquipmentSystem : EntitySystem
|
||||
|
||||
_popup.PopupEntity(Loc.GetString("mech-equipment-begin-install", ("item", uid)), mech);
|
||||
|
||||
component.TokenSource = new();
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(args.User, component.InstallDuration, component.TokenSource.Token, mech, uid)
|
||||
var insertEquipment = new InsertEquipmentEvent();
|
||||
var doAfterEventArgs = new DoAfterEventArgs(args.User, component.InstallDuration, target: mech, used: uid)
|
||||
{
|
||||
BreakOnStun = true,
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
UsedFinishedEvent = new MechEquipmentInstallFinished(mech),
|
||||
UsedCancelledEvent = new MechEquipmentInstallCancelled()
|
||||
});
|
||||
BreakOnUserMove = true
|
||||
};
|
||||
|
||||
_doAfter.DoAfter(doAfterEventArgs, insertEquipment);
|
||||
}
|
||||
|
||||
private void OnFinished(EntityUid uid, MechEquipmentComponent component, MechEquipmentInstallFinished args)
|
||||
private void OnInsertEquipment(EntityUid uid, MechEquipmentComponent component, DoAfterEvent<InsertEquipmentEvent> args)
|
||||
{
|
||||
component.TokenSource = null;
|
||||
_popup.PopupEntity(Loc.GetString("mech-equipment-finish-install", ("item", uid)), args.Mech);
|
||||
_mech.InsertEquipment(args.Mech, uid);
|
||||
if (args.Handled || args.Cancelled || args.Args.Target == null)
|
||||
return;
|
||||
|
||||
_popup.PopupEntity(Loc.GetString("mech-equipment-finish-install", ("item", uid)), args.Args.Target.Value);
|
||||
_mech.InsertEquipment(args.Args.Target.Value, uid);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnCancelled(EntityUid uid, MechEquipmentComponent component, MechEquipmentInstallCancelled args)
|
||||
private sealed class InsertEquipmentEvent : EntityEventArgs
|
||||
{
|
||||
component.TokenSource = null;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Mech.Components;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Tools;
|
||||
using Content.Server.Wires;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Mech;
|
||||
using Content.Shared.Mech.Components;
|
||||
using Content.Shared.Mech.EntitySystems;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Movement.Events;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Content.Shared.Verbs;
|
||||
@@ -47,12 +45,9 @@ public sealed class MechSystem : SharedMechSystem
|
||||
SubscribeLocalEvent<MechComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<MechComponent, GetVerbsEvent<AlternativeVerb>>(OnAlternativeVerb);
|
||||
SubscribeLocalEvent<MechComponent, MechOpenUiEvent>(OnOpenUi);
|
||||
SubscribeLocalEvent<MechComponent, MechEntryFinishedEvent>(OnEntryFinished);
|
||||
SubscribeLocalEvent<MechComponent, MechEntryCanclledEvent>(OnEntryExitCancelled);
|
||||
SubscribeLocalEvent<MechComponent, MechExitFinishedEvent>(OnExitFinished);
|
||||
SubscribeLocalEvent<MechComponent, MechExitCanclledEvent>(OnEntryExitCancelled);
|
||||
SubscribeLocalEvent<MechComponent, MechRemoveBatteryFinishedEvent>(OnRemoveBatteryFinished);
|
||||
SubscribeLocalEvent<MechComponent, MechRemoveBatteryCancelledEvent>(OnRemoveBatteryCancelled);
|
||||
SubscribeLocalEvent<MechComponent, DoAfterEvent<RemoveBatteryEvent>>(OnRemoveBattery);
|
||||
SubscribeLocalEvent<MechComponent, DoAfterEvent<MechEntryEvent>>(OnMechEntry);
|
||||
SubscribeLocalEvent<MechComponent, DoAfterEvent<MechExitEvent>>(OnMechExit);
|
||||
|
||||
SubscribeLocalEvent<MechComponent, DamageChangedEvent>(OnDamageChanged);
|
||||
SubscribeLocalEvent<MechComponent, MechEquipmentRemoveMessage>(OnRemoveEquipmentMessage);
|
||||
@@ -87,32 +82,29 @@ public sealed class MechSystem : SharedMechSystem
|
||||
return;
|
||||
}
|
||||
|
||||
if (component.EntryTokenSource == null &&
|
||||
TryComp<ToolComponent>(args.Used, out var tool) &&
|
||||
tool.Qualities.Contains("Prying") &&
|
||||
component.BatterySlot.ContainedEntity != null)
|
||||
if (TryComp<ToolComponent>(args.Used, out var tool) && tool.Qualities.Contains("Prying") && component.BatterySlot.ContainedEntity != null)
|
||||
{
|
||||
component.EntryTokenSource = new();
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(args.User, component.BatteryRemovalDelay, component.EntryTokenSource.Token, uid, args.Target)
|
||||
var removeBattery = new RemoveBatteryEvent();
|
||||
|
||||
var doAfterEventArgs = new DoAfterEventArgs(args.User, component.BatteryRemovalDelay, target: uid, used: args.Target)
|
||||
{
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
TargetFinishedEvent = new MechRemoveBatteryFinishedEvent(),
|
||||
TargetCancelledEvent = new MechRemoveBatteryCancelledEvent()
|
||||
});
|
||||
BreakOnUserMove = true
|
||||
};
|
||||
|
||||
_doAfter.DoAfter(doAfterEventArgs, removeBattery);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRemoveBatteryFinished(EntityUid uid, MechComponent component, MechRemoveBatteryFinishedEvent args)
|
||||
private void OnRemoveBattery(EntityUid uid, MechComponent component, DoAfterEvent<RemoveBatteryEvent> args)
|
||||
{
|
||||
component.EntryTokenSource = null;
|
||||
if (args.Cancelled || args.Handled)
|
||||
return;
|
||||
|
||||
RemoveBattery(uid, component);
|
||||
_actionBlocker.UpdateCanMove(uid);
|
||||
}
|
||||
|
||||
private void OnRemoveBatteryCancelled(EntityUid uid, MechComponent component, MechRemoveBatteryCancelledEvent args)
|
||||
{
|
||||
component.EntryTokenSource = null;
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnMapInit(EntityUid uid, MechComponent component, MapInitEvent args)
|
||||
@@ -171,16 +163,14 @@ public sealed class MechSystem : SharedMechSystem
|
||||
Text = Loc.GetString("mech-verb-enter"),
|
||||
Act = () =>
|
||||
{
|
||||
if (component.EntryTokenSource != null)
|
||||
return;
|
||||
component.EntryTokenSource = new CancellationTokenSource();
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(args.User, component.EntryDelay, component.EntryTokenSource.Token, uid)
|
||||
var mechEntryEvent = new MechEntryEvent();
|
||||
var doAfterEventArgs = new DoAfterEventArgs(args.User, component.EntryDelay, target: uid)
|
||||
{
|
||||
BreakOnUserMove = true,
|
||||
BreakOnStun = true,
|
||||
TargetFinishedEvent = new MechEntryFinishedEvent(args.User),
|
||||
TargetCancelledEvent = new MechEntryCanclledEvent()
|
||||
});
|
||||
};
|
||||
|
||||
_doAfter.DoAfter(doAfterEventArgs, mechEntryEvent);
|
||||
}
|
||||
};
|
||||
var openUiVerb = new AlternativeVerb //can't hijack someone else's mech
|
||||
@@ -199,45 +189,46 @@ public sealed class MechSystem : SharedMechSystem
|
||||
Priority = 1, // Promote to top to make ejecting the ALT-click action
|
||||
Act = () =>
|
||||
{
|
||||
if (component.EntryTokenSource != null)
|
||||
return;
|
||||
if (args.User == component.PilotSlot.ContainedEntity)
|
||||
{
|
||||
TryEject(uid, component);
|
||||
return;
|
||||
}
|
||||
|
||||
component.EntryTokenSource = new CancellationTokenSource();
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(args.User, component.ExitDelay, component.EntryTokenSource.Token, uid)
|
||||
var mechExitEvent = new MechExitEvent();
|
||||
var doAfterEventArgs = new DoAfterEventArgs(args.User, component.ExitDelay, target: uid)
|
||||
{
|
||||
BreakOnUserMove = true,
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnStun = true,
|
||||
TargetFinishedEvent = new MechExitFinishedEvent(),
|
||||
TargetCancelledEvent = new MechExitCanclledEvent()
|
||||
});
|
||||
BreakOnStun = true
|
||||
};
|
||||
|
||||
_doAfter.DoAfter(doAfterEventArgs, mechExitEvent);
|
||||
}
|
||||
};
|
||||
args.Verbs.Add(ejectVerb);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEntryFinished(EntityUid uid, MechComponent component, MechEntryFinishedEvent args)
|
||||
private void OnMechEntry(EntityUid uid, MechComponent component, DoAfterEvent<MechEntryEvent> args)
|
||||
{
|
||||
component.EntryTokenSource = null;
|
||||
TryInsert(uid, args.User, component);
|
||||
if (args.Cancelled || args.Handled)
|
||||
return;
|
||||
|
||||
TryInsert(uid, args.Args.User, component);
|
||||
_actionBlocker.UpdateCanMove(uid);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnExitFinished(EntityUid uid, MechComponent component, MechExitFinishedEvent args)
|
||||
private void OnMechExit(EntityUid uid, MechComponent component, DoAfterEvent<MechExitEvent> args)
|
||||
{
|
||||
component.EntryTokenSource = null;
|
||||
if (args.Cancelled || args.Handled)
|
||||
return;
|
||||
|
||||
TryEject(uid, component);
|
||||
}
|
||||
|
||||
private void OnEntryExitCancelled(EntityUid uid, MechComponent component, EntityEventArgs args)
|
||||
{
|
||||
component.EntryTokenSource = null;
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnDamageChanged(EntityUid uid, SharedMechComponent component, DamageChangedEvent args)
|
||||
@@ -454,4 +445,27 @@ public sealed class MechSystem : SharedMechSystem
|
||||
args.Handled = true;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when the battery is successfully removed from the mech,
|
||||
/// on both success and failure
|
||||
/// </summary>
|
||||
private sealed class RemoveBatteryEvent : EntityEventArgs
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when a person enters a mech, on both success and failure
|
||||
/// </summary>
|
||||
private sealed class MechEntryEvent : EntityEventArgs
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when a person removes someone from a mech,
|
||||
/// on both success and failure
|
||||
/// </summary>
|
||||
private sealed class MechExitEvent : EntityEventArgs
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user