tamagotchi /TamagotchiGame/TamagotchiGame/Common/SpriteAnimation.cs

Language C# Lines 213
MD5 Hash e7d81a2dcedeee83e665e41e23aa7abc Estimated Cost $3,095 (why?)
Repository https://bitbucket.org/hasyimi/tamagotchi View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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;
        }
    }
}
Back to Top