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>
This commit is contained in:
130
Content.Server/GameObjects/Components/DoAfterComponent.cs
Normal file
130
Content.Server/GameObjects/Components/DoAfterComponent.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Shared.GameObjects.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Interfaces.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Network;
|
||||
|
||||
namespace Content.Server.GameObjects.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class DoAfterComponent : SharedDoAfterComponent
|
||||
{
|
||||
public IReadOnlyCollection<DoAfter> DoAfters => _doAfters.Keys;
|
||||
private readonly Dictionary<DoAfter, byte> _doAfters = new Dictionary<DoAfter, byte>();
|
||||
|
||||
// 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.
|
||||
private byte _runningIndex;
|
||||
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
switch (message)
|
||||
{
|
||||
case PlayerAttachedMsg _:
|
||||
UpdateClient();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Only sending data to the relevant client (at least, other clients don't need to know about do_after for now).
|
||||
private void UpdateClient()
|
||||
{
|
||||
if (!TryGetConnectedClient(out var connectedClient))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var (doAfter, id) in _doAfters)
|
||||
{
|
||||
// THE ALMIGHTY PYRAMID
|
||||
var message = new DoAfterMessage(
|
||||
id,
|
||||
doAfter.UserGrid,
|
||||
doAfter.TargetGrid,
|
||||
doAfter.StartTime,
|
||||
doAfter.EventArgs.Delay,
|
||||
doAfter.EventArgs.BreakOnUserMove,
|
||||
doAfter.EventArgs.BreakOnTargetMove,
|
||||
doAfter.EventArgs.Target?.Uid ?? EntityUid.Invalid);
|
||||
|
||||
SendNetworkMessage(message, connectedClient);
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryGetConnectedClient(out INetChannel? connectedClient)
|
||||
{
|
||||
connectedClient = null;
|
||||
|
||||
if (!Owner.TryGetComponent(out IActorComponent actorComponent))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
connectedClient = actorComponent.playerSession.ConnectedClient;
|
||||
if (!connectedClient.IsConnected)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Add(DoAfter doAfter)
|
||||
{
|
||||
_doAfters.Add(doAfter, _runningIndex);
|
||||
|
||||
if (TryGetConnectedClient(out var connectedClient))
|
||||
{
|
||||
var message = new DoAfterMessage(
|
||||
_runningIndex,
|
||||
doAfter.UserGrid,
|
||||
doAfter.TargetGrid,
|
||||
doAfter.StartTime,
|
||||
doAfter.EventArgs.Delay,
|
||||
doAfter.EventArgs.BreakOnUserMove,
|
||||
doAfter.EventArgs.BreakOnTargetMove,
|
||||
doAfter.EventArgs.Target?.Uid ?? EntityUid.Invalid);
|
||||
|
||||
SendNetworkMessage(message, connectedClient);
|
||||
}
|
||||
|
||||
_runningIndex++;
|
||||
}
|
||||
|
||||
public void Cancelled(DoAfter doAfter)
|
||||
{
|
||||
if (!_doAfters.TryGetValue(doAfter, out var index))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (TryGetConnectedClient(out var connectedClient))
|
||||
{
|
||||
var message = new CancelledDoAfterMessage(index);
|
||||
SendNetworkMessage(message, connectedClient);
|
||||
}
|
||||
|
||||
_doAfters.Remove(doAfter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call when the particular DoAfter is finished.
|
||||
/// Client should be tracking this independently.
|
||||
/// </summary>
|
||||
/// <param name="doAfter"></param>
|
||||
public void Finished(DoAfter doAfter)
|
||||
{
|
||||
if (!_doAfters.ContainsKey(doAfter))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_doAfters.Remove(doAfter);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user