DoAfter Refactor (#13225)
Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System.Threading;
|
||||
using Content.Shared.Construction.EntitySystems;
|
||||
using Content.Shared.Tools;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
@@ -21,8 +20,6 @@ namespace Content.Shared.Construction.Components
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("delay")]
|
||||
public float Delay = 1f;
|
||||
|
||||
public CancellationTokenSource? CancelToken = null;
|
||||
}
|
||||
|
||||
public abstract class BaseAnchoredAttemptEvent : CancellableEntityEventArgs
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,6 @@ public sealed class EnsnaringComponent : Component
|
||||
[DataField("canMoveBreakout")]
|
||||
public bool CanMoveBreakout;
|
||||
|
||||
public CancellationTokenSource? CancelToken;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -20,8 +20,6 @@ public sealed class MechEquipmentComponent : Component
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public EntityUid? EquipmentOwner;
|
||||
|
||||
public CancellationTokenSource? TokenSource = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Threading;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.DragDrop;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameStates;
|
||||
@@ -79,8 +78,6 @@ public abstract class SharedCryoPodComponent: Component
|
||||
|
||||
public bool IsPrying { get; set; }
|
||||
|
||||
public CancellationTokenSource? DragDropCancelToken;
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum CryoPodVisuals : byte
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Server.Medical.Components;
|
||||
using Content.Server.Medical.Components;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.DragDrop;
|
||||
using Content.Shared.Emag.Systems;
|
||||
@@ -143,17 +144,6 @@ public abstract partial class SharedCryoPodSystem: EntitySystem
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
protected void DoInsertCryoPod(EntityUid uid, SharedCryoPodComponent cryoPodComponent, DoInsertCryoPodEvent args)
|
||||
{
|
||||
cryoPodComponent.DragDropCancelToken = null;
|
||||
InsertBody(uid, args.ToInsert, cryoPodComponent);
|
||||
}
|
||||
|
||||
protected void DoInsertCancelCryoPod(EntityUid uid, SharedCryoPodComponent cryoPodComponent, DoInsertCancelledCryoPodEvent args)
|
||||
{
|
||||
cryoPodComponent.DragDropCancelToken = null;
|
||||
}
|
||||
|
||||
protected void OnCryoPodPryFinished(EntityUid uid, SharedCryoPodComponent cryoPodComponent, CryoPodPryFinished args)
|
||||
{
|
||||
cryoPodComponent.IsPrying = false;
|
||||
@@ -167,8 +157,6 @@ public abstract partial class SharedCryoPodSystem: EntitySystem
|
||||
|
||||
#region Event records
|
||||
|
||||
protected record DoInsertCryoPodEvent(EntityUid ToInsert);
|
||||
protected record DoInsertCancelledCryoPodEvent;
|
||||
protected record CryoPodPryFinished;
|
||||
protected record CryoPodPryInterrupted;
|
||||
|
||||
|
||||
@@ -109,7 +109,9 @@ public sealed class EncryptionKeySystem : EntitySystem
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_toolSystem.UseTool(args.Used, args.User, uid, 0f, 0f, component.KeysExtractionMethod, toolComponent: tool))
|
||||
var toolEvData = new ToolEventData(null);
|
||||
|
||||
if(!_toolSystem.UseTool(args.Used, args.User, uid, 0f, new[] { component.KeysExtractionMethod }, toolEvData, toolComponent: tool))
|
||||
return;
|
||||
|
||||
var contained = component.KeyContainer.ContainedEntities.ToArray();
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Threading;
|
||||
using Content.Shared.Disease;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Store;
|
||||
@@ -67,9 +66,6 @@ public sealed class RevenantComponent : Component
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite), DataField("maxEssenceUpgradeAmount")]
|
||||
public float MaxEssenceUpgradeAmount = 10;
|
||||
|
||||
public CancellationTokenSource? SoulSearchCancelToken;
|
||||
public CancellationTokenSource? HarvestCancelToken;
|
||||
#endregion
|
||||
|
||||
//In the nearby radius, causes various objects to be thrown, messed with, and containers opened
|
||||
|
||||
@@ -16,8 +16,9 @@ namespace Content.Shared.Storage.Components
|
||||
public TimeSpan DelayPerItem = TimeSpan.FromSeconds(0.2);
|
||||
|
||||
/// <summary>
|
||||
/// Cancellation token for the doafter.
|
||||
/// The multiplier modifier
|
||||
/// </summary>
|
||||
public CancellationTokenSource? CancelToken;
|
||||
[DataField("multiplier")]
|
||||
public float Multiplier = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Threading;
|
||||
using Content.Shared.Audio;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
@@ -41,15 +39,4 @@ public sealed class HandTeleporterComponent : Component
|
||||
[DataField("portalCreationDelay")]
|
||||
public float PortalCreationDelay = 2.5f;
|
||||
|
||||
public CancellationTokenSource? CancelToken = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised on doafter success for creating a portal.
|
||||
/// </summary>
|
||||
public record HandTeleporterSuccessEvent(EntityUid User);
|
||||
|
||||
/// <summary>
|
||||
/// Raised on doafter cancel for creating a portal.
|
||||
/// </summary>
|
||||
public record HandTeleporterCancelledEvent;
|
||||
|
||||
@@ -20,4 +20,68 @@ namespace Content.Shared.Tools.Components
|
||||
[DataField("useSound")]
|
||||
public SoundSpecifier? UseSound { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt event called *before* any do afters to see if the tool usage should succeed or not.
|
||||
/// You can change the fuel consumption by changing the Fuel property.
|
||||
/// </summary>
|
||||
public sealed class ToolUseAttemptEvent : CancellableEntityEventArgs
|
||||
{
|
||||
public float Fuel { get; set; }
|
||||
public EntityUid User { get; }
|
||||
|
||||
public ToolUseAttemptEvent(float fuel, EntityUid user)
|
||||
{
|
||||
Fuel = fuel;
|
||||
User = user;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on the user of a tool to see if they can actually use it.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public struct ToolUserAttemptUseEvent
|
||||
{
|
||||
public EntityUid User;
|
||||
public EntityUid? Target;
|
||||
public bool Cancelled = false;
|
||||
|
||||
public ToolUserAttemptUseEvent(EntityUid user, EntityUid? target)
|
||||
{
|
||||
User = user;
|
||||
Target = target;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt event called *after* any do afters to see if the tool usage should succeed or not.
|
||||
/// You can use this event to consume any fuel needed.
|
||||
/// </summary>
|
||||
public sealed class ToolUseFinishAttemptEvent : CancellableEntityEventArgs
|
||||
{
|
||||
public float Fuel { get; }
|
||||
public EntityUid User { get; }
|
||||
|
||||
public ToolUseFinishAttemptEvent(float fuel, EntityUid user)
|
||||
{
|
||||
Fuel = fuel;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ToolEventData
|
||||
{
|
||||
public readonly Object? Ev;
|
||||
public readonly Object? CancelledEv;
|
||||
public readonly float Fuel;
|
||||
public readonly EntityUid? TargetEntity;
|
||||
|
||||
public ToolEventData(Object? ev, float fuel = 0f, Object? cancelledEv = null, EntityUid? targetEntity = null)
|
||||
{
|
||||
Ev = ev;
|
||||
CancelledEv = cancelledEv;
|
||||
Fuel = fuel;
|
||||
TargetEntity = targetEntity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Tools;
|
||||
@@ -11,6 +14,7 @@ public abstract class SharedToolSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
|
||||
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -18,32 +22,77 @@ public abstract class SharedToolSystem : EntitySystem
|
||||
SubscribeLocalEvent<MultipleToolComponent, ActivateInWorldEvent>(OnMultipleToolActivated);
|
||||
SubscribeLocalEvent<MultipleToolComponent, ComponentGetState>(OnMultipleToolGetState);
|
||||
SubscribeLocalEvent<MultipleToolComponent, ComponentHandleState>(OnMultipleToolHandleState);
|
||||
|
||||
SubscribeLocalEvent<ToolComponent, DoAfterEvent<ToolEventData>>(OnDoAfter);
|
||||
|
||||
SubscribeLocalEvent<ToolDoAfterComplete>(OnDoAfterComplete);
|
||||
SubscribeLocalEvent<ToolDoAfterCancelled>(OnDoAfterCancelled);
|
||||
}
|
||||
|
||||
public bool UseTool(EntityUid tool, EntityUid user, EntityUid? target, float fuel,
|
||||
float doAfterDelay, string toolQualityNeeded, object? doAfterCompleteEvent = null, object? doAfterCancelledEvent = null, EntityUid? doAfterEventTarget = null,
|
||||
Func<bool>? doAfterCheck = null, ToolComponent? toolComponent = null)
|
||||
private void OnDoAfter(EntityUid uid, ToolComponent component, DoAfterEvent<ToolEventData> args)
|
||||
{
|
||||
return UseTool(tool, user, target, fuel, doAfterDelay, new[] { toolQualityNeeded },
|
||||
doAfterCompleteEvent, doAfterCancelledEvent, doAfterEventTarget, doAfterCheck, toolComponent);
|
||||
if (args.Handled || args.Cancelled || args.AdditionalData.Ev == null)
|
||||
return;
|
||||
|
||||
if (ToolFinishUse(uid, args.Args.User, args.AdditionalData.Fuel))
|
||||
{
|
||||
if (args.AdditionalData.TargetEntity != null)
|
||||
RaiseLocalEvent(args.AdditionalData.TargetEntity.Value, args.AdditionalData.Ev);
|
||||
else
|
||||
RaiseLocalEvent(args.AdditionalData.Ev);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
else if (args.AdditionalData.CancelledEv != null)
|
||||
{
|
||||
if (args.AdditionalData.TargetEntity != null)
|
||||
RaiseLocalEvent(args.AdditionalData.TargetEntity.Value, args.AdditionalData.CancelledEv);
|
||||
else
|
||||
RaiseLocalEvent(args.AdditionalData.CancelledEv);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool UseTool(
|
||||
EntityUid tool,
|
||||
EntityUid user,
|
||||
EntityUid? target,
|
||||
float fuel,
|
||||
float doAfterDelay,
|
||||
IEnumerable<string> toolQualitiesNeeded,
|
||||
object? doAfterCompleteEvent = null,
|
||||
object? doAfterCancelledEvent = null,
|
||||
EntityUid? doAfterEventTarget = null,
|
||||
Func<bool>? doAfterCheck = null,
|
||||
ToolComponent? toolComponent = null,
|
||||
CancellationToken? cancelToken = null)
|
||||
public bool UseTool(EntityUid tool, EntityUid user, EntityUid? target, float doAfterDelay, IEnumerable<string> toolQualitiesNeeded, ToolEventData toolEventData, float fuel = 0f, ToolComponent? toolComponent = null, Func<bool>? doAfterCheck = null)
|
||||
{
|
||||
// predicted tools when.
|
||||
return false;
|
||||
// No logging here, after all that'd mean the caller would need to check if the component is there or not.
|
||||
if (!Resolve(tool, ref toolComponent, false))
|
||||
return false;
|
||||
|
||||
var ev = new ToolUserAttemptUseEvent(user, target);
|
||||
RaiseLocalEvent(user, ref ev);
|
||||
if (ev.Cancelled)
|
||||
return false;
|
||||
|
||||
if (!ToolStartUse(tool, user, fuel, toolQualitiesNeeded, toolComponent))
|
||||
return false;
|
||||
|
||||
if (doAfterDelay > 0f)
|
||||
{
|
||||
var doAfterArgs = new DoAfterEventArgs(user, doAfterDelay / toolComponent.SpeedModifier, target:target, used:tool)
|
||||
{
|
||||
ExtraCheck = doAfterCheck,
|
||||
BreakOnDamage = true,
|
||||
BreakOnStun = true,
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
NeedHand = true
|
||||
};
|
||||
|
||||
_doAfterSystem.DoAfter(doAfterArgs, toolEventData);
|
||||
return true;
|
||||
}
|
||||
|
||||
return ToolFinishUse(tool, user, fuel, toolComponent);
|
||||
}
|
||||
|
||||
public bool UseTool(EntityUid tool, EntityUid user, EntityUid? target, float doAfterDelay, string toolQualityNeeded,
|
||||
ToolEventData toolEventData, float fuel = 0, ToolComponent? toolComponent = null,
|
||||
Func<bool>? doAfterCheck = null)
|
||||
{
|
||||
return UseTool(tool, user, target, doAfterDelay, new[] { toolQualityNeeded }, toolEventData, fuel,
|
||||
toolComponent, doAfterCheck);
|
||||
}
|
||||
|
||||
private void OnMultipleToolHandleState(EntityUid uid, MultipleToolComponent component, ref ComponentHandleState args)
|
||||
@@ -116,5 +165,125 @@ public abstract class SharedToolSystem : EntitySystem
|
||||
if (_protoMan.TryIndex(current.Behavior.First(), out ToolQualityPrototype? quality))
|
||||
multiple.CurrentQualityName = Loc.GetString(quality.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a tool entity has the specified quality or not.
|
||||
/// </summary>
|
||||
public bool HasQuality(EntityUid uid, string quality, ToolComponent? tool = null)
|
||||
{
|
||||
return Resolve(uid, ref tool, false) && tool.Qualities.Contains(quality);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a tool entity has all specified qualities or not.
|
||||
/// </summary>
|
||||
public bool HasAllQualities(EntityUid uid, IEnumerable<string> qualities, ToolComponent? tool = null)
|
||||
{
|
||||
return Resolve(uid, ref tool, false) && tool.Qualities.ContainsAll(qualities);
|
||||
}
|
||||
|
||||
|
||||
private bool ToolStartUse(EntityUid tool, EntityUid user, float fuel, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
|
||||
{
|
||||
if (!Resolve(tool, ref toolComponent))
|
||||
return false;
|
||||
|
||||
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded))
|
||||
return false;
|
||||
|
||||
var beforeAttempt = new ToolUseAttemptEvent(fuel, user);
|
||||
RaiseLocalEvent(tool, beforeAttempt, false);
|
||||
|
||||
return !beforeAttempt.Cancelled;
|
||||
}
|
||||
|
||||
private bool ToolFinishUse(EntityUid tool, EntityUid user, float fuel, ToolComponent? toolComponent = null)
|
||||
{
|
||||
if (!Resolve(tool, ref toolComponent))
|
||||
return false;
|
||||
|
||||
var afterAttempt = new ToolUseFinishAttemptEvent(fuel, user);
|
||||
RaiseLocalEvent(tool, afterAttempt, false);
|
||||
|
||||
if (afterAttempt.Cancelled)
|
||||
return false;
|
||||
|
||||
if (toolComponent.UseSound != null)
|
||||
PlayToolSound(tool, toolComponent);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void PlayToolSound(EntityUid uid, ToolComponent? tool = null)
|
||||
{
|
||||
if (!Resolve(uid, ref tool))
|
||||
return;
|
||||
|
||||
if (tool.UseSound is not {} sound)
|
||||
return;
|
||||
|
||||
// Pass tool.Owner to Filter.Pvs to avoid a TryGetEntity call.
|
||||
SoundSystem.Play(sound.GetSound(), Filter.Pvs(tool.Owner),
|
||||
uid, AudioHelpers.WithVariation(0.175f).WithVolume(-5f));
|
||||
}
|
||||
|
||||
private void OnDoAfterComplete(ToolDoAfterComplete ev)
|
||||
{
|
||||
// Actually finish the tool use! Depending on whether that succeeds or not, either event will be broadcast.
|
||||
if(ToolFinishUse(ev.Uid, ev.UserUid, ev.Fuel))
|
||||
{
|
||||
if (ev.EventTarget != null)
|
||||
RaiseLocalEvent(ev.EventTarget.Value, ev.CompletedEvent, false);
|
||||
else
|
||||
RaiseLocalEvent(ev.CompletedEvent);
|
||||
}
|
||||
else if(ev.CancelledEvent != null)
|
||||
{
|
||||
if (ev.EventTarget != null)
|
||||
RaiseLocalEvent(ev.EventTarget.Value, ev.CancelledEvent, false);
|
||||
else
|
||||
RaiseLocalEvent(ev.CancelledEvent);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDoAfterCancelled(ToolDoAfterCancelled ev)
|
||||
{
|
||||
if (ev.EventTarget != null)
|
||||
RaiseLocalEvent(ev.EventTarget.Value, ev.Event, false);
|
||||
else
|
||||
RaiseLocalEvent(ev.Event);
|
||||
}
|
||||
|
||||
private sealed class ToolDoAfterComplete : EntityEventArgs
|
||||
{
|
||||
public readonly object CompletedEvent;
|
||||
public readonly object? CancelledEvent;
|
||||
public readonly EntityUid Uid;
|
||||
public readonly EntityUid UserUid;
|
||||
public readonly float Fuel;
|
||||
public readonly EntityUid? EventTarget;
|
||||
|
||||
public ToolDoAfterComplete(object completedEvent, object? cancelledEvent, EntityUid uid, EntityUid userUid, float fuel, EntityUid? eventTarget = null)
|
||||
{
|
||||
CompletedEvent = completedEvent;
|
||||
Uid = uid;
|
||||
UserUid = userUid;
|
||||
Fuel = fuel;
|
||||
CancelledEvent = cancelledEvent;
|
||||
EventTarget = eventTarget;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ToolDoAfterCancelled : EntityEventArgs
|
||||
{
|
||||
public readonly object Event;
|
||||
public readonly EntityUid? EventTarget;
|
||||
|
||||
public ToolDoAfterCancelled(object @event, EntityUid? eventTarget = null)
|
||||
{
|
||||
Event = @event;
|
||||
EventTarget = eventTarget;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user