Game-Design / Sprint3_actual / Concrete_ISprites / AnimatedMovingSprite.cs
AnimatedMovingSprite.cs
Raw
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Sprint3_actual.StateFiles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sprint3_actual
{
    class AnimatedMovingSprite : ISprite
    {
        public Texture2D Texture { get; set; }
        public int OscilatingFramesNum { get; set; }
        public int SpriteWidth { get; set; }
        private Link link;
        private int currentFrame;
        private bool advancing;
        private List<int> XVals { get; set; }
        private int YVal { get; set; }
        private const int spriteWH = 115;
        private const int shift = 57;
        public AnimatedMovingSprite(Texture2D texture, int oscilatingFramesNum, int spriteWidth, List<int> xVals, int yVal, Link link)
        {
            Texture = texture;
            OscilatingFramesNum = oscilatingFramesNum;
            SpriteWidth = spriteWidth;
            XVals = xVals;
            YVal = yVal;
            this.link = link;
            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);

            if (currentFrame == 0)
            {
                sourceRectangle = new Rectangle(XVals.ElementAt(0), YVal, spriteWH, spriteWH);
                destinationRectangle = new Rectangle((int)link.linkX - shift, (int)link.linkY - shift, spriteWH, spriteWH);
            }
            else
            {
                sourceRectangle = new Rectangle(XVals.ElementAt(1), YVal, spriteWH, spriteWH);
                destinationRectangle = new Rectangle((int)link.linkX - shift, (int)link.linkY - shift, spriteWH, spriteWH);
            }
            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++;
                    }
                }
            }
        }
    }
}