PageRenderTime 104ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 51ms app.codeStats 0ms

/TamagotchiGame/TamagotchiGame/Common/SpriteAnimation.cs

https://bitbucket.org/hasyimi/tamagotchi
C# | 212 lines | 127 code | 37 blank | 48 comment | 22 complexity | e7d81a2dcedeee83e665e41e23aa7abc MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5
  6using Microsoft.Xna.Framework;
  7using Microsoft.Xna.Framework.Graphics;
  8
  9namespace TamagotchiGame.Common
 10{
 11    public enum AnimationType
 12    {
 13        Looping,
 14        NonLooping,
 15        Pendulum
 16    }
 17
 18    /// <summary>
 19    /// A concrete class of a SpriteAnimation, implementing the ISpriteAnimation interface.
 20    /// SpriteAnimation is a wrapper class for a single-row sprite sheet animation.
 21    /// It has supports for non-looping and looping animations.
 22    /// </summary>
 23    public class SpriteAnimation
 24    {
 25        #region Fields
 26
 27        private int framesCount, currentFrame;
 28        private float elapsedTime;
 29        private Texture2D texture;
 30        private bool isReversed;
 31
 32        #endregion
 33
 34        #region Properties
 35
 36        /// <summary>
 37        /// The number of frames for this animation.
 38        /// </summary>
 39        public int FramesCount { get { return framesCount; } }
 40
 41        /// <summary>
 42        /// The current frame of this animation.
 43        /// </summary>
 44        public int CurrentFrame { get { return currentFrame; } }
 45
 46        /// <summary>
 47        /// The width of a single frame.
 48        /// </summary>
 49        public int FrameWidth { get; set; }
 50
 51        /// <summary>
 52        /// The height of the sprite sheet.
 53        /// </summary>
 54        public int FrameHeight { get; set; }
 55
 56        /// <summary>
 57        /// The time delay for displaying each frame.
 58        /// </summary>
 59        public TimeSpan FrameDelay { get; set; }
 60
 61        public TimeSpan TotalDelay
 62        {
 63            get { return TimeSpan.FromSeconds( FrameDelay.TotalSeconds * FramesCount ); }
 64
 65            set
 66            {
 67                FrameDelay = TimeSpan.FromSeconds(value.TotalSeconds / FramesCount);
 68            }
 69        }
 70
 71        /// <summary>
 72        /// The sprite sheet.
 73        /// </summary>
 74        public Texture2D Texture { get { return texture; } }
 75
 76        /// <summary>
 77        /// The animation type. Either looping or non-looping.
 78        /// </summary>
 79        public AnimationType CurrentAnimationType { get; set; }
 80
 81        /// <summary>
 82        /// Is the animation paused?
 83        /// </summary>
 84        public bool IsPaused { get; set; }
 85
 86        #endregion
 87
 88        /// <summary>
 89        /// The SpriteAnimation constructor.
 90        /// </summary>
 91        /// <param name="texture">The sprite sheet.</param>
 92        /// <param name="frameWidth">The width of a single frame.</param>
 93        /// <param name="frameDelay">The delay for playing each frame.</param>
 94        public SpriteAnimation(Texture2D texture, int frameWidth, TimeSpan frameDelay)
 95        {
 96            framesCount = texture.Width / frameWidth;
 97            FrameWidth = frameWidth;
 98            FrameHeight = texture.Height;
 99            FrameDelay = frameDelay;
100            this.texture = texture;
101
102            isReversed = false;
103        }
104
105        /// <summary>
106        /// Updates the SpriteAnimation. In other words, this function is for playing the animation.
107        /// </summary>
108        /// <param name="gameTime">Provides a snapshot of timing information for the current frame.</param>
109        public void Update(GameTime gameTime)
110        {
111            if (!IsPaused)
112            {
113                elapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
114
115                if (elapsedTime >= FrameDelay.TotalSeconds)
116                {
117                    if (CurrentAnimationType == AnimationType.NonLooping)
118                    {
119                        if (currentFrame + 1 < framesCount)
120                            currentFrame++;
121                    }
122                    else if (CurrentAnimationType == AnimationType.Looping)
123                    {
124                        currentFrame = (currentFrame + 1) % framesCount;
125                    }
126                    else if (CurrentAnimationType == AnimationType.Pendulum)
127                    {
128                        if (isReversed)
129                        {
130                            currentFrame = (currentFrame - 1) % framesCount;
131
132                            if (currentFrame == 0)
133                            {
134                                IsPaused = true;
135                                isReversed = false;
136                            }
137                        }
138                        else
139                        {
140                            currentFrame = (currentFrame + 1) % framesCount;
141
142                            if (currentFrame + 1 == framesCount)
143                            {
144                                IsPaused = true;
145                                isReversed = true;
146                            }
147                        }
148                    }
149
150                    elapsedTime = 0.0f;
151                }
152            }
153        }
154
155        /// <summary>
156        /// Displays the animation on the screen.
157        /// </summary>
158        /// <param name="spriteBatch">An instance of SpriteBatch.</param>
159        /// <param name="position">The position where the frame will be displayed.</param>
160        /// <param name="isFlipped">Is the sprite flipped?</param>
161        public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth)
162        {
163            SpriteEffects spriteEffects;
164
165            if (isFlipped)
166                spriteEffects = SpriteEffects.FlipHorizontally;
167
168            else spriteEffects = SpriteEffects.None;
169
170            spriteBatch.Draw(texture, position,
171                new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
172                Color.White, 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
173        }
174
175        public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth, Color color)
176        {
177            SpriteEffects spriteEffects;
178
179            if (isFlipped)
180                spriteEffects = SpriteEffects.FlipHorizontally;
181
182            else spriteEffects = SpriteEffects.None;
183
184            spriteBatch.Draw(texture, position,
185                new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
186                color, 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
187        }
188
189        public void Draw(SpriteBatch spriteBatch, Vector2 position, bool isFlipped, float depth, byte alpha)
190        {
191            SpriteEffects spriteEffects;
192
193            if (isFlipped)
194                spriteEffects = SpriteEffects.FlipHorizontally;
195
196            else spriteEffects = SpriteEffects.None;
197
198            spriteBatch.Draw(texture, position,
199                new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight),
200                new Color(255, 255, 255, alpha), 0.0f, Vector2.Zero, 1.0f, spriteEffects, depth);
201        }
202
203        /// <summary>
204        /// Resets the animation, setting its current frame to 0.
205        /// </summary>
206        public void Reset()
207        {
208            currentFrame = 0;
209            elapsedTime = 0.0f;
210        }
211    }
212}