// --------------------------------------------------------------------------------------------------------------------
//
// Exit Games GmbH, 2012
//
//
// TimeKeeper Helper. See class description.
//
// developer@exitgames.com
// --------------------------------------------------------------------------------------------------------------------
namespace ExitGames.Client.DemoParticle
{
using System;
///
/// A utility class that turns it's ShouldExecute property to true after a set interval time has passed.
///
///
/// TimeKeepers can be useful to execute tasks in a certain interval within a game loop (integrating a recurring task into a certain thread).
///
/// An interval can be overridden, when you set ShouldExecute to true.
/// Call Reset after execution of whatever you do to re-enable the TimeKeeper (ShouldExecute becomes false until interval passed).
/// Being based on Environment.TickCount, this is not very precise but cheap.
///
public class TimeKeeper
{
private int lastExecutionTime = Environment.TickCount;
private bool shouldExecute;
/// Interval in which ShouldExecute should be true (and something is executed).
public int Interval { get; set; }
/// A disabled TimeKeeper never turns ShouldExecute to true. Reset won't affect IsEnabled!
public bool IsEnabled { get; set; }
/// Turns true of the time interval has passed (after reset or creation) or someone set ShouldExecute manually.
/// Call Reset to start a new interval.
public bool ShouldExecute
{
get { return (this.IsEnabled && (this.shouldExecute || (Environment.TickCount - this.lastExecutionTime > this.Interval))); }
set { this.shouldExecute = value; }
}
///
/// Creates a new TimeKeeper and sets it's interval.
///
///
public TimeKeeper(int interval)
{
this.IsEnabled = true;
this.Interval = interval;
}
/// ShouldExecute becomes false and the time interval is refreshed for next execution.
/// Does not affect IsEnabled.
public void Reset()
{
this.shouldExecute = false;
this.lastExecutionTime = Environment.TickCount;
}
}
}