using System;
using System.Runtime.InteropServices;
namespace Sandbox;
///
/// A convenience struct to easily measure time since an event last happened, based on .
///
/// Typical usage would see you assigning 0 to a variable of this type to reset the timer.
/// Then the struct would return time since the last reset. i.e.:
///
/// TimeSince lastUsed = 0;
/// if ( lastUsed > 10 ) { /*Do something*/ }
///
///
[Expose]
[StructLayout( LayoutKind.Sequential )]
public struct TimeSince : IEquatable
{
double time;
public static explicit operator double( TimeSince ts ) => Time.NowDouble - ts.time;
public static implicit operator float( TimeSince ts ) => (float)(Time.NowDouble - ts.time);
public static implicit operator TimeSince( double ts ) => new() { time = Time.NowDouble - ts };
public static implicit operator TimeSince( float ts ) => new() { time = Time.NowDouble - ts };
public static bool operator <( in TimeSince ts, float f ) => ts.Relative < f;
public static bool operator >( in TimeSince ts, float f ) => ts.Relative > f;
public static bool operator <=( in TimeSince ts, float f ) => ts.Relative <= f;
public static bool operator >=( in TimeSince ts, float f ) => ts.Relative >= f;
public static bool operator <( in TimeSince ts, int f ) => ts.Relative < f;
public static bool operator >( in TimeSince ts, int f ) => ts.Relative > f;
public static bool operator <=( in TimeSince ts, int f ) => ts.Relative <= f;
public static bool operator >=( in TimeSince ts, int f ) => ts.Relative >= f;
///
/// Time at which the timer reset happened, based on .
///
public float Absolute => (float)time;
///
/// Time passed since last reset, in seconds.
///
public float Relative => this;
public override string ToString() => $"{Relative}";
#region equality
public static bool operator ==( TimeSince left, TimeSince right ) => left.Equals( right );
public static bool operator !=( TimeSince left, TimeSince right ) => !(left == right);
public override bool Equals( object obj ) => obj is TimeSince o && Equals( o );
public readonly bool Equals( TimeSince o ) => this.time == o.time;
public readonly override int GetHashCode() => time.GetHashCode();
#endregion
}
///
/// A convenience struct to easily manage a time countdown, based on .
///
/// Typical usage would see you assigning to a variable of this type a necessary amount of seconds.
/// Then the struct would return the time countdown, or can be used as a bool i.e.:
///
/// TimeUntil nextAttack = 10;
/// if ( nextAttack ) { /*Do something*/ }
///
///
[Expose]
[StructLayout( LayoutKind.Sequential )]
public struct TimeUntil : IEquatable
{
private double time;
private double startTime;
public static implicit operator bool( TimeUntil ts ) => Time.NowDouble >= ts.time;
public static explicit operator double( TimeUntil ts ) => ts.time - Time.NowDouble;
public static implicit operator float( TimeUntil ts ) => (float)(ts.time - Time.NowDouble);
public static bool operator <( in TimeUntil ts, float f ) => ts.Relative < f;
public static bool operator >( in TimeUntil ts, float f ) => ts.Relative > f;
public static bool operator <=( in TimeUntil ts, float f ) => ts.Relative <= f;
public static bool operator >=( in TimeUntil ts, float f ) => ts.Relative >= f;
public static bool operator <( in TimeUntil ts, int f ) => ts.Relative < f;
public static bool operator >( in TimeUntil ts, int f ) => ts.Relative > f;
public static bool operator <=( in TimeUntil ts, int f ) => ts.Relative <= f;
public static bool operator >=( in TimeUntil ts, int f ) => ts.Relative >= f;
public static implicit operator TimeUntil( double ts ) => new() { time = Time.NowDouble + ts, startTime = Time.NowDouble };
public static implicit operator TimeUntil( float ts ) => new() { time = Time.NowDouble + ts, startTime = Time.NowDouble };
///
/// Time to which we are counting down to, based on .
///
public float Absolute => (float)time;
///
/// The actual countdown, in seconds.
///
public float Relative => this;
///
/// Amount of seconds passed since the countdown started.
///
public float Passed => (float)(Time.NowDouble - startTime);
///
/// The countdown, but as a fraction, i.e. a value from 0 (start of countdown) to 1 (end of countdown)
///
public float Fraction => (float)Math.Clamp( (Time.NowDouble - startTime) / (time - startTime), 0.0f, 1.0f );
public override string ToString() => $"{Relative}";
#region equality
public static bool operator ==( TimeUntil left, TimeUntil right ) => left.Equals( right );
public static bool operator !=( TimeUntil left, TimeUntil right ) => !(left == right);
public readonly override bool Equals( object obj ) => obj is TimeUntil o && Equals( o );
public readonly bool Equals( TimeUntil o ) => time == o.time;
public readonly override int GetHashCode() => HashCode.Combine( time );
#endregion
}