/TamagotchiGame/TamagotchiGame/Common/SpriteAnimation.cs
C# | 212 lines | 127 code | 37 blank | 48 comment | 22 complexity | e7d81a2dcedeee83e665e41e23aa7abc MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
-
- namespace TamagotchiGame.Common
- {
- public enum AnimationType
- {
- Looping,
- NonLooping,
- Pendulum
- }
-
- /// <summary>
- /// A concrete class of a SpriteAnimation, implementing the ISpriteAnimation interface.
- /// SpriteAnimation is a wrapper class for a single-row sprite sheet animation.
- /// It has supports for non-looping and looping animations.
- /// </summary>
- public class SpriteAnimation
- {
- #region Fields
-
- private int framesCount, currentFrame;
- private float elapsedTime;
- private Texture2D texture;
- private bool isReversed;
-
- #endregion
-
- #region Properties
-
- /// <summary>
- /// The number of frames for this animation.
- /// </summary>
- public int FramesCount { get { return framesCount; } }
-
- /// <summary>
- /// The current frame of this animation.
- /// </summary>
- public int CurrentFrame { get { return currentFrame; } }
-
- /// <summary>
- /// The width of a single frame.
- /// </summary>
- public int FrameWidth { get; set; }
-
- /// <summary>
- /// The height of the sprite sheet.
- /// </summary>
- public int FrameHeight { get; set; }
-
- /// <summary>
- /// The time delay for displaying each frame.
- /// </summary>
- public TimeSpan FrameDelay { get; set; }
-
- public TimeSpan TotalDelay
- {
- get { return TimeSpan.FromSeconds( FrameDelay.TotalSeconds * FramesCount ); }
-
- set
- {
- FrameDelay = TimeSpan.FromSeconds(value.TotalSeconds / FramesCount);
- }
- }
-
- /// <summary>
- /// The sprite sheet.
- /// </summary>
- public Texture2D Texture { get { return texture; } }
-
- /// <summary>
- /// The animation type. Either looping or non-looping.
- /// </summary>
- public AnimationType CurrentAnimationType { get; set; }
-
- /// <summary>
- /// Is the animation paused?
- /// </summary>
- public bool IsPaused { get; set; }
-
- #endregion
-
- /// <summary>
- /// The SpriteAnimation constructor.
- /// </summary>
- /// <param name="texture">The sprite sheet.</param>
- /// <param name="frameWidth">The width of a single frame.</param>
- /// <param name="frameDelay">The delay for playing each frame.</param>
- public SpriteAnimation(Texture2D texture, int frameWidth, TimeSpan frameDelay)
- {
- framesCount = texture.Width / frameWidth;
- FrameWidth = frameWidth;
- FrameHeight = texture.Height;
- FrameDelay = frameDelay;
- this.texture = texture;
-
- isReversed = false;
- }
-
- /// <summary>
- /// Updates the SpriteAnimation. In other words, this function is for playing the animation.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing information for the current frame.</param>
- public void Update(GameTime gameTime)
- {
- if (!IsPaused)
- {
- elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
-
- if (elapsedTime >= FrameDelay.TotalSeconds)
- {
- if (CurrentAnimationType == AnimationType.NonLooping)
- {
- if (currentFrame + 1 < framesCount)
- currentFrame++;
- }
- else if (CurrentAnimationType == AnimationType.Looping)
- {
- currentFrame = (currentFrame + 1) % framesCount;
- }
- else if (CurrentAnimationType == AnimationType.Pendulum)
- {
- if (isReversed)
- {
- currentFrame = (currentFrame - 1) % framesCount;
-
- if (currentFrame == 0)
- {
- IsPaused = true;
- isReversed = false;
- }
- }
- else
- {
- currentFrame = (currentFrame + 1) % framesCount;
-
- if (currentFrame + 1 == framesCount)
- {
- IsPaused = true;
- isReversed = true;
- }
- }
- }
-
- elapsedTime = 0.0f;
- }
- }
- }
-
- /// <summary>
- /// Displays the animation on the screen.
- /// </summary>
- /// <param name="spriteBatch">An instance of SpriteBatch.</param>
- /// <param name="position">The position where the frame will be displayed.</param>
- /// <param name="isFlipped">Is the sprite flipped?</param>
- public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth)
- {
- SpriteEffects spriteEffects;
-
- if (isFlipped)
- spriteEffects = SpriteEffects.FlipHorizontally;
-
- else spriteEffects = SpriteEffects.None;
-
- spriteBatch.Draw(texture, position,
- new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
- Color.White, 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
- }
-
- public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth, Color color)
- {
- SpriteEffects spriteEffects;
-
- if (isFlipped)
- spriteEffects = SpriteEffects.FlipHorizontally;
-
- else spriteEffects = SpriteEffects.None;
-
- spriteBatch.Draw(texture, position,
- new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
- color, 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
- }
-
- public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth, byte alpha)
- {
- SpriteEffects spriteEffects;
-
- if (isFlipped)
- spriteEffects = SpriteEffects.FlipHorizontally;
-
- else spriteEffects = SpriteEffects.None;
-
- spriteBatch.Draw(texture, position,
- new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
- new Color(255, 255, 255, alpha), 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
- }
-
- /// <summary>
- /// Resets the animation, setting its current frame to 0.
- /// </summary>
- public void Reset()
- {
- currentFrame = 0;
- elapsedTime = 0.0f;
- }
- }
- }