Created
July 27, 2016 09:05
-
-
Save zombience/4ee364b9320746bc182af2f866412049 to your computer and use it in GitHub Desktop.
SimpleTimer is a bit of code for Unity3D that provides a way to keep track of intervals. It can return time as remaining time, or as a normalized interval (between 0 and 1).
This file contains hidden or 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
using UnityEngine; | |
using System.Collections; | |
public class SimpleTimer | |
{ | |
public float life { get { return _life; } private set { _life = value; } } | |
public float elapsed { get { return _curTime; } } | |
public float normalized { get { return _curTime / life; } } // returns timer as a range between 0 and 1 | |
public float remaining { get { return life - elapsed; } } | |
public bool isFinished { get { return elapsed >= life; } } | |
public bool isPaused { get { return _isPaused; } private set { _isPaused = value; } } | |
protected bool _fixedTime; | |
protected bool _isPaused; | |
protected float _life; | |
protected float _startTime; | |
protected float _pauseTime; | |
protected float _curTime { get { return (isPaused ? _pauseTime : _getTime) - _startTime; } set { _pauseTime = value; } } | |
protected float _getTime { get { return _fixedTime ? Time.fixedTime : Time.time; } } | |
public SimpleTimer() { } | |
/// <summary> | |
/// timer is implicitly started on instantiation | |
/// </summary> | |
/// <param name="lifeSpan">length of the timer</param> | |
/// <param name="useFixedTime">use fixed (physics) time or screen update time</param> | |
public SimpleTimer(float lifeSpan, bool useFixedTime = false) { life = lifeSpan; _fixedTime = useFixedTime; _startTime = _getTime; } | |
/// <summary> | |
/// starts timer again using time remaining | |
/// </summary> | |
public void Resume() { _startTime = (isPaused ? _getTime - elapsed : _getTime); isPaused = false; } | |
/// <summary> | |
/// stop pauses the timer and allows for resume at current elapsed time | |
/// </summary> | |
public void Stop() | |
{ | |
if (!isPaused) | |
{ | |
_curTime = _getTime; | |
isPaused = true; | |
} | |
} | |
/// <summary> | |
/// Add time to the timer | |
/// </summary> | |
/// <param name="amt"></param> | |
public void AddTime(float amt) | |
{ | |
_life += amt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment