All pastes #978816 Raw Edit

tobz

public text v1 · immutable
·published 2008-04-09 20:54 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCell.Core.Timers
{
	public struct TimerEntry
	{
		private float _totalTime;
		public float InitialDelay, Interval;
		Action<float> Callback;

		public TimerEntry(float delay, float interval, Action<float> callback)
		{
			_totalTime = 0.0f;
			InitialDelay = delay;
			Interval = interval;
			Callback = callback;
		}

		public void Start()
		{
			_totalTime = 0.0f;
		}

		public void Stop()
		{
			_totalTime = -1.0f;
		}

		public void Update(float updateDelta)
		{
			// means this timer is not running.
			if (_totalTime == -1.0f)
				return;

			if (InitialDelay > 0.0f)
			{
				InitialDelay -= updateDelta;
			}
			else
			{
				if (_totalTime == 0.0f)
				{
					// initial fire.
					Callback(_totalTime);
				}
				else
				{
					_totalTime += updateDelta;

					if (_totalTime >= Interval)
					{
						// time to fire off? i tink so!
						Callback(_totalTime);
						_totalTime = 0.0f;
					}
				}
			}
		}
	}
}