Skip to content

Instantly share code, notes, and snippets.

@RevenantX
Created March 14, 2019 14:16
Show Gist options
  • Save RevenantX/9cb604403d2bc5eec631b06fc255d528 to your computer and use it in GitHub Desktop.
Save RevenantX/9cb604403d2bc5eec631b06fc255d528 to your computer and use it in GitHub Desktop.
namespace Game.Shared.Helpers
{
public class GameTimer
{
private float _time;
private float _maxTime;
public GameTimer(float maxTime)
{
_maxTime = maxTime;
Finish();
}
public GameTimer()
{
}
public float MaxTime
{
get { return _maxTime; }
}
public float ElapsedTime
{
get { return _time; }
}
public bool IsTimeElapsed
{
get { return _time >= _maxTime; }
}
public float Progress
{
get
{
float p = _time/_maxTime;
return p > 1f ? 1f : p;
}
}
public void Invert()
{
if (_time < _maxTime)
_time = _maxTime - _time;
else
_time = 0f;
}
public void Reset()
{
if (_time > _maxTime)
_time -= _maxTime;
else
_time = 0f;
}
public void Reset(float maxTime)
{
_maxTime = maxTime;
_time = 0f;
}
public void Finish()
{
_time = _maxTime;
}
public float LerpByProgress(float a, float b)
{
return a + (b - a) * Progress;
}
public float LerpByProgress(float a, float b, bool inverse)
{
return inverse
? b + (a - b) * Progress
: a + (b - a) * Progress;
}
public bool UpdateAndCheck(float delta)
{
if (IsTimeElapsed)
return false;
return Update(delta);
}
public bool Update(float delta)
{
if(_time < _maxTime)
_time += delta;
return IsTimeElapsed;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment