//-----------------------------------------------------------------------------
// AnimatedGameComponentAnimation.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
namespace CardsFramework
{
///
/// Represents an animation that can alter an animated component.
///
public class AnimatedGameComponentAnimation
{
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 TimeSpan StartDelay { 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 StartDelay + Duration;
}
}
}
public bool IsLooped { get; set; }
private bool isDone = false;
private bool isStarted = false;
private TimeSpan totalElapsed = TimeSpan.Zero;
///
/// Initializes a new instance of the class. Be default, an animation starts
/// immediately and has a duration of 150 milliseconds.
///
public AnimatedGameComponentAnimation()
{
StartDelay = TimeSpan.Zero;
Duration = TimeSpan.FromMilliseconds(150);
}
///
/// 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 (totalElapsed >= StartDelay)
{
if (PerformBeforeStart != null)
{
PerformBeforeStart(PerformBeforSartArgs);
PerformBeforeStart = null;
}
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)
{
totalElapsed += elapsedTime;
if (isStarted)
{
Elapsed += elapsedTime;
}
}
///
/// Runs the animation.
///
/// Game time information.
public virtual void Run(GameTime gameTime)
{
bool isStarted = IsStarted();
}
}
}