using Robust.Shared.Timing;
namespace Content.Shared.Timing;
///
/// Represents a range of an "action" in time, as start/end times.
///
///
/// Positions in time are represented as s, usually from
/// or .
///
/// The time the action starts.
/// The time action ends.
[Serializable]
public record struct StartEndTime(TimeSpan Start, TimeSpan End)
{
///
/// How long the action takes.
///
public TimeSpan Length => End - Start;
///
/// Get how far the action has progressed relative to a time value.
///
/// The time to get the current progress value for.
/// If true, clamp values outside the time range to 0 through 1.
///
///
/// A progress value. Zero means is at ,
/// one means is at .
///
///
/// This function returns if and are identical.
///
///
public float ProgressAt(TimeSpan time, bool clamp = true)
{
var length = Length;
if (length == default)
return float.NaN;
var progress = (float) ((time - Start) / length);
if (clamp)
progress = MathHelper.Clamp01(progress);
return progress;
}
public static StartEndTime FromStartDuration(TimeSpan start, TimeSpan duration)
{
return new StartEndTime(start, start + duration);
}
public static StartEndTime FromStartDuration(TimeSpan start, float durationSeconds)
{
return new StartEndTime(start, start + TimeSpan.FromSeconds(durationSeconds));
}
public static StartEndTime FromCurTime(IGameTiming gameTiming, TimeSpan duration)
{
return FromStartDuration(gameTiming.CurTime, duration);
}
public static StartEndTime FromCurTime(IGameTiming gameTiming, float durationSeconds)
{
return FromStartDuration(gameTiming.CurTime, durationSeconds);
}
}