Game-Design / Sprint3_actual / Everything_NPC / NPCSprite.cs
NPCSprite.cs
Raw
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Sprint3_actual.Everything_NPC
{
    class NPCSprite : ISprite
    {
        private Texture2D Texture;

        private int SpriteWidth;
        private int SpriteHeight;
        private int SpriteX;
        private int SpriteY;
        private int PermaSourceX;
        private INPC NPC;
        private Rectangle sourceRectangle;
        private Rectangle destinationRectangle;

        private int currentFrame, timeSinceFrameChange, msPerFrame, frames;

        public NPCSprite(Texture2D texture, int framesNum, int sourceX, int sourceY, int spriteWidth, int spriteHeight, int drawX, int drawY, INPC npc)
        {
            this.Texture = texture;
            this.SpriteWidth = spriteWidth;
            this.SpriteHeight = spriteHeight;
            this.SpriteX = sourceX;
            this.SpriteY = sourceY;
            this.PermaSourceX = sourceX;
            this.NPC = npc;
            

            this.currentFrame = 0;
            this.frames = framesNum;
            this.timeSinceFrameChange = 0;
            this.msPerFrame = 200;
            this.sourceRectangle = new Rectangle(SpriteX + (currentFrame * SpriteWidth), SpriteY, SpriteWidth, SpriteHeight);

            if (NPC.GetType() != typeof(Moldorm)) this.destinationRectangle = NPC.getRect();
            else destinationRectangle = ((Moldorm)NPC).getMoldormBodyRect(-1);
        }

        public void draw()
        {
            if (NPC.GetType() != typeof(Moldorm)) this.destinationRectangle = NPC.getRect();
            else destinationRectangle = ((Moldorm)NPC).getMoldormBodyRect(-1);
            GlobalVars.spriteBatch.Draw(this.Texture, this.destinationRectangle, this.sourceRectangle, Color.White);
        }

        public void update(GameTime gameTime)
        {

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

                this.currentFrame++;
                this.SpriteX += this.SpriteWidth;

                if (this.currentFrame == this.frames)
                {
                    this.currentFrame = 0;
                    this.SpriteX = PermaSourceX;
                }
            }

            sourceRectangle = new Rectangle(this.SpriteX, this.SpriteY, this.SpriteWidth, this.SpriteHeight);


        }
    }

}