Files
tbd-station-14/Content.Client/GameObjects/EntitySystems/DoAfter/DoAfterSystem.cs
metalgearsloth 5b3b2e3207 do_after (#1616)
* do_after

Ports (most of) do_after from SS13.
Callers are expected to await the DoAfter task from the DoAfterSystem.
I had a dummy component for in-game testing which I removed for the PR so nothing in game uses do_after at the moment.
Currently only the movement cancellation is predicted client-side.

* Minor do_after doc cleanup

* do_the_shuffle

Fix nullable build errors.

* The last nullable

* Implement NeedHand

Thanks zum.

* nullable dereference

* Adjust the system query

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2020-08-08 18:16:13 +02:00

150 lines
5.1 KiB
C#

#nullable enable
using System.Linq;
using Content.Client.GameObjects.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
namespace Content.Client.GameObjects.EntitySystems.DoAfter
{
/// <summary>
/// Handles events that need to happen after a certain amount of time where the event could be cancelled by factors
/// such as moving.
/// </summary>
[UsedImplicitly]
public sealed class DoAfterSystem : EntitySystem
{
/*
* How this is currently setup (client-side):
* DoAfterGui handles the actual bars displayed above heads. It also uses FrameUpdate to flash cancellations
* DoAfterEntitySystem handles checking predictions every tick as well as removing / cancelling DoAfters due to time elapsed.
* DoAfterComponent handles network messages inbound as well as storing the DoAfter data.
* It'll also handle overall cleanup when one is removed (i.e. removing it from DoAfterGui).
*/
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
/// <summary>
/// Rather than checking attached player every tick we'll just store it from the message.
/// </summary>
private IEntity? _player;
/// <summary>
/// We'll use an excess time so stuff like finishing effects can show.
/// </summary>
public const float ExcessTime = 0.5f;
public DoAfterGui? Gui { get; private set; }
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PlayerAttachSysMessage>(message => HandlePlayerAttached(message.AttachedEntity));
}
public override void Shutdown()
{
base.Shutdown();
Gui?.Dispose();
Gui = null;
}
private void HandlePlayerAttached(IEntity? entity)
{
_player = entity;
// Setup the GUI and pass the new data to it if applicable.
Gui?.Dispose();
if (entity == null)
{
return;
}
Gui ??= new DoAfterGui();
Gui.AttachedEntity = entity;
if (entity.TryGetComponent(out DoAfterComponent doAfterComponent))
{
foreach (var (_, doAfter) in doAfterComponent.DoAfters)
{
Gui.AddDoAfter(doAfter);
}
}
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var currentTime = _gameTiming.CurTime;
if (_player == null || !_player.TryGetComponent(out DoAfterComponent doAfterComponent))
{
return;
}
var doAfters = doAfterComponent.DoAfters.ToList();
if (doAfters.Count == 0)
{
return;
}
var userGrid = _player.Transform.GridPosition;
// Check cancellations / finishes
foreach (var (id, doAfter) in doAfters)
{
var elapsedTime = (currentTime - doAfter.StartTime).TotalSeconds;
// If we've passed the final time (after the excess to show completion graphic) then remove.
if (elapsedTime > doAfter.Delay + ExcessTime)
{
Gui?.RemoveDoAfter(id);
doAfterComponent.Remove(doAfter);
continue;
}
// Don't predict cancellation if it's already finished.
if (elapsedTime > doAfter.Delay)
{
continue;
}
// Predictions
if (doAfter.BreakOnUserMove)
{
if (userGrid != doAfter.UserGrid)
{
doAfterComponent.Cancel(id, currentTime);
continue;
}
}
if (doAfter.BreakOnTargetMove)
{
var targetEntity = _entityManager.GetEntity(doAfter.TargetUid);
if (targetEntity.Transform.GridPosition != doAfter.TargetGrid)
{
doAfterComponent.Cancel(id, currentTime);
continue;
}
}
}
var count = doAfterComponent.CancelledDoAfters.Count;
// Remove cancelled DoAfters after ExcessTime has elapsed
for (var i = count - 1; i >= 0; i--)
{
var cancelled = doAfterComponent.CancelledDoAfters[i];
if ((currentTime - cancelled.CancelTime).TotalSeconds > ExcessTime)
{
doAfterComponent.Remove(cancelled.Message);
}
}
}
}
}