GAM456-SpaceInvaders / SpaceInvaders / 1_Sprite / Sprite.cs
Sprite.cs
Raw
using System;
using System.Diagnostics;

namespace SpaceInvaders
{
    public class Sprite : SpriteBase
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        public enum SpriteName
        {
            Crab,
            Squid,
            Octopus,
            UFO,
            Explosion,

            Bomb_Cross,
            Bomb_Straight,
            Bomb_Zigzag,

            Brick,
            BrickLeft_Top0,
            BrickLeft_Top1,
            BrickLeft_Bottom,
            BrickRight_Top0,
            BrickRight_Top1,
            BrickRight_Bottom,

            Ship,
            Missile,

            Box1,
            Box2,

            NullObject,
            Uninitialized
        }

        private float x;
        private float y;
        private float sx;                    //width
        private float sy;                    //height
        private float angle;

        public SpriteName name;
        private Image pImage;                //ref to img

        private Azul.Sprite poAzulSprite;
        private Azul.Color poColor;           //like color filter
        private Azul.Rect poRect;    


        //-------------------------------------------------------------------
        //  CONSTRUCTION
        //-------------------------------------------------------------------

        public Sprite()
        {
            //set defaults
            this.x = 0.0f;
            this.y = 0.0f;
            this.sx = 1.0f;
            this.sy = 1.0f;
            this.angle = 0.0f;

            this.name = SpriteName.Uninitialized;
            this.pImage = null;

            //initialize azul elements
            this.poColor = new Azul.Color();         //LTN: owned by Sprite
            Debug.Assert(this.poColor != null);
            this.poColor.Set(1.0f, 1.0f, 1.0f, 1.0f);

            this.poAzulSprite = new Azul.Sprite();   //LTN: owned by Sprite
            Debug.Assert(this.poAzulSprite != null);

            this.poRect = new Azul.Rect();
            Debug.Assert(this.poRect != null);
        }


        //-------------------------------------------------------------------
        //  CLASS METHODS
        //-------------------------------------------------------------------

        public void SetSprite(SpriteName name, Image pImg, float x, float y, float sx,
            float sy, Azul.Color pColor)
        {
            //check values
            Debug.Assert(pImg != null);
            Debug.Assert(this.poRect != null);
            Debug.Assert(this.poAzulSprite != null);
            Debug.Assert(this.poColor != null);

            //set values
            this.pImage = pImg;
            this.name = name;

            this.poRect.Set(x, y, sx, sy);

            if(pColor != null)
            {
                this.poColor.Set(pColor);
            }
            
            this.poAzulSprite.Swap(pImg.GetAzulTexture(), pImg.GetAzulRect(), this.poRect, this.poColor);
            this.poAzulSprite.Update();

            this.x = poAzulSprite.x;
            this.y = poAzulSprite.y;
            this.sx = poAzulSprite.sx;
            this.sy = poAzulSprite.sy;
            this.angle = poAzulSprite.angle;
        }

        public void ChangeColor(float r, float g, float b, float alpha = 1.0f)
        {
            Debug.Assert(this.poColor != null);
            Debug.Assert(this.poAzulSprite != null);

            this.poColor.Set(r, g, b, alpha);
            this.poAzulSprite.SwapColor(this.poColor);
        }

        public void ChangeColor(Azul.Color color)
        {
            Debug.Assert(this.poColor != null);
            Debug.Assert(this.poAzulSprite != null);

            this.poColor.Set(color);
            this.poAzulSprite.SwapColor(this.poColor);
        }

        public void ChangeImage(Image pImage)
        {
            Debug.Assert(pImage != null);
            Debug.Assert(this.poAzulSprite != null);

            this.pImage = pImage;
            this.poAzulSprite.SwapTexture(this.pImage.GetAzulTexture());
            this.poAzulSprite.SwapTextureRect(this.pImage.GetAzulRect());
        }

        public Azul.Rect GetRect()
        {
            Debug.Assert(this.poRect != null);
            return this.poRect;
        }

        public void SetPos(float posx, float posy, float sx, float sy)
        {
            this.x = posx;
            this.y = posy;
            this.sx = sx;
            this.sy = sy;
        }


        //-------------------------------------------------------------------
        //  OVERRIDDEN METHODS
        //-------------------------------------------------------------------

        public override void Update()
        {
            this.poAzulSprite.x = this.x;
            this.poAzulSprite.y = this.y;
            this.poAzulSprite.sx = this.sx;
            this.poAzulSprite.sy = this.sy;
            this.poAzulSprite.angle = this.angle;

            this.poAzulSprite.Update();
        }

        public override void Render()
        {
            this.poAzulSprite.Render();
        }

        public override bool Compare(Node pNode)
        {
            Debug.Assert(pNode != null);

            Sprite pSpriteB = (Sprite)pNode;
            if(this.name == pSpriteB.name)
            {
                return true;
            }

            return false;
        }

        public override object GetName()
        {
            return this.name;
        }

        public override void Reset()
        {
            this.Clear();
        }

        public override void PrintStats()
        {
            Debug.WriteLine("      Name: {0} --> {1}", this.name, this.GetHashCode());
            //Debug.WriteLine("     Image: {0} --> {1}", this.pImage.name, this.pImage.GetHashCode());
            Debug.WriteLine("AzulSprite: {0}", this.poAzulSprite.GetHashCode());
            Debug.WriteLine("      x, y: {0}, {1}", this.x, this.y);
            Debug.WriteLine("    sx, sy: {0}, {1}", this.sx, this.sy);
            Debug.WriteLine("     angle: {0}", this.angle);
            Debug.WriteLine("     color: {0}", this.poColor.GetHashCode());
            base.PrintStats();
        }


        //-------------------------------------------------------------------
        //  PRIVATE METHODS
        //-------------------------------------------------------------------

        private void Clear()
        {
            //check values
            Debug.Assert(this.poColor != null);
            Debug.Assert(this.poAzulSprite != null);

            this.x = 0.0f;
            this.y = 0.0f;
            this.sx = 1.0f;
            this.sy = 1.0f;
            this.angle = 0.0f;

            this.name = SpriteName.Uninitialized;
            this.pImage = null;
            this.poColor.Set(1.0f, 1.0f, 1.0f, 1.0f);

            Image pImg = ImageMan.FindImage(Image.ImageName.HotPink);
            Debug.Assert(pImg != null);

            this.poRect.Set(0.0f, 0.0f, 1.0f, 1.0f);
            this.poAzulSprite.Swap(pImg.GetAzulTexture(), pImg.GetAzulRect(), this.poRect, this.poColor);
            this.poAzulSprite.Update();
        }


    } //end class

} //end namespace