Game-Design / Sprint3 / Concrete ISprites / AnimatedStillSprite.cs
AnimatedStillSprite.cs
Raw
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sprint2_actual
{
    class AnimatedStillSprite : ISprite
    {
        public Texture2D Texture { get; set; }
        public int OscilatingFramesNum { get; set; }
        public int SpriteWidth { get; set; }
        public int StartingSpriteX { get; set; }
        public int StartingSpriteY { get; set; }
        public Vector2 pos;
        private int currentFrame;
        private bool advancing;

        public AnimatedStillSprite(Texture2D texture, int oscilatingFramesNum, int spriteWidth, int startingSpriteX, int startingSpriteY, int startingPosX, int startingPosY)
        {
            Texture = texture;
            OscilatingFramesNum = oscilatingFramesNum;
            SpriteWidth = spriteWidth;
            StartingSpriteX = startingSpriteX;
            StartingSpriteY = startingSpriteY;
            pos = new Vector2(startingPosX, startingPosY);
            currentFrame = 0;
            advancing = true;
        }
        public void draw()
        {
            Rectangle sourceRectangle = new Rectangle(StartingSpriteX + (currentFrame * SpriteWidth), StartingSpriteY, SpriteWidth, SpriteWidth);
            Rectangle destinationRectangle = new Rectangle((int)pos.X, (int)pos.Y, SpriteWidth, SpriteWidth);

            GlobalVars.spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
        }

        public void update(GameTime gameTime)
        {
            if ((int)gameTime.TotalGameTime.TotalMilliseconds % 60 < 15)
            {
                if (advancing)
                {
                    currentFrame++;
                    if (currentFrame == OscilatingFramesNum)
                    {
                        advancing = false;
                        currentFrame--;
                    }
                }
                else
                {
                    currentFrame--;
                    if (currentFrame < 0)
                    {
                        advancing = true;
                        currentFrame++;
                    }
                }
            }
        }
    }
}