Game-Design / Sprint3 / ItemSprite.cs
ItemSprite.cs
Raw
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sprint2_actual
{
    public class ItemSprite : ISprite
    {
        public Texture2D Texture { get; set; }

        private int SpriteWidth;
        private int SpriteHeight;
        private int SourceX;
        private int SourceY;
        private int PermaSourceY;
        private int drawX;
        private int drawY;
        private Rectangle sourceRectangle;
        private Rectangle destinationRectangle;

        private int timeSinceFrameChange;
        private int msPerFrame;
        private int currentFrame;
        private int frames;

        public ItemSprite(Texture2D texture, int frames, int sourceX, int sourceY, int spriteWidth, int spriteHeight, int drawX, int drawY)
        {
            this.Texture = texture;
            this.SpriteWidth = spriteWidth;
            this.SpriteHeight = spriteHeight;
            this.SourceX = sourceX;
            this.SourceY = sourceY;
            this.PermaSourceY = sourceY;

            this.sourceRectangle = new Rectangle(this.SourceX, this.SourceY, this.SpriteWidth, this.SpriteHeight);
            this.destinationRectangle = new Rectangle((drawX - spriteWidth / 2), (drawY - spriteHeight / 2),
                spriteWidth, spriteHeight);

            this.currentFrame = 0;
            this.frames = frames;
            this.timeSinceFrameChange = 0;
            this.msPerFrame = 500;

        }

        public void draw()
        {
            

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

        public void update(GameTime gameTime)
        {

            this.timeSinceFrameChange += gameTime.ElapsedGameTime.Milliseconds;
            if (this.timeSinceFrameChange > this.msPerFrame)
            {
                this.timeSinceFrameChange = 0;

                this.currentFrame++;
                this.SourceY += this.SpriteHeight;

                if (this.currentFrame == this.frames)
                {
                    this.currentFrame = 0;
                    this.SourceY = PermaSourceY;
                }
            }

            sourceRectangle = new Rectangle(this.SourceX, this.SourceY, this.SpriteWidth, this.SpriteHeight);


        }
    }
}