#region File Description
//-----------------------------------------------------------------------------
// FramesetGameComponentAnimation.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.Graphics;
using Microsoft.Xna.Framework;
#endregion
namespace CardsFramework
{
///
/// A "typical" animation that consists of alternating between a set of frames.
///
public class FramesetGameComponentAnimation : AnimatedGameComponentAnimation
{
#region Fields
Texture2D framesTexture;
int numberOfFrames;
int numberOfFramePerRow;
Vector2 frameSize;
private double percent = 0;
#endregion
#region Initializations
///
/// Creates a new instance of the class.
///
/// The frames texture (animation sheet).
/// The number of frames in the sheet.
/// The number of frame per row.
/// Size of the frame.
public FramesetGameComponentAnimation(Texture2D framesTexture, int numberOfFrames,
int numberOfFramePerRow, Vector2 frameSize)
{
this.framesTexture = framesTexture;
this.numberOfFrames = numberOfFrames;
this.numberOfFramePerRow = numberOfFramePerRow;
this.frameSize = frameSize;
}
#endregion
///
/// Runs the frame set animation.
///
/// Game time information.
public override void Run(GameTime gameTime)
{
if (IsStarted())
{
// Calculate the completion percent of the animation
percent += (((gameTime.ElapsedGameTime.TotalMilliseconds /
(Duration.TotalMilliseconds / AnimationCycles)) * 100));
if (percent >= 100)
{
percent = 0;
}
// Calculate the current frame index
int animationIndex = (int)(numberOfFrames * percent / 100);
Component.CurrentSegment =
new Rectangle(
(int)frameSize.X * (animationIndex % numberOfFramePerRow),
(int)frameSize.Y * (animationIndex / numberOfFramePerRow),
(int)frameSize.X, (int)frameSize.Y);
Component.CurrentFrame = framesTexture;
}
else
{
Component.CurrentFrame = null;
Component.CurrentSegment = null;
}
base.Run(gameTime);
}
}
}