Predict general interactions. (#6856)

This commit is contained in:
Leon Friedrich
2022-03-09 20:12:17 +13:00
committed by GitHub
parent 60e7ef6073
commit 0f435f31c8
10 changed files with 283 additions and 256 deletions

View File

@@ -1,8 +1,6 @@
using System;
using System.Threading;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Timing
{
@@ -10,20 +8,40 @@ namespace Content.Shared.Timing
/// Timer that creates a cooldown each time an object is activated/used
/// </summary>
[RegisterComponent]
[NetworkedComponent]
public sealed class UseDelayComponent : Component
{
[ViewVariables]
public TimeSpan LastUseTime;
[ViewVariables]
[DataField("delay")]
public float Delay = 1;
public TimeSpan? DelayEndTime;
[ViewVariables]
public float Elapsed = 0f;
[DataField("delay")]
[ViewVariables(VVAccess.ReadWrite)]
public TimeSpan Delay = TimeSpan.FromSeconds(1);
/// <summary>
/// Stores remaining delay pausing (and eventually, serialization).
/// </summary>
[DataField("remainingDelay")]
public TimeSpan? RemainingDelay;
public CancellationTokenSource? CancellationTokenSource;
public bool ActiveDelay => CancellationTokenSource is { Token: { IsCancellationRequested: false } };
}
[Serializable, NetSerializable]
public sealed class UseDelayComponentState : ComponentState
{
public readonly TimeSpan LastUseTime;
public readonly TimeSpan Delay;
public readonly TimeSpan? DelayEndTime;
public UseDelayComponentState(TimeSpan lastUseTime, TimeSpan delay, TimeSpan? delayEndTime)
{
LastUseTime = lastUseTime;
Delay = delay;
DelayEndTime = delayEndTime;
}
}
}

View File

@@ -0,0 +1,138 @@
using System.Threading;
using Content.Shared.Cooldown;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Shared.Timing;
public sealed class UseDelaySystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
private HashSet<UseDelayComponent> _activeDelays = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<UseDelayComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<UseDelayComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<UseDelayComponent, EntityPausedEvent>(OnPaused);
}
private void OnPaused(EntityUid uid, UseDelayComponent component, EntityPausedEvent args)
{
if (args.Paused)
{
// This entity just got paused, but wasn't before
if (component.DelayEndTime != null)
component.RemainingDelay = _gameTiming.CurTime - component.DelayEndTime;
_activeDelays.Remove(component);
}
else if (component.RemainingDelay == null)
{
// Got unpaused, but had no active delay
return;
}
// We got unpaused, resume the delay/cooldown. Currently this takes for granted that ItemCooldownComponent
// handles the pausing on its own. I'm not even gonna check, because I CBF fixing it if it doesn't.
component.DelayEndTime = _gameTiming.CurTime + component.RemainingDelay;
Dirty(component);
_activeDelays.Add(component);
}
private void OnHandleState(EntityUid uid, UseDelayComponent component, ref ComponentHandleState args)
{
if (args.Current is not UseDelayComponentState state)
return;
component.LastUseTime = state.LastUseTime;
component.Delay = state.Delay;
component.DelayEndTime = state.DelayEndTime;
if (component.DelayEndTime == null)
_activeDelays.Remove(component);
else
_activeDelays.Add(component);
}
private void OnGetState(EntityUid uid, UseDelayComponent component, ref ComponentGetState args)
{
args.State = new UseDelayComponentState(component.LastUseTime, component.Delay, component.DelayEndTime);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var toRemove = new RemQueue<UseDelayComponent>();
var curTime = _gameTiming.CurTime;
var mQuery = EntityManager.GetEntityQuery<MetaDataComponent>();
foreach (var delay in _activeDelays)
{
if (curTime > delay.DelayEndTime
|| !mQuery.TryGetComponent(delay.Owner, out var meta)
|| meta.Deleted
|| delay.CancellationTokenSource?.Token.IsCancellationRequested == true)
{
toRemove.Add(delay);
}
}
foreach (var delay in toRemove)
{
delay.CancellationTokenSource = null;
delay.DelayEndTime = null;
_activeDelays.Remove(delay);
}
}
public void BeginDelay(EntityUid uid, UseDelayComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return;
if (component.ActiveDelay || Deleted(uid)) return;
component.CancellationTokenSource = new CancellationTokenSource();
DebugTools.Assert(!_activeDelays.Contains(component));
_activeDelays.Add(component);
var currentTime = _gameTiming.CurTime;
component.LastUseTime = currentTime;
component.DelayEndTime = currentTime + component.Delay;
Dirty(component);
// TODO just merge these components?
var cooldown = EnsureComp<ItemCooldownComponent>(component.Owner);
cooldown.CooldownStart = currentTime;
cooldown.CooldownEnd = component.DelayEndTime;
}
public void Cancel(UseDelayComponent component)
{
component.CancellationTokenSource?.Cancel();
component.CancellationTokenSource = null;
component.DelayEndTime = null;
_activeDelays.Remove(component);
Dirty(component);
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.Owner, component);
}
}