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


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

        public enum SpriteBoxName
        {
            Box,
            Box1,
            Box2,
            NullObject,
            Uninitialized
        }

        private float x;
        private float y;
        private float sx;
        private float sy;
        private float angle;

        public SpriteBoxName name;
        private Azul.Color poLineColor;
        private Azul.SpriteBox poAzulSpriteBox;

        //LTN: owned by Sprite Box 
        private static readonly Azul.Rect poTmpRect = new Azul.Rect();


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

        public SpriteBox() : base()
        {
            this.name = SpriteBoxName.Uninitialized;

            //shouldn't be null if new is called
            Debug.Assert(SpriteBox.poTmpRect != null);
            SpriteBox.poTmpRect.Set(0, 0, 1, 1);

            //LTN: owned by Sprite Box
            this.poLineColor = new Azul.Color(1, 1, 1);
            Debug.Assert(this.poLineColor != null);

            //LTN: owned by Sprite Box
            this.poAzulSpriteBox = new Azul.SpriteBox(poTmpRect, this.poLineColor);
            Debug.Assert(this.poAzulSpriteBox != null);

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


        //-------------------------------------------------------------------
        //  PUBLIC METHODS
        //-------------------------------------------------------------------

        public void SetBox(SpriteBoxName name, float x, float y, float sx, float sy, Azul.Color pLineColor)
        {
            Debug.Assert(this.poAzulSpriteBox != null);
            Debug.Assert(this.poLineColor != null);

            Debug.Assert(poTmpRect != null);
            SpriteBox.poTmpRect.Set(x, y, sx, sy);

            this.name = name;
            
            if(pLineColor != null)
            {
                this.poLineColor.Set(pLineColor);
            }

            this.poAzulSpriteBox.Swap(poTmpRect, this.poLineColor);

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

        public void SetBox(SpriteBoxName name, float x, float y, float width, float height)
        {
            Debug.Assert(this.poAzulSpriteBox != null);
            Debug.Assert(this.poLineColor != null);

            Debug.Assert(poTmpRect != null);
            SpriteBox.poTmpRect.Set(x, y, width, height);

            this.name = name;
            this.poAzulSpriteBox.Swap(poTmpRect, this.poLineColor);

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

        public void SetPos(float x, float y)
        {
            this.x = x;
            this.y = y;
        }

        public void ChangeColor(Azul.Color pColor)
        {
            Debug.Assert(pColor != null);
            this.poAzulSpriteBox.SwapColor(pColor);
        }

        public void SetColor(float r, float g, float b, float alpha = 1.0f)
        {
            Debug.Assert(this.poLineColor != null);
            this.poLineColor.Set(r, g, b, alpha);
            this.poAzulSpriteBox.SwapColor(this.poLineColor);
        }

        public void SetRect(float x, float y, float width, float height)
        {
            this.SetBox(this.name, x, y, width, height);
        }

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

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

            this.poAzulSpriteBox.Update();
        }

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

            SpriteBox pBox2 = (SpriteBox)pNode;
            if(this.name == pBox2.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("     Color: {0},{1},{2} ({3})", this.poLineColor.red, this.poLineColor.green, this.poLineColor.blue, this.poLineColor.GetHashCode());
            Debug.WriteLine("AzulSprite: {0}", this.poAzulSpriteBox.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);

            base.PrintStats();
        }
        

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

        private void Clear()
        {
            this.name = SpriteBoxName.Uninitialized;
            this.poLineColor.Set(1, 1, 1);

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


    } //end class

} //end namespace