ECS usedelay (#6348)
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Administration.Logs;
|
||||
using Content.Shared.CombatMode;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Input;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Physics;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Throwing;
|
||||
@@ -625,7 +622,7 @@ namespace Content.Shared.Interaction
|
||||
RaiseLocalEvent(used, activateMsg);
|
||||
if (activateMsg.Handled)
|
||||
{
|
||||
delayComponent?.BeginDelay();
|
||||
BeginDelay(delayComponent);
|
||||
_adminLogSystem.Add(LogType.InteractActivate, LogImpact.Low, $"{ToPrettyString(user):user} activated {ToPrettyString(used):used}");
|
||||
return;
|
||||
}
|
||||
@@ -635,7 +632,7 @@ namespace Content.Shared.Interaction
|
||||
|
||||
var activateEventArgs = new ActivateEventArgs(user, used);
|
||||
activateComp.Activate(activateEventArgs);
|
||||
delayComponent?.BeginDelay();
|
||||
BeginDelay(delayComponent);
|
||||
_adminLogSystem.Add(LogType.InteractActivate, LogImpact.Low, $"{ToPrettyString(user):user} activated {ToPrettyString(used):used}"); // No way to check success.
|
||||
}
|
||||
#endregion
|
||||
@@ -669,7 +666,7 @@ namespace Content.Shared.Interaction
|
||||
RaiseLocalEvent(used, useMsg);
|
||||
if (useMsg.Handled)
|
||||
{
|
||||
delayComponent?.BeginDelay();
|
||||
BeginDelay(delayComponent);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -681,7 +678,7 @@ namespace Content.Shared.Interaction
|
||||
// If a Use returns a status completion we finish our interaction
|
||||
if (use.UseEntity(new UseEntityEventArgs(user)))
|
||||
{
|
||||
delayComponent?.BeginDelay();
|
||||
BeginDelay(delayComponent);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -689,6 +686,12 @@ namespace Content.Shared.Interaction
|
||||
return false;
|
||||
}
|
||||
|
||||
protected virtual void BeginDelay(UseDelayComponent? component = null)
|
||||
{
|
||||
// This is temporary until we have predicted UseDelay.
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Alternative interactions on an entity.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Content.Shared.Cooldown;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.Timing
|
||||
@@ -12,61 +9,21 @@ namespace Content.Shared.Timing
|
||||
/// <summary>
|
||||
/// Timer that creates a cooldown each time an object is activated/used
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class UseDelayComponent : Component
|
||||
[RegisterComponent, ComponentProtoName("UseDelay")]
|
||||
public sealed class UseDelayComponent : Component
|
||||
{
|
||||
public override string Name => "UseDelay";
|
||||
|
||||
private TimeSpan _lastUseTime;
|
||||
[ViewVariables]
|
||||
public TimeSpan LastUseTime;
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("delay")]
|
||||
private float _delay = 1;
|
||||
/// <summary>
|
||||
/// The time, in seconds, between an object's use and when it can be used again
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public float Delay { get => _delay; set => _delay = value; }
|
||||
public float Delay = 1;
|
||||
|
||||
public bool ActiveDelay{ get; private set; }
|
||||
[ViewVariables]
|
||||
public float Elapsed = 0f;
|
||||
|
||||
private CancellationTokenSource? cancellationTokenSource;
|
||||
public CancellationTokenSource? CancellationTokenSource;
|
||||
|
||||
public void BeginDelay()
|
||||
{
|
||||
if (ActiveDelay)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ActiveDelay = true;
|
||||
|
||||
cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
Owner.SpawnTimer(TimeSpan.FromSeconds(Delay), () => ActiveDelay = false, cancellationTokenSource.Token);
|
||||
|
||||
_lastUseTime = IoCManager.Resolve<IGameTiming>().CurTime;
|
||||
|
||||
var cooldown = IoCManager.Resolve<IEntityManager>().EnsureComponent<ItemCooldownComponent>(Owner);
|
||||
cooldown.CooldownStart = _lastUseTime;
|
||||
cooldown.CooldownEnd = _lastUseTime + TimeSpan.FromSeconds(Delay);
|
||||
}
|
||||
|
||||
public void Cancel()
|
||||
{
|
||||
cancellationTokenSource?.Cancel();
|
||||
ActiveDelay = false;
|
||||
|
||||
if (IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out ItemCooldownComponent? cooldown))
|
||||
{
|
||||
cooldown.CooldownEnd = IoCManager.Resolve<IGameTiming>().CurTime;
|
||||
}
|
||||
}
|
||||
|
||||
public void Restart()
|
||||
{
|
||||
cancellationTokenSource?.Cancel();
|
||||
ActiveDelay = false;
|
||||
BeginDelay();
|
||||
}
|
||||
public bool ActiveDelay => CancellationTokenSource is { Token: { IsCancellationRequested: false } };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user