Net.Like.Xue.Tokyo/Assets/BITKit/Core/Utils/IntervalUpdate.cs

51 lines
1.4 KiB
C#

namespace BITKit
{
[System.Serializable]
public class IntervalUpdate
{
public IntervalUpdate(){}
public IntervalUpdate(float interval)
{
Interval = interval;
}
public double Interval=1;
private double allowUpdateTime=0;
public bool AllowUpdateWithoutReset => BITApp.Time.TimeAsDouble >= allowUpdateTime;
public double Remaining => allowUpdateTime - BITApp.Time.TimeAsDouble;
public bool AllowUpdate
{
get
{
if (!AllowUpdateWithoutReset) return false;
Reset();
return true;
}
}
public void Reset(bool immediately = false)
{
allowUpdateTime = BITApp.Time.TimeAsDouble + (immediately ? 0 : Interval);
}
public void AddDelay(double interval,bool increment = false)
{
var currentTime = BITApp.Time.TimeAsDouble;
if (increment)
{
var remaining = allowUpdateTime - currentTime;
if (remaining < 0)
{
allowUpdateTime = currentTime + Interval + interval;
}
else
{
allowUpdateTime += interval;
}
}
else
{
allowUpdateTime = currentTime + interval;
}
}
}
}