DoAfter Refactor (#13225)
Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
This commit is contained in:
12
Content.Shared/DoAfter/ActiveDoAfterComponent.cs
Normal file
12
Content.Shared/DoAfter/ActiveDoAfterComponent.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Shared.DoAfter;
|
||||
|
||||
/// <summary>
|
||||
/// Added to entities that are currently performing any doafters.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class ActiveDoAfterComponent : Component
|
||||
{
|
||||
|
||||
}
|
||||
82
Content.Shared/DoAfter/DoAfter.cs
Normal file
82
Content.Shared/DoAfter/DoAfter.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.DoAfter;
|
||||
[Serializable, NetSerializable]
|
||||
[DataDefinition]
|
||||
public sealed class DoAfter
|
||||
{
|
||||
[NonSerialized]
|
||||
[Obsolete]
|
||||
public Task<DoAfterStatus> AsTask;
|
||||
|
||||
[NonSerialized]
|
||||
[Obsolete("Will be obsolete for EventBus")]
|
||||
public TaskCompletionSource<DoAfterStatus> Tcs;
|
||||
|
||||
//TODO: Should be merged into here
|
||||
public readonly DoAfterEventArgs EventArgs;
|
||||
|
||||
//ID so the client DoAfterSystem can track
|
||||
public byte ID;
|
||||
|
||||
public bool Cancelled = false;
|
||||
|
||||
//Cache the delay so the timer properly shows
|
||||
public float Delay;
|
||||
|
||||
//Keep track of the time this DoAfter started
|
||||
public TimeSpan StartTime;
|
||||
|
||||
//Keep track of the time this DoAfter was cancelled
|
||||
public TimeSpan CancelledTime;
|
||||
|
||||
//How long has the do after been running?
|
||||
public TimeSpan Elapsed = TimeSpan.Zero;
|
||||
|
||||
/// <summary>
|
||||
/// Accrued time when cancelled.
|
||||
/// </summary>
|
||||
public TimeSpan CancelledElapsed = TimeSpan.Zero;
|
||||
|
||||
public EntityCoordinates UserGrid;
|
||||
public EntityCoordinates TargetGrid;
|
||||
|
||||
[NonSerialized]
|
||||
public Action<bool>? Done;
|
||||
|
||||
#pragma warning disable RA0004
|
||||
public DoAfterStatus Status => AsTask.IsCompletedSuccessfully ? AsTask.Result : DoAfterStatus.Running;
|
||||
#pragma warning restore RA0004
|
||||
|
||||
// NeedHand
|
||||
public readonly string? ActiveHand;
|
||||
public readonly EntityUid? ActiveItem;
|
||||
|
||||
public DoAfter(DoAfterEventArgs eventArgs, IEntityManager entityManager)
|
||||
{
|
||||
EventArgs = eventArgs;
|
||||
StartTime = IoCManager.Resolve<IGameTiming>().CurTime;
|
||||
|
||||
if (eventArgs.BreakOnUserMove)
|
||||
UserGrid = entityManager.GetComponent<TransformComponent>(eventArgs.User).Coordinates;
|
||||
|
||||
if (eventArgs.Target != null && eventArgs.BreakOnTargetMove)
|
||||
// Target should never be null if the bool is set.
|
||||
TargetGrid = entityManager.GetComponent<TransformComponent>(eventArgs.Target!.Value).Coordinates;
|
||||
|
||||
// For this we need to stay on the same hand slot and need the same item in that hand slot
|
||||
// (or if there is no item there we need to keep it free).
|
||||
if (eventArgs.NeedHand && entityManager.TryGetComponent(eventArgs.User, out SharedHandsComponent? handsComponent))
|
||||
{
|
||||
ActiveHand = handsComponent.ActiveHand?.Name;
|
||||
ActiveItem = handsComponent.ActiveHandEntity;
|
||||
}
|
||||
|
||||
Tcs = new TaskCompletionSource<DoAfterStatus>();
|
||||
AsTask = Tcs.Task;
|
||||
}
|
||||
}
|
||||
89
Content.Shared/DoAfter/DoAfterComponent.cs
Normal file
89
Content.Shared/DoAfter/DoAfterComponent.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.DoAfter;
|
||||
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class DoAfterComponent : Component
|
||||
{
|
||||
[DataField("doAfters")]
|
||||
public readonly Dictionary<byte, DoAfter> DoAfters = new();
|
||||
|
||||
[DataField("cancelledDoAfters")]
|
||||
public readonly Dictionary<byte, DoAfter> CancelledDoAfters = new();
|
||||
|
||||
// So the client knows which one to update (and so we don't send all of the do_afters every time 1 updates)
|
||||
// we'll just send them the index. Doesn't matter if it wraps around.
|
||||
[DataField("runningIndex")]
|
||||
public byte RunningIndex;
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class DoAfterComponentState : ComponentState
|
||||
{
|
||||
public Dictionary<byte, DoAfter> DoAfters;
|
||||
|
||||
public DoAfterComponentState(Dictionary<byte, DoAfter> doAfters)
|
||||
{
|
||||
DoAfters = doAfters;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use this event to raise your DoAfter events now.
|
||||
/// Check for cancelled, and if it is, then null the token there.
|
||||
/// </summary>
|
||||
/// TODO: Add a networked DoAfterEvent to pass in AdditionalData for the future
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class DoAfterEvent : HandledEntityEventArgs
|
||||
{
|
||||
public bool Cancelled;
|
||||
public readonly DoAfterEventArgs Args;
|
||||
|
||||
public DoAfterEvent(bool cancelled, DoAfterEventArgs args)
|
||||
{
|
||||
Cancelled = cancelled;
|
||||
Args = args;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use this event to raise your DoAfter events now.
|
||||
/// Check for cancelled, and if it is, then null the token there.
|
||||
/// Can't be serialized
|
||||
/// </summary>
|
||||
/// TODO: Net/Serilization isn't supported so this needs to be networked somehow
|
||||
public sealed class DoAfterEvent<T> : HandledEntityEventArgs
|
||||
{
|
||||
public T AdditionalData;
|
||||
public bool Cancelled;
|
||||
public readonly DoAfterEventArgs Args;
|
||||
|
||||
public DoAfterEvent(T additionalData, bool cancelled, DoAfterEventArgs args)
|
||||
{
|
||||
AdditionalData = additionalData;
|
||||
Cancelled = cancelled;
|
||||
Args = args;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class CancelledDoAfterMessage : EntityEventArgs
|
||||
{
|
||||
public EntityUid Uid;
|
||||
public byte ID;
|
||||
|
||||
public CancelledDoAfterMessage(EntityUid uid, byte id)
|
||||
{
|
||||
Uid = uid;
|
||||
ID = id;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum DoAfterStatus : byte
|
||||
{
|
||||
Running,
|
||||
Cancelled,
|
||||
Finished,
|
||||
}
|
||||
121
Content.Shared/DoAfter/DoAfterEventArgs.cs
Normal file
121
Content.Shared/DoAfter/DoAfterEventArgs.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System.Threading;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.DoAfter;
|
||||
//TODO: Merge into DoAfter
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class DoAfterEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// The entity invoking do_after
|
||||
/// </summary>
|
||||
public EntityUid User;
|
||||
|
||||
/// <summary>
|
||||
/// How long does the do_after require to complete
|
||||
/// </summary>
|
||||
public float Delay;
|
||||
|
||||
/// <summary>
|
||||
/// Applicable target (if relevant)
|
||||
/// </summary>
|
||||
public EntityUid? Target;
|
||||
|
||||
/// <summary>
|
||||
/// Entity used by the User on the Target.
|
||||
/// </summary>
|
||||
public EntityUid? Used;
|
||||
|
||||
public bool RaiseOnUser = true;
|
||||
|
||||
public bool RaiseOnTarget = true;
|
||||
|
||||
public bool RaiseOnUsed = true;
|
||||
|
||||
/// <summary>
|
||||
/// Manually cancel the do_after so it no longer runs
|
||||
/// </summary>
|
||||
[NonSerialized]
|
||||
public CancellationToken CancelToken;
|
||||
|
||||
// Break the chains
|
||||
/// <summary>
|
||||
/// Whether we need to keep our active hand as is (i.e. can't change hand or change item).
|
||||
/// This also covers requiring the hand to be free (if applicable).
|
||||
/// </summary>
|
||||
public bool NeedHand;
|
||||
|
||||
/// <summary>
|
||||
/// If do_after stops when the user moves
|
||||
/// </summary>
|
||||
public bool BreakOnUserMove;
|
||||
|
||||
/// <summary>
|
||||
/// If do_after stops when the target moves (if there is a target)
|
||||
/// </summary>
|
||||
public bool BreakOnTargetMove;
|
||||
|
||||
/// <summary>
|
||||
/// Threshold for user and target movement
|
||||
/// </summary>
|
||||
public float MovementThreshold;
|
||||
|
||||
public bool BreakOnDamage;
|
||||
|
||||
/// <summary>
|
||||
/// Threshold for user damage
|
||||
/// </summary>
|
||||
public FixedPoint2? DamageThreshold;
|
||||
public bool BreakOnStun;
|
||||
|
||||
/// <summary>
|
||||
/// Should the DoAfter event broadcast?
|
||||
/// </summary>
|
||||
public bool Broadcast;
|
||||
|
||||
/// <summary>
|
||||
/// Threshold for distance user from the used OR target entities.
|
||||
/// </summary>
|
||||
public float? DistanceThreshold;
|
||||
|
||||
/// <summary>
|
||||
/// Requires a function call once at the end (like InRangeUnobstructed).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Anything that needs a pre-check should do it itself so no DoAfterState is ever sent to the client.
|
||||
/// </remarks>
|
||||
[NonSerialized]
|
||||
//TODO: Replace with eventbus
|
||||
public Func<bool>? PostCheck;
|
||||
|
||||
/// <summary>
|
||||
/// Additional conditions that need to be met. Return false to cancel.
|
||||
/// </summary>
|
||||
[NonSerialized]
|
||||
//TODO Replace with eventbus
|
||||
public Func<bool>? ExtraCheck;
|
||||
|
||||
public DoAfterEventArgs(
|
||||
EntityUid user,
|
||||
float delay,
|
||||
CancellationToken cancelToken = default,
|
||||
EntityUid? target = null,
|
||||
EntityUid? used = null)
|
||||
{
|
||||
User = user;
|
||||
Delay = delay;
|
||||
CancelToken = cancelToken;
|
||||
Target = target;
|
||||
Used = used;
|
||||
MovementThreshold = 0.1f;
|
||||
DamageThreshold = 1.0;
|
||||
|
||||
if (Target == null)
|
||||
{
|
||||
DebugTools.Assert(!BreakOnTargetMove);
|
||||
BreakOnTargetMove = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
using Content.Shared.FixedPoint;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.DoAfter
|
||||
{
|
||||
[NetworkedComponent()]
|
||||
public abstract class SharedDoAfterComponent : Component
|
||||
{
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class DoAfterComponentState : ComponentState
|
||||
{
|
||||
public List<ClientDoAfter> DoAfters { get; }
|
||||
|
||||
public DoAfterComponentState(List<ClientDoAfter> doAfters)
|
||||
{
|
||||
DoAfters = doAfters;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class CancelledDoAfterMessage : EntityEventArgs
|
||||
{
|
||||
public EntityUid Uid;
|
||||
public byte ID { get; }
|
||||
|
||||
public CancelledDoAfterMessage(EntityUid uid, byte id)
|
||||
{
|
||||
Uid = uid;
|
||||
ID = id;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Merge this with the actual DoAfter
|
||||
/// <summary>
|
||||
/// We send a trimmed-down version of the DoAfter for the client for it to use.
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class ClientDoAfter
|
||||
{
|
||||
public bool Cancelled = false;
|
||||
|
||||
/// <summary>
|
||||
/// Accrued time when cancelled.
|
||||
/// </summary>
|
||||
public float CancelledAccumulator;
|
||||
|
||||
// To see what these do look at DoAfter and DoAfterEventArgs
|
||||
public byte ID { get; }
|
||||
|
||||
public TimeSpan StartTime { get; }
|
||||
|
||||
public EntityCoordinates UserGrid { get; }
|
||||
|
||||
public EntityCoordinates TargetGrid { get; }
|
||||
|
||||
public EntityUid? Target { get; }
|
||||
|
||||
public float Accumulator;
|
||||
|
||||
public float Delay { get; }
|
||||
|
||||
// TODO: The other ones need predicting
|
||||
public bool BreakOnUserMove { get; }
|
||||
|
||||
public bool BreakOnTargetMove { get; }
|
||||
|
||||
public float MovementThreshold { get; }
|
||||
|
||||
public FixedPoint2 DamageThreshold { get; }
|
||||
|
||||
public ClientDoAfter(byte id, EntityCoordinates userGrid, EntityCoordinates targetGrid, TimeSpan startTime,
|
||||
float delay, bool breakOnUserMove, bool breakOnTargetMove, float movementThreshold, FixedPoint2 damageThreshold, EntityUid? target = null)
|
||||
{
|
||||
ID = id;
|
||||
UserGrid = userGrid;
|
||||
TargetGrid = targetGrid;
|
||||
StartTime = startTime;
|
||||
Delay = delay;
|
||||
BreakOnUserMove = breakOnUserMove;
|
||||
BreakOnTargetMove = breakOnTargetMove;
|
||||
MovementThreshold = movementThreshold;
|
||||
DamageThreshold = damageThreshold;
|
||||
Target = target;
|
||||
}
|
||||
}
|
||||
}
|
||||
385
Content.Shared/DoAfter/SharedDoAfterSystem.cs
Normal file
385
Content.Shared/DoAfter/SharedDoAfterSystem.cs
Normal file
@@ -0,0 +1,385 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Mobs;
|
||||
using Content.Shared.Stunnable;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.DoAfter;
|
||||
|
||||
public abstract class SharedDoAfterSystem : EntitySystem
|
||||
{
|
||||
[Dependency] protected readonly IGameTiming GameTiming = default!;
|
||||
|
||||
// We cache the list as to not allocate every update tick...
|
||||
private readonly Queue<DoAfter> _pending = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<DoAfterComponent, DamageChangedEvent>(OnDamage);
|
||||
SubscribeLocalEvent<DoAfterComponent, MobStateChangedEvent>(OnStateChanged);
|
||||
SubscribeLocalEvent<DoAfterComponent, ComponentGetState>(OnDoAfterGetState);
|
||||
}
|
||||
|
||||
private void Add(EntityUid entity, DoAfterComponent component, DoAfter doAfter)
|
||||
{
|
||||
doAfter.ID = component.RunningIndex;
|
||||
doAfter.Delay = doAfter.EventArgs.Delay;
|
||||
component.DoAfters.Add(component.RunningIndex, doAfter);
|
||||
EnsureComp<ActiveDoAfterComponent>(entity);
|
||||
component.RunningIndex++;
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
private void OnDoAfterGetState(EntityUid uid, DoAfterComponent component, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new DoAfterComponentState(component.DoAfters);
|
||||
}
|
||||
|
||||
private void Cancelled(DoAfterComponent component, DoAfter doAfter)
|
||||
{
|
||||
if (!component.DoAfters.TryGetValue(doAfter.ID, out var index))
|
||||
return;
|
||||
|
||||
component.DoAfters.Remove(doAfter.ID);
|
||||
|
||||
if (component.DoAfters.Count == 0)
|
||||
RemComp<ActiveDoAfterComponent>(component.Owner);
|
||||
|
||||
RaiseNetworkEvent(new CancelledDoAfterMessage(component.Owner, index.ID));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call when the particular DoAfter is finished.
|
||||
/// Client should be tracking this independently.
|
||||
/// </summary>
|
||||
private void Finished(DoAfterComponent component, DoAfter doAfter)
|
||||
{
|
||||
if (!component.DoAfters.ContainsKey(doAfter.ID))
|
||||
return;
|
||||
|
||||
component.DoAfters.Remove(doAfter.ID);
|
||||
|
||||
if (component.DoAfters.Count == 0)
|
||||
RemComp<ActiveDoAfterComponent>(component.Owner);
|
||||
}
|
||||
|
||||
private void OnStateChanged(EntityUid uid, DoAfterComponent component, MobStateChangedEvent args)
|
||||
{
|
||||
if (args.NewMobState != MobState.Dead || args.NewMobState != MobState.Critical)
|
||||
return;
|
||||
|
||||
foreach (var (_, doAfter) in component.DoAfters)
|
||||
{
|
||||
Cancel(uid, doAfter, component);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancels DoAfter if it breaks on damage and it meets the threshold
|
||||
/// </summary>
|
||||
/// <param name="uid">The EntityUID of the user</param>
|
||||
/// <param name="component"></param>
|
||||
/// <param name="args"></param>
|
||||
private void OnDamage(EntityUid uid, DoAfterComponent component, DamageChangedEvent args)
|
||||
{
|
||||
if (!args.InterruptsDoAfters || !args.DamageIncreased || args.DamageDelta == null)
|
||||
return;
|
||||
|
||||
foreach (var doAfter in component.DoAfters.Values)
|
||||
{
|
||||
if (doAfter.EventArgs.BreakOnDamage && args.DamageDelta?.Total.Float() > doAfter.EventArgs.DamageThreshold)
|
||||
Cancel(uid, doAfter, component);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
foreach (var (_, comp) in EntityManager.EntityQuery<ActiveDoAfterComponent, DoAfterComponent>())
|
||||
{
|
||||
foreach (var doAfter in comp.DoAfters.Values.ToArray())
|
||||
{
|
||||
Run(comp.Owner, comp, doAfter);
|
||||
|
||||
switch (doAfter.Status)
|
||||
{
|
||||
case DoAfterStatus.Running:
|
||||
break;
|
||||
case DoAfterStatus.Cancelled:
|
||||
_pending.Enqueue(doAfter);
|
||||
break;
|
||||
case DoAfterStatus.Finished:
|
||||
_pending.Enqueue(doAfter);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
while (_pending.TryDequeue(out var doAfter))
|
||||
{
|
||||
if (doAfter.Status == DoAfterStatus.Cancelled)
|
||||
{
|
||||
Cancelled(comp, doAfter);
|
||||
|
||||
if (doAfter.Done != null)
|
||||
doAfter.Done(true);
|
||||
}
|
||||
|
||||
if (doAfter.Status == DoAfterStatus.Finished)
|
||||
{
|
||||
Finished(comp, doAfter);
|
||||
|
||||
if (doAfter.Done != null)
|
||||
doAfter.Done(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tasks that are delayed until the specified time has passed
|
||||
/// These can be potentially cancelled by the user moving or when other things happen.
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns></returns>
|
||||
[Obsolete("Use the synchronous version instead, DoAfter")]
|
||||
public async Task<DoAfterStatus> WaitDoAfter(DoAfterEventArgs eventArgs)
|
||||
{
|
||||
var doAfter = CreateDoAfter(eventArgs);
|
||||
|
||||
await doAfter.AsTask;
|
||||
|
||||
return doAfter.Status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a DoAfter without waiting for it to finish. You can use events with this.
|
||||
/// These can be potentially cancelled by the user moving or when other things happen.
|
||||
/// Use this when you need to send extra data with the DoAfter
|
||||
/// </summary>
|
||||
/// <param name="eventArgs">The DoAfterEventArgs</param>
|
||||
/// <param name="data">The extra data sent over </param>
|
||||
public void DoAfter<T>(DoAfterEventArgs eventArgs, T data)
|
||||
{
|
||||
var doAfter = CreateDoAfter(eventArgs);
|
||||
|
||||
doAfter.Done = cancelled => { Send(data, cancelled, eventArgs); };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a DoAfter without waiting for it to finish. You can use events with this.
|
||||
/// These can be potentially cancelled by the user moving or when other things happen.
|
||||
/// Use this if you don't have any extra data to send with the DoAfter
|
||||
/// </summary>
|
||||
/// <param name="eventArgs">The DoAfterEventArgs</param>
|
||||
public DoAfter DoAfter(DoAfterEventArgs eventArgs)
|
||||
{
|
||||
var doAfter = CreateDoAfter(eventArgs);
|
||||
|
||||
doAfter.Done = cancelled => { Send(cancelled, eventArgs); };
|
||||
|
||||
return doAfter;
|
||||
}
|
||||
|
||||
private DoAfter CreateDoAfter(DoAfterEventArgs eventArgs)
|
||||
{
|
||||
// Setup
|
||||
eventArgs.CancelToken = new CancellationToken();
|
||||
var doAfter = new DoAfter(eventArgs, EntityManager);
|
||||
// Caller's gonna be responsible for this I guess
|
||||
var doAfterComponent = Comp<DoAfterComponent>(eventArgs.User);
|
||||
doAfter.ID = doAfterComponent.RunningIndex;
|
||||
doAfter.StartTime = GameTiming.CurTime;
|
||||
Add(eventArgs.User, doAfterComponent, doAfter);
|
||||
return doAfter;
|
||||
}
|
||||
|
||||
private void Run(EntityUid entity, DoAfterComponent comp, DoAfter doAfter)
|
||||
{
|
||||
switch (doAfter.Status)
|
||||
{
|
||||
case DoAfterStatus.Running:
|
||||
break;
|
||||
case DoAfterStatus.Cancelled:
|
||||
case DoAfterStatus.Finished:
|
||||
return;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
doAfter.Elapsed = GameTiming.CurTime - doAfter.StartTime;
|
||||
|
||||
if (IsFinished(doAfter))
|
||||
{
|
||||
if (!TryPostCheck(doAfter))
|
||||
{
|
||||
Cancel(entity, doAfter, comp);
|
||||
}
|
||||
else
|
||||
{
|
||||
doAfter.Tcs.SetResult(DoAfterStatus.Finished);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsCancelled(doAfter))
|
||||
{
|
||||
Cancel(entity, doAfter, comp);
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryPostCheck(DoAfter doAfter)
|
||||
{
|
||||
return doAfter.EventArgs.PostCheck?.Invoke() != false;
|
||||
}
|
||||
|
||||
private bool IsFinished(DoAfter doAfter)
|
||||
{
|
||||
var delay = TimeSpan.FromSeconds(doAfter.EventArgs.Delay);
|
||||
|
||||
if (doAfter.Elapsed <= delay)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool IsCancelled(DoAfter doAfter)
|
||||
{
|
||||
var eventArgs = doAfter.EventArgs;
|
||||
var xForm = GetEntityQuery<TransformComponent>();
|
||||
|
||||
if (!Exists(eventArgs.User) || eventArgs.Target is { } target && !Exists(target))
|
||||
return true;
|
||||
|
||||
if (eventArgs.CancelToken.IsCancellationRequested)
|
||||
return true;
|
||||
|
||||
//TODO: Handle Inertia in space
|
||||
if (eventArgs.BreakOnUserMove && !xForm.GetComponent(eventArgs.User).Coordinates
|
||||
.InRange(EntityManager, doAfter.UserGrid, eventArgs.MovementThreshold))
|
||||
return true;
|
||||
|
||||
if (eventArgs.Target != null && eventArgs.BreakOnTargetMove && !xForm.GetComponent(eventArgs.Target!.Value)
|
||||
.Coordinates.InRange(EntityManager, doAfter.TargetGrid, eventArgs.MovementThreshold))
|
||||
return true;
|
||||
|
||||
if (eventArgs.ExtraCheck != null && !eventArgs.ExtraCheck.Invoke())
|
||||
return true;
|
||||
|
||||
if (eventArgs.BreakOnStun && HasComp<StunnedComponent>(eventArgs.User))
|
||||
return true;
|
||||
|
||||
if (eventArgs.NeedHand)
|
||||
{
|
||||
if (!TryComp<SharedHandsComponent>(eventArgs.User, out var handsComp))
|
||||
{
|
||||
//TODO: Figure out active hand and item values
|
||||
|
||||
// If we had a hand but no longer have it that's still a paddlin'
|
||||
if (doAfter.ActiveHand != null)
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var currentActiveHand = handsComp.ActiveHand?.Name;
|
||||
if (doAfter.ActiveHand != currentActiveHand)
|
||||
return true;
|
||||
|
||||
var currentItem = handsComp.ActiveHandEntity;
|
||||
if (doAfter.ActiveItem != currentItem)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (eventArgs.DistanceThreshold != null)
|
||||
{
|
||||
var userXform = xForm.GetComponent(eventArgs.User);
|
||||
|
||||
if (eventArgs.Target != null && !eventArgs.User.Equals(eventArgs.Target))
|
||||
{
|
||||
//recalculate Target location in case Target has also moved
|
||||
var targetCoords = xForm.GetComponent(eventArgs.Target.Value).Coordinates;
|
||||
if (!userXform.Coordinates.InRange(EntityManager, targetCoords, eventArgs.DistanceThreshold.Value))
|
||||
return true;
|
||||
}
|
||||
|
||||
if (eventArgs.Used != null)
|
||||
{
|
||||
var usedCoords = xForm.GetComponent(eventArgs.Used.Value).Coordinates;
|
||||
if (!userXform.Coordinates.InRange(EntityManager, usedCoords, eventArgs.DistanceThreshold.Value))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Cancel(EntityUid entity, DoAfter doAfter, DoAfterComponent? comp = null)
|
||||
{
|
||||
if (!Resolve(entity, ref comp, false))
|
||||
return;
|
||||
|
||||
if (comp.CancelledDoAfters.ContainsKey(doAfter.ID))
|
||||
return;
|
||||
|
||||
if (!comp.DoAfters.ContainsKey(doAfter.ID))
|
||||
return;
|
||||
|
||||
doAfter.Cancelled = true;
|
||||
doAfter.CancelledTime = GameTiming.CurTime;
|
||||
|
||||
var doAfterMessage = comp.DoAfters[doAfter.ID];
|
||||
comp.CancelledDoAfters.Add(doAfter.ID, doAfterMessage);
|
||||
|
||||
if (doAfter.Status == DoAfterStatus.Running)
|
||||
{
|
||||
doAfter.Tcs.SetResult(DoAfterStatus.Cancelled);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send the DoAfter event, used where you don't need any extra data to send.
|
||||
/// </summary>
|
||||
/// <param name="cancelled"></param>
|
||||
/// <param name="args"></param>
|
||||
private void Send(bool cancelled, DoAfterEventArgs args)
|
||||
{
|
||||
var ev = new DoAfterEvent(cancelled, args);
|
||||
|
||||
RaiseDoAfterEvent(ev, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send the DoAfter event, used where you need extra data to send
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="cancelled"></param>
|
||||
/// <param name="args"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
private void Send<T>(T data, bool cancelled, DoAfterEventArgs args)
|
||||
{
|
||||
var ev = new DoAfterEvent<T>(data, cancelled, args);
|
||||
|
||||
RaiseDoAfterEvent(ev, args);
|
||||
}
|
||||
|
||||
private void RaiseDoAfterEvent<TEvent>(TEvent ev, DoAfterEventArgs args) where TEvent : notnull
|
||||
{
|
||||
if (EntityManager.EntityExists(args.User) && args.RaiseOnUser)
|
||||
RaiseLocalEvent(args.User, ev, args.Broadcast);
|
||||
|
||||
if (args.Target is { } target && EntityManager.EntityExists(target) && args.RaiseOnTarget)
|
||||
RaiseLocalEvent(target, ev, args.Broadcast);
|
||||
|
||||
if (args.Used is { } used && EntityManager.EntityExists(used) && args.RaiseOnUsed)
|
||||
RaiseLocalEvent(used, ev, args.Broadcast);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user