From ced3d03bdb3ce866d832e03fb212865140905a9a Mon Sep 17 00:00:00 2001 From: Thomas Leyh Date: Sun, 24 Jul 2016 08:14:18 +0200 Subject: Add project files. --- V3/UpdatesPerSecond.cs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 V3/UpdatesPerSecond.cs (limited to 'V3/UpdatesPerSecond.cs') 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 +{ + /// + /// A class for telling when to do an update for events with a constant frequency. + /// + public sealed class UpdatesPerSecond + { + private double mTimeBetweenUpdates; + private double mTimeSinceLastUpdate; + + /// + /// Initializes class. + /// + /// How many updates should be done per second. + public UpdatesPerSecond(double frequency) + { + ChangeFrequency(frequency); + } + + /// + /// Change update frequency. + /// + /// How many updates should be done per second. + private void ChangeFrequency(double frequency) + { + mTimeBetweenUpdates = 1d / frequency; + } + + /// + /// Tells if it is time to do an update. + /// + /// Current game time. + /// + public bool IsItTime(GameTime gameTime) + { + mTimeSinceLastUpdate += gameTime.ElapsedGameTime.TotalSeconds; + if (mTimeSinceLastUpdate >= mTimeBetweenUpdates) + { + Reset(); + return true; + } + return false; + } + + /// + /// Reset internal timer for checking if update should be done. + /// + private void Reset() + { + mTimeSinceLastUpdate = 0; + } + } +} \ No newline at end of file -- cgit v1.2.1