ECS usedelay (#6348)
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Content.Server.Administration.Logs;
|
using Content.Server.Administration.Logs;
|
||||||
@@ -7,6 +6,7 @@ using Content.Server.CombatMode;
|
|||||||
using Content.Server.Hands.Components;
|
using Content.Server.Hands.Components;
|
||||||
using Content.Server.Pulling;
|
using Content.Server.Pulling;
|
||||||
using Content.Server.Storage.Components;
|
using Content.Server.Storage.Components;
|
||||||
|
using Content.Server.Timing;
|
||||||
using Content.Shared.ActionBlocker;
|
using Content.Shared.ActionBlocker;
|
||||||
using Content.Shared.Database;
|
using Content.Shared.Database;
|
||||||
using Content.Shared.DragDrop;
|
using Content.Shared.DragDrop;
|
||||||
@@ -14,18 +14,16 @@ using Content.Shared.Input;
|
|||||||
using Content.Shared.Interaction;
|
using Content.Shared.Interaction;
|
||||||
using Content.Shared.Interaction.Helpers;
|
using Content.Shared.Interaction.Helpers;
|
||||||
using Content.Shared.Item;
|
using Content.Shared.Item;
|
||||||
using Content.Shared.Popups;
|
|
||||||
using Content.Shared.Pulling.Components;
|
using Content.Shared.Pulling.Components;
|
||||||
|
using Content.Shared.Timing;
|
||||||
using Content.Shared.Weapons.Melee;
|
using Content.Shared.Weapons.Melee;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using Robust.Server.GameObjects;
|
using Robust.Server.GameObjects;
|
||||||
using Robust.Server.Player;
|
|
||||||
using Robust.Shared.Containers;
|
using Robust.Shared.Containers;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.Input;
|
using Robust.Shared.Input;
|
||||||
using Robust.Shared.Input.Binding;
|
using Robust.Shared.Input.Binding;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.IoC;
|
||||||
using Robust.Shared.Localization;
|
|
||||||
using Robust.Shared.Log;
|
using Robust.Shared.Log;
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
using Robust.Shared.Players;
|
using Robust.Shared.Players;
|
||||||
@@ -41,6 +39,7 @@ namespace Content.Server.Interaction
|
|||||||
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
||||||
[Dependency] private readonly PullingSystem _pullSystem = default!;
|
[Dependency] private readonly PullingSystem _pullSystem = default!;
|
||||||
[Dependency] private readonly AdminLogSystem _adminLogSystem = default!;
|
[Dependency] private readonly AdminLogSystem _adminLogSystem = default!;
|
||||||
|
[Dependency] private readonly UseDelaySystem _useDelay = default!;
|
||||||
|
|
||||||
public override void Initialize()
|
public override void Initialize()
|
||||||
{
|
{
|
||||||
@@ -87,6 +86,11 @@ namespace Content.Server.Interaction
|
|||||||
return storage.SubscribedSessions.Contains(actor.PlayerSession);
|
return storage.SubscribedSessions.Contains(actor.PlayerSession);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void BeginDelay(UseDelayComponent? component = null)
|
||||||
|
{
|
||||||
|
_useDelay.BeginDelay(component);
|
||||||
|
}
|
||||||
|
|
||||||
#region Drag drop
|
#region Drag drop
|
||||||
private void HandleDragDropRequestEvent(DragDropRequestEvent msg, EntitySessionEventArgs args)
|
private void HandleDragDropRequestEvent(DragDropRequestEvent msg, EntitySessionEventArgs args)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(food.TrashPrototype))
|
if (string.IsNullOrEmpty(food.TrashPrototype))
|
||||||
EntityManager.QueueDeleteEntity((food).Owner);
|
EntityManager.QueueDeleteEntity(food.Owner);
|
||||||
else
|
else
|
||||||
DeleteAndSpawnTrash(food, user);
|
DeleteAndSpawnTrash(food, user);
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityManager.QueueDeleteEntity((component).Owner);
|
EntityManager.QueueDeleteEntity(component.Owner);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddEatVerb(EntityUid uid, FoodComponent component, GetInteractionVerbsEvent ev)
|
private void AddEatVerb(EntityUid uid, FoodComponent component, GetInteractionVerbsEvent ev)
|
||||||
|
|||||||
90
Content.Server/Timing/UseDelaySystem.cs
Normal file
90
Content.Server/Timing/UseDelaySystem.cs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using Content.Shared.Cooldown;
|
||||||
|
using Content.Shared.Timing;
|
||||||
|
using Robust.Shared.GameObjects;
|
||||||
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Timing;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
|
namespace Content.Server.Timing;
|
||||||
|
|
||||||
|
public sealed class UseDelaySystem : EntitySystem
|
||||||
|
{
|
||||||
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||||
|
|
||||||
|
private HashSet<UseDelayComponent> _activeDelays = new();
|
||||||
|
|
||||||
|
public override void Update(float frameTime)
|
||||||
|
{
|
||||||
|
base.Update(frameTime);
|
||||||
|
|
||||||
|
var toRemove = new RemQueue<UseDelayComponent>();
|
||||||
|
|
||||||
|
foreach (var delay in _activeDelays)
|
||||||
|
{
|
||||||
|
MetaDataComponent? metaData = null;
|
||||||
|
|
||||||
|
if (Deleted(delay.Owner, metaData) ||
|
||||||
|
delay.CancellationTokenSource?.Token.IsCancellationRequested == true)
|
||||||
|
{
|
||||||
|
toRemove.Add(delay);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Paused(delay.Owner, metaData)) continue;
|
||||||
|
|
||||||
|
delay.Elapsed += frameTime;
|
||||||
|
|
||||||
|
if (delay.Elapsed < delay.Delay) continue;
|
||||||
|
|
||||||
|
toRemove.Add(delay);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var delay in toRemove)
|
||||||
|
{
|
||||||
|
delay.CancellationTokenSource = null;
|
||||||
|
delay.Elapsed = 0f;
|
||||||
|
_activeDelays.Remove(delay);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void BeginDelay(UseDelayComponent? component = null)
|
||||||
|
{
|
||||||
|
if (component == null ||
|
||||||
|
component.ActiveDelay ||
|
||||||
|
Deleted(component.Owner)) return;
|
||||||
|
|
||||||
|
component.CancellationTokenSource = new CancellationTokenSource();
|
||||||
|
|
||||||
|
DebugTools.Assert(!_activeDelays.Contains(component));
|
||||||
|
_activeDelays.Add(component);
|
||||||
|
|
||||||
|
var currentTime = _gameTiming.CurTime;
|
||||||
|
component.LastUseTime = currentTime;
|
||||||
|
|
||||||
|
var cooldown = EnsureComp<ItemCooldownComponent>(component.Owner);
|
||||||
|
cooldown.CooldownStart = currentTime;
|
||||||
|
cooldown.CooldownEnd = currentTime + TimeSpan.FromSeconds(component.Delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Cancel(UseDelayComponent component)
|
||||||
|
{
|
||||||
|
component.CancellationTokenSource?.Cancel();
|
||||||
|
component.CancellationTokenSource = null;
|
||||||
|
|
||||||
|
if (TryComp<ItemCooldownComponent>(component.Owner, out var cooldown))
|
||||||
|
{
|
||||||
|
cooldown.CooldownEnd = _gameTiming.CurTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Restart(UseDelayComponent component)
|
||||||
|
{
|
||||||
|
component.CancellationTokenSource?.Cancel();
|
||||||
|
component.CancellationTokenSource = null;
|
||||||
|
BeginDelay(component);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Content.Shared.ActionBlocker;
|
using Content.Shared.ActionBlocker;
|
||||||
using Content.Shared.Administration.Logs;
|
using Content.Shared.Administration.Logs;
|
||||||
using Content.Shared.CombatMode;
|
using Content.Shared.CombatMode;
|
||||||
using Content.Shared.Database;
|
using Content.Shared.Database;
|
||||||
using Content.Shared.Hands;
|
|
||||||
using Content.Shared.Hands.Components;
|
using Content.Shared.Hands.Components;
|
||||||
using Content.Shared.Input;
|
using Content.Shared.Input;
|
||||||
using Content.Shared.Interaction.Helpers;
|
using Content.Shared.Interaction.Helpers;
|
||||||
using Content.Shared.Inventory;
|
|
||||||
using Content.Shared.Physics;
|
using Content.Shared.Physics;
|
||||||
using Content.Shared.Popups;
|
using Content.Shared.Popups;
|
||||||
using Content.Shared.Throwing;
|
using Content.Shared.Throwing;
|
||||||
@@ -625,7 +622,7 @@ namespace Content.Shared.Interaction
|
|||||||
RaiseLocalEvent(used, activateMsg);
|
RaiseLocalEvent(used, activateMsg);
|
||||||
if (activateMsg.Handled)
|
if (activateMsg.Handled)
|
||||||
{
|
{
|
||||||
delayComponent?.BeginDelay();
|
BeginDelay(delayComponent);
|
||||||
_adminLogSystem.Add(LogType.InteractActivate, LogImpact.Low, $"{ToPrettyString(user):user} activated {ToPrettyString(used):used}");
|
_adminLogSystem.Add(LogType.InteractActivate, LogImpact.Low, $"{ToPrettyString(user):user} activated {ToPrettyString(used):used}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -635,7 +632,7 @@ namespace Content.Shared.Interaction
|
|||||||
|
|
||||||
var activateEventArgs = new ActivateEventArgs(user, used);
|
var activateEventArgs = new ActivateEventArgs(user, used);
|
||||||
activateComp.Activate(activateEventArgs);
|
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.
|
_adminLogSystem.Add(LogType.InteractActivate, LogImpact.Low, $"{ToPrettyString(user):user} activated {ToPrettyString(used):used}"); // No way to check success.
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -669,7 +666,7 @@ namespace Content.Shared.Interaction
|
|||||||
RaiseLocalEvent(used, useMsg);
|
RaiseLocalEvent(used, useMsg);
|
||||||
if (useMsg.Handled)
|
if (useMsg.Handled)
|
||||||
{
|
{
|
||||||
delayComponent?.BeginDelay();
|
BeginDelay(delayComponent);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -681,7 +678,7 @@ namespace Content.Shared.Interaction
|
|||||||
// If a Use returns a status completion we finish our interaction
|
// If a Use returns a status completion we finish our interaction
|
||||||
if (use.UseEntity(new UseEntityEventArgs(user)))
|
if (use.UseEntity(new UseEntityEventArgs(user)))
|
||||||
{
|
{
|
||||||
delayComponent?.BeginDelay();
|
BeginDelay(delayComponent);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -689,6 +686,12 @@ namespace Content.Shared.Interaction
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual void BeginDelay(UseDelayComponent? component = null)
|
||||||
|
{
|
||||||
|
// This is temporary until we have predicted UseDelay.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Alternative interactions on an entity.
|
/// Alternative interactions on an entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Content.Shared.Cooldown;
|
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.IoC;
|
|
||||||
using Robust.Shared.Serialization.Manager.Attributes;
|
using Robust.Shared.Serialization.Manager.Attributes;
|
||||||
using Robust.Shared.Timing;
|
|
||||||
using Robust.Shared.ViewVariables;
|
using Robust.Shared.ViewVariables;
|
||||||
|
|
||||||
namespace Content.Shared.Timing
|
namespace Content.Shared.Timing
|
||||||
@@ -12,61 +9,21 @@ namespace Content.Shared.Timing
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Timer that creates a cooldown each time an object is activated/used
|
/// Timer that creates a cooldown each time an object is activated/used
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[RegisterComponent]
|
[RegisterComponent, ComponentProtoName("UseDelay")]
|
||||||
public class UseDelayComponent : Component
|
public sealed class UseDelayComponent : Component
|
||||||
{
|
{
|
||||||
public override string Name => "UseDelay";
|
[ViewVariables]
|
||||||
|
public TimeSpan LastUseTime;
|
||||||
private TimeSpan _lastUseTime;
|
|
||||||
|
|
||||||
|
[ViewVariables]
|
||||||
[DataField("delay")]
|
[DataField("delay")]
|
||||||
private float _delay = 1;
|
public 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 bool ActiveDelay{ get; private set; }
|
[ViewVariables]
|
||||||
|
public float Elapsed = 0f;
|
||||||
|
|
||||||
private CancellationTokenSource? cancellationTokenSource;
|
public CancellationTokenSource? CancellationTokenSource;
|
||||||
|
|
||||||
public void BeginDelay()
|
public bool ActiveDelay => CancellationTokenSource is { Token: { IsCancellationRequested: false } };
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user