Files
OldThink/Content.Client/DoAfter/DoAfterSystem.cs

108 lines
3.7 KiB
C#
Raw Normal View History

2023-08-25 20:40:42 +02:00
using System.Diagnostics.CodeAnalysis;
2022-02-20 06:15:57 +11:00
using Content.Shared.DoAfter;
using Content.Shared.Hands.Components;
using Robust.Client.Graphics;
using Robust.Client.Player;
namespace Content.Client.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>
public sealed class DoAfterSystem : SharedDoAfterSystem
{
[Dependency] private readonly IOverlayManager _overlay = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly MetaDataSystem _metadata = default!;
public override void Initialize()
{
base.Initialize();
2024-01-24 13:20:14 +03:00
_overlay.AddOverlay(new DoAfterOverlay(EntityManager, GameTiming, _player));
}
public override void Shutdown()
{
base.Shutdown();
_overlay.RemoveOverlay<DoAfterOverlay>();
}
2022-08-13 14:32:23 +10:00
public override void Update(float frameTime)
{
// Currently this only predicts do afters initiated by the player.
2022-02-20 06:15:57 +11:00
// TODO maybe predict do-afters if the local player is the target of some other players do-after? Specifically
// ones that depend on the target not moving, because the cancellation of those do afters should be readily
// predictable by clients.
var playerEntity = _player.LocalPlayer?.ControlledEntity;
2022-02-20 06:15:57 +11:00
if (!TryComp(playerEntity, out ActiveDoAfterComponent? active))
return;
2022-02-20 06:15:57 +11:00
if (_metadata.EntityPaused(playerEntity.Value))
return;
2022-02-20 06:15:57 +11:00
var time = GameTiming.CurTime;
var comp = Comp<DoAfterComponent>(playerEntity.Value);
var xformQuery = GetEntityQuery<TransformComponent>();
var handsQuery = GetEntityQuery<HandsComponent>();
Update(playerEntity.Value, active, comp, time, xformQuery, handsQuery);
}
2023-08-25 20:40:42 +02:00
/// <summary>
/// Try to find an active do-after being executed by the local player.
/// </summary>
/// <param name="entity">The entity the do after must be targeting (<see cref="DoAfterArgs.Target"/>)</param>
/// <param name="doAfter">The found do-after.</param>
/// <param name="event">The event to be raised on the found do-after when it completes.</param>
/// <param name="progress">The progress of the found do-after, from 0 to 1.</param>
/// <typeparam name="T">The type of event that must be raised by the found do-after.</typeparam>
/// <returns>True if a do-after was found.</returns>
public bool TryFindActiveDoAfter<T>(
EntityUid entity,
[NotNullWhen(true)] out Shared.DoAfter.DoAfter? doAfter,
[NotNullWhen(true)] out T? @event,
out float progress)
where T : DoAfterEvent
{
var playerEntity = _player.LocalPlayer?.ControlledEntity;
doAfter = null;
@event = null;
progress = default;
if (!TryComp(playerEntity, out ActiveDoAfterComponent? active))
return false;
if (_metadata.EntityPaused(playerEntity.Value))
return false;
var comp = Comp<DoAfterComponent>(playerEntity.Value);
var time = GameTiming.CurTime;
foreach (var candidate in comp.DoAfters.Values)
{
if (candidate.Cancelled)
continue;
if (candidate.Args.Target != entity)
continue;
if (candidate.Args.Event is not T candidateEvent)
continue;
@event = candidateEvent;
doAfter = candidate;
var elapsed = time - doAfter.StartTime;
progress = (float) Math.Min(1, elapsed.TotalSeconds / doAfter.Args.Delay.TotalSeconds);
return true;
}
return false;
}
}