diff options
author | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
---|---|---|
committer | Thomas Leyh <leyh.thomas@web.de> | 2016-07-24 08:14:18 +0200 |
commit | ced3d03bdb3ce866d832e03fb212865140905a9a (patch) | |
tree | 2a16c2063a46d3c354ce1585029dda3124f6ad93 /V3/UpdatesPerSecond.cs | |
parent | 0394dccaf06e1009e591a6ff4d645895574724c1 (diff) | |
download | V3-release.tar.gz V3-release.tar.bz2 |
Diffstat (limited to 'V3/UpdatesPerSecond.cs')
-rw-r--r-- | V3/UpdatesPerSecond.cs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/V3/UpdatesPerSecond.cs b/V3/UpdatesPerSecond.cs new file mode 100644 index 0000000..e180be1 --- /dev/null +++ b/V3/UpdatesPerSecond.cs @@ -0,0 +1,55 @@ +using Microsoft.Xna.Framework; + +namespace V3 +{ + /// <summary> + /// A class for telling when to do an update for events with a constant frequency. + /// </summary> + public sealed class UpdatesPerSecond + { + private double mTimeBetweenUpdates; + private double mTimeSinceLastUpdate; + + /// <summary> + /// Initializes class. + /// </summary> + /// <param name="frequency">How many updates should be done per second.</param> + public UpdatesPerSecond(double frequency) + { + ChangeFrequency(frequency); + } + + /// <summary> + /// Change update frequency. + /// </summary> + /// <param name="frequency">How many updates should be done per second.</param> + private void ChangeFrequency(double frequency) + { + mTimeBetweenUpdates = 1d / frequency; + } + + /// <summary> + /// Tells if it is time to do an update. + /// </summary> + /// <param name="gameTime">Current game time.</param> + /// <returns></returns> + public bool IsItTime(GameTime gameTime) + { + mTimeSinceLastUpdate += gameTime.ElapsedGameTime.TotalSeconds; + if (mTimeSinceLastUpdate >= mTimeBetweenUpdates) + { + Reset(); + return true; + } + return false; + } + + /// <summary> + /// Reset internal timer for checking if update should be done. + /// </summary> + private void Reset() + { + mTimeSinceLastUpdate = 0; + } + } +}
\ No newline at end of file |