using System;
namespace NEG.Utils.Timing
{
public class TimeMachine
{
private double time;
public TimeMachine()
{
time = 0;
}
///
/// Adds time int this TimeMachine
///
/// Amount of time to be added
public void Forward(double time)
{
this.time += time;
}
///
/// Decreeses the amount of time in this TimeMashine by up to given amount
///
///
/// Amount of time decressed
public double Warp(double maxTime)
{
double timeLeft = time - maxTime;
time = Math.Max(timeLeft, 0);
return Math.Min(maxTime + timeLeft, maxTime);
}
///
/// Attempts to decrese the amount of time in this TimeMachine
/// If there is at least time accumulated in this machine subtructs that amount and returns true, otherwise returns false
///
///
public bool TryWarp(double time)
{
if (this.time >= time)
{
this.time -= time;
return true;
}
return false;
}
///
/// Result is equivalent to calling as many times as possible, but is faster fo larger values
///
/// Single unit of warp time, must be non negative/param>
/// Maximum amount of warps, must be non negative
/// Amount of warps
public int MultiWarp(double interval, int limit = int.MaxValue)
{
int result = (int)Math.Floor(time / interval);
result = Math.Clamp(result, 0, limit);
time -= result * interval;
return result;
}
}
}