using Robust.Shared.Timing; namespace Content.Shared.Timing; public sealed class UseDelaySystem : EntitySystem { [Dependency] private readonly IGameTiming _gameTiming = default!; [Dependency] private readonly MetaDataSystem _metadata = default!; public override void Initialize() { SubscribeLocalEvent(OnUnpaused); } private void OnUnpaused(Entity ent, ref EntityUnpausedEvent args) { // We got unpaused, resume the delay ent.Comp.DelayStartTime += args.PausedTime; ent.Comp.DelayEndTime += args.PausedTime; Dirty(ent); } public void SetDelay(Entity ent, TimeSpan delay) { if (ent.Comp.Delay == delay) return; ent.Comp.Delay = delay; Dirty(ent); } /// /// Returns true if the entity has a currently active UseDelay. /// public bool IsDelayed(Entity ent) { return ent.Comp.DelayEndTime >= _gameTiming.CurTime; } /// /// Cancels the current delay. /// public void CancelDelay(Entity ent) { ent.Comp.DelayEndTime = _gameTiming.CurTime; Dirty(ent); } /// /// Resets the UseDelay entirely for this entity if possible. /// /// Check if the entity has an ongoing delay, return false if it does, return true if it does not. public bool TryResetDelay(Entity ent, bool checkDelayed = false) { if (checkDelayed && IsDelayed(ent)) return false; var curTime = _gameTiming.CurTime; ent.Comp.DelayStartTime = curTime; ent.Comp.DelayEndTime = curTime - _metadata.GetPauseTime(ent) + ent.Comp.Delay; Dirty(ent); return true; } public bool TryResetDelay(EntityUid uid, bool checkDelayed = false, UseDelayComponent? component = null) { if (!Resolve(uid, ref component, false)) return false; return TryResetDelay((uid, component), checkDelayed); } }