Created
March 14, 2019 14:16
-
-
Save RevenantX/9cb604403d2bc5eec631b06fc255d528 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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