mangolander /MangoLander/MangoLander/Graphics/AnimatedSprite.cs

Language C# Lines 122
MD5 Hash 05a9fddae4fca5da3466b18235cd4c72 Estimated Cost $2,307 (why?)
Repository https://bitbucket.org/tomnz/mangolander 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MangoLander.Graphics
{
    class AnimatedSprite : IUpdateable
    {
        public Texture2D Texture { get; private set; }
        public int FrameWidth { get; set; }
        public int FrameHeight { get; set; }

        public int CurrentFrame { get; private set; }
        public int TotalFrames { get; private set; }

        public double FrameDelay { get; set; }
        public bool LoopAnimation { get; set; }

        private bool _initialized = false;

        private double _elapsedSinceLastFrame = 0;
        private bool _playing = false;

        public AnimatedSprite()
        {
        }

        public AnimatedSprite(Texture2D texture, int frameWidth, int frameHeight, double frameDelay, bool loopAnimation)
        {
            Initialize(texture, frameWidth, frameHeight, frameDelay, loopAnimation);
        }

        public void Initialize(Texture2D texture, int frameWidth, int frameHeight, double frameDelay, bool loopAnimation)
        {
            this.Texture = texture;
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.FrameDelay = frameDelay;
            this.LoopAnimation = loopAnimation;
            this.CurrentFrame = 0;

            if (this.FrameWidth > this.Texture.Width || this.FrameHeight > this.Texture.Height)
            {
                // Illegal texture and frame height/width specification
                _initialized = false;
                return;
            }

            this.TotalFrames = this.Texture.Width / this.FrameWidth;
            _initialized = true;
        }

        private Rectangle GetCurrentFrameRectangle()
        {
            return new Rectangle(this.FrameWidth * this.CurrentFrame, 0, this.FrameWidth, this.FrameHeight);
        }

        // Control methods
        public void Play()
        {
            this.CurrentFrame = 0;
            _playing = true;
            _elapsedSinceLastFrame = 0;
        }

        public void Stop()
        {
            _playing = false;
            _elapsedSinceLastFrame = 0;
        }

        public void Reset()
        {
            this.CurrentFrame = 0;
            _elapsedSinceLastFrame = 0;
        }

        public void Draw(SpriteBatch spriteBatch, Rectangle destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth)
        {
            if (!_initialized)
                return;

            spriteBatch.Draw(this.Texture, destinationRectangle, this.GetCurrentFrameRectangle(), color, rotation, origin, effects, layerDepth);
        }

        public void Update(GameTime gameTime)
        {
            if (!_initialized || !_playing || this.FrameDelay <= 0 || this.TotalFrames == 0)
                return;

            double currentFrameTime = gameTime.ElapsedGameTime.TotalSeconds;
            _elapsedSinceLastFrame += currentFrameTime;

            while (_elapsedSinceLastFrame > this.FrameDelay)
            {
                _elapsedSinceLastFrame -= this.FrameDelay;
                this.CurrentFrame++;

                // Are we at the end?
                if (this.CurrentFrame == this.TotalFrames)
                {
                    if (this.LoopAnimation)
                    {
                        // Loop back to the start
                        this.CurrentFrame = 0;
                    }
                    else
                    {
                        // Stop playing
                        _playing = false;
                        _elapsedSinceLastFrame = 0;
                        break;
                    }
                }
            }
        }
    }
}
Back to Top