Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xna.Framework;
- namespace BaseXNAGameLibrary.Utils
- {
- /// <summary>
- /// Performs an action over a specified amount of time.
- /// The action is an optional Action delegate,
- /// if not set this class simply serves as a mechanism of notifying another class when a certain amount of time has passed.
- ///
- /// Author: Nick Vanheer, immersivenick.wordpress.com
- /// Feel free to extend and let me know if you've made it more powerful!
- /// </summary>
- public class TimeAction
- {
- TimeSpan time = TimeSpan.FromSeconds(1);
- TimeSpan timeSoFar = TimeSpan.Zero;
- bool isLoop = false;
- public float LerpAmount { get; private set; } /* value between 0 and 1 indicating the current progress (0 = start, 1 = end) */
- /// <summary>
- /// Instantiates a new TimeAction object
- /// </summary>
- /// <param name="time">The time after which something needs to happen</param>
- /// <param name="loop">Repeats the action over the specified time interval</param>
- public TimeAction(TimeSpan time, bool loop = false)
- {
- this.time = time;
- this.isLoop = loop;
- }
- /// <summary>
- /// Updates the TimeAction class and executes an optional action after the time interval has been met
- /// </summary>
- /// <param name="gt">The current GameTime</param>
- /// <param name="a">An Action delegate to perform after the specified time has been elapsed (optional)</param>
- /// <returns></returns>
- public bool Update(GameTime gt, Action a = null)
- {
- if (timeSoFar < time)
- {
- timeSoFar += gt.ElapsedGameTime;
- if (timeSoFar >= time)
- {
- if(a != null)
- a();
- if (isLoop)
- timeSoFar = new TimeSpan(0, 0, 0);
- else
- timeSoFar = time;
- return true;
- }
- LerpAmount = (float)timeSoFar.Ticks / time.Ticks;
- return false;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement