Files
OldThink/Content.Shared/Timing/UseDelaySystem.cs

60 lines
1.8 KiB
C#
Raw Normal View History

2022-03-09 20:12:17 +13:00
using Robust.Shared.Timing;
namespace Content.Shared.Timing;
public sealed class UseDelaySystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
2024-01-03 21:33:09 -04:00
[Dependency] private readonly MetaDataSystem _metadata = default!;
2022-03-09 20:12:17 +13:00
2024-01-03 21:33:09 -04:00
public void SetDelay(Entity<UseDelayComponent> ent, TimeSpan delay)
{
2024-01-03 21:33:09 -04:00
if (ent.Comp.Delay == delay)
2022-03-09 20:12:17 +13:00
return;
ent.Comp.Delay = delay;
2024-01-03 21:33:09 -04:00
Dirty(ent);
2022-03-09 20:12:17 +13:00
}
2024-01-03 21:33:09 -04:00
/// <summary>
/// Returns true if the entity has a currently active UseDelay.
/// </summary>
public bool IsDelayed(Entity<UseDelayComponent> ent)
2022-03-09 20:12:17 +13:00
{
2024-01-03 21:33:09 -04:00
return ent.Comp.DelayEndTime >= _gameTiming.CurTime;
2022-03-09 20:12:17 +13:00
}
2024-01-03 21:33:09 -04:00
/// <summary>
/// Cancels the current delay.
/// </summary>
public void CancelDelay(Entity<UseDelayComponent> ent)
2022-03-09 20:12:17 +13:00
{
2024-01-03 21:33:09 -04:00
ent.Comp.DelayEndTime = _gameTiming.CurTime;
Dirty(ent);
2022-03-09 20:12:17 +13:00
}
2023-11-14 22:55:45 +11:00
/// <summary>
2024-01-03 21:33:09 -04:00
/// Resets the UseDelay entirely for this entity if possible.
2023-11-14 22:55:45 +11:00
/// </summary>
2024-01-03 21:33:09 -04:00
/// <param name="checkDelayed">Check if the entity has an ongoing delay, return false if it does, return true if it does not.</param>
public bool TryResetDelay(Entity<UseDelayComponent> ent, bool checkDelayed = false)
2022-03-09 20:12:17 +13:00
{
2024-01-03 21:33:09 -04:00
if (checkDelayed && IsDelayed(ent))
2023-11-14 22:55:45 +11:00
return false;
2022-03-09 20:12:17 +13:00
2024-01-03 21:33:09 -04:00
var curTime = _gameTiming.CurTime;
ent.Comp.DelayStartTime = curTime;
ent.Comp.DelayEndTime = curTime - _metadata.GetPauseTime(ent) + ent.Comp.Delay;
Dirty(ent);
2023-11-14 22:55:45 +11:00
return true;
2022-03-09 20:12:17 +13:00
}
public bool TryResetDelay(EntityUid uid, bool checkDelayed = false, UseDelayComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return false;
return TryResetDelay((uid, component), checkDelayed);
}
2022-03-09 20:12:17 +13:00
}