//----------------------------------------------------------------------------- // TransitionGameComponentAnimation.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 { /// /// An animation which moves a component from one point to the other. /// public class TransitionGameComponentAnimation : AnimatedGameComponentAnimation { Vector2 sourcePosition; Vector2 positionDelta; float percent = 0; Vector2 destinationPosition; /// /// Initializes a new instance of the class. /// /// The source position. /// The destination position. public TransitionGameComponentAnimation(Vector2 sourcePosition, Vector2 destinationPosition) { this.destinationPosition = destinationPosition; this.sourcePosition = sourcePosition; positionDelta = destinationPosition - sourcePosition; } /// /// Runs the transition animation. /// /// Game time information. public override void Run(GameTime gameTime) { if (IsStarted()) { // Calculate the animation's completion percentage. percent += (float)(gameTime.ElapsedGameTime.TotalSeconds / Duration.TotalSeconds); percent = MathHelper.Clamp(percent, 0f, 1f); // Apply cubic ease-out for smooth sliding float easedPercent = EaseOutCubic(percent); Component.CurrentPosition = sourcePosition + positionDelta * easedPercent; if (IsDone()) { Component.CurrentPosition = destinationPosition; } } } /// /// Cubic ease-out function for smooth animation. /// private float EaseOutCubic(float t) { return 1f - (float)Math.Pow(1f - t, 2); } } }