#region File Description //----------------------------------------------------------------------------- // AnimatedGameComponentAnimation.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; #endregion namespace CardsFramework { /// /// Represents an animation that can alter an animated component. /// public class AnimatedGameComponentAnimation { #region Fields and Properties protected TimeSpan Elapsed { get; set; } public AnimatedGameComponent Component { get; internal set; } /// /// An action to perform before the animation begins. /// public Action PerformBeforeStart; public object PerformBeforSartArgs { get; set; } /// /// An action to perform once the animation is complete. /// public Action PerformWhenDone; public object PerformWhenDoneArgs { get; set; } uint animationCycles = 1; /// /// Sets the amount of cycles to perform for the animation. /// public uint AnimationCycles { get { return animationCycles; } set { if (value > 0) { animationCycles = value; } } } public DateTime StartTime { get; set; } public TimeSpan Duration { get; set; } /// /// Returns the time at which the animation is estimated to end. /// public TimeSpan EstimatedTimeForAnimationCompletion { get { if (isStarted) { return (Duration - Elapsed); } else { return StartTime - DateTime.Now + Duration; } } } public bool IsLooped { get; set; } private bool isDone = false; private bool isStarted = false; #endregion #region Initiaizations /// /// Initializes a new instance of the class. Be default, an animation starts /// immediately and has a duration of 150 milliseconds. /// public AnimatedGameComponentAnimation() { StartTime = DateTime.Now; Duration = TimeSpan.FromMilliseconds(150); } #endregion /// /// Check whether or not the animation is done playing. Looped animations /// never finish playing. /// /// Whether or not the animation is done playing public bool IsDone() { if (!isDone) { isDone = !IsLooped && (Elapsed >= Duration); if (isDone && PerformWhenDone != null) { PerformWhenDone(PerformWhenDoneArgs); PerformWhenDone = null; } } return isDone; } /// /// Returns whether or not the animation is started. As a side-effect, starts /// the animation if it is not started and it is time for it to start. /// /// Whether or not the animation is started public bool IsStarted() { if (!isStarted) { if (StartTime <= DateTime.Now) { if (PerformBeforeStart != null) { PerformBeforeStart(PerformBeforSartArgs); PerformBeforeStart = null; } StartTime = DateTime.Now; isStarted = true; } } return isStarted; } /// /// Increases the amount of elapsed time as seen by the animation, but only /// if the animation is started. /// /// The timespan by which to incerase the animation's /// elapsed time. internal void AccumulateElapsedTime(TimeSpan elapsedTime) { if (isStarted) { Elapsed += elapsedTime; } } /// /// Runs the animation. /// /// Game time information. public virtual void Run(GameTime gameTime) { bool isStarted = IsStarted(); } } }