PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/Pitch/Pitch/AnimationStrip.cs

https://bitbucket.org/slgriff/pitch
C# | 138 lines | 117 code | 21 blank | 0 comment | 3 complexity | cb90f689252d6947077643f6689d6a4f MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using Microsoft.Xna.Framework;
  5using Microsoft.Xna.Framework.Graphics;
  6
  7namespace Pitch
  8{
  9    public class AnimationStrip
 10    {
 11        #region Declarations
 12        private Texture2D texture;
 13        private int frameWidth;
 14        private int frameHeight;
 15
 16        private float frameTimer = 0f;
 17        private float frameDelay = 0.05f;
 18
 19        private int currentFrame;
 20
 21        private bool loopAnimation = true;
 22        private bool finishedPlaying = false;
 23
 24        private string name;
 25        private string nextAnimation;
 26        #endregion
 27
 28        #region Properties
 29        public int FrameWidth
 30        {
 31            get { return frameWidth; }
 32            set { frameWidth = value; }
 33        }
 34
 35        public int FrameHeight
 36        {
 37            get { return frameHeight; }
 38            set { frameHeight = value; }
 39        }
 40
 41        public Texture2D Texture
 42        {
 43            get { return texture; }
 44            set { texture = value; }
 45        }
 46
 47        public string Name
 48        {
 49            get { return name; }
 50            set { name = value; }
 51        }
 52
 53        public string NextAnimation
 54        {
 55            get { return nextAnimation; }
 56            set { nextAnimation = value; }
 57        }
 58
 59        public bool LoopAnimation
 60        {
 61            get { return loopAnimation; }
 62            set { loopAnimation = value; }
 63        }
 64
 65        public bool FinishedPlaying
 66        {
 67            get { return finishedPlaying; }
 68        }
 69
 70        public int FrameCount
 71        {
 72            get { return texture.Width / frameWidth; }
 73        }
 74
 75        public float FrameLength
 76        {
 77            get { return frameDelay; }
 78            set { frameDelay = value; }
 79        }
 80
 81        public Rectangle FrameRectangle
 82        {
 83            get
 84            {
 85                return new Rectangle(
 86                    currentFrame * frameWidth,
 87                    0,
 88                    frameWidth,
 89                    frameHeight);
 90            }
 91        }
 92        #endregion
 93
 94        #region Constructor
 95        public AnimationStrip(Texture2D texture, int frameWidth, string name)
 96        {
 97            this.texture = texture;
 98            this.frameWidth = frameWidth;
 99            this.frameHeight = texture.Height;
100            this.name = name;
101        }
102        #endregion
103
104        #region Public Methods
105        public void Play()
106        {
107            currentFrame = 0;
108            finishedPlaying = false;
109        }
110
111        public void Update(GameTime gameTime)
112        {
113            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
114
115            frameTimer += elapsed;
116
117            if (frameTimer >= frameDelay)
118            {
119                currentFrame++;
120                if (currentFrame >= FrameCount)
121                {
122                    if (loopAnimation)
123                    {
124                        currentFrame = 0;
125                    }
126                    else
127                    {
128                        currentFrame = FrameCount - 1;
129                        finishedPlaying = true;
130                    }
131                }
132                frameTimer = 0f;
133            }
134        }
135        #endregion
136
137    }
138}