GAM456-SpaceInvaders / SpaceInvaders / 4_GameObject / GameObject.cs
GameObject.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public abstract class GameObject : Component
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------
        public enum ObjectName
        {
            AlienGrid,
            AlienColumn,
            AlienColumn_0,
            AlienColumn_1,
            AlienColumn_2,
            AlienColumn_3,
            AlienColumn_4,
            AlienColumn_5,
            AlienColumn_6,
            AlienColumn_7,
            AlienColumn_8,
            AlienColumn_9,
            AlienColumn_10,

            Crab,
            Squid,
            Octopus,
            UFORoot,
            UFO,

            BombRoot,
            Bomb,

            MissileGroup,
            Missile,

            Ship,
            ShipRoot,

            ShieldGrids,
            ShieldRoot,
            ShieldColumn_0,
            ShieldColumn_1,
            ShieldColumn_2,
            ShieldColumn_3,
            ShieldColumn_4,
            ShieldColumn_5,
            ShieldColumn_6,
            ShieldBrick,
            ShieldBrick_LeftTop0,
            ShieldBrick_LeftTop1,
            ShieldBrick_LeftBottom,
            ShieldBrick_RightTop0,
            ShieldBrick_RightTop1,
            ShieldBrick_RightBottom,

            WallGroup,
            Left_Wall,
            Right_Wall,
            Top_Wall,
            Bottom_Wall,

            Bumpers,
            LeftBumper,
            RightBumper,

            NullObject,
            Uninitialized
        }

        public GameObject.ObjectName name;
        public Sprite.SpriteName spriteName;
        public SpriteProxy pProxy;
        public CollisionObject pColObj;

        public bool isDead;
        public float x;
        public float y;


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

        protected GameObject(Component.Type type, GameObject.ObjectName objName, Sprite.SpriteName proxyName) 
            : base(type)
        {
            this.name = objName;
            this.x = 0.0f;
            this.y = 0.0f;

            this.isDead = false;
            this.spriteName = proxyName;
            SpriteProxy pProxy = SpriteProxyMan.AddProxy(proxyName);
            Debug.Assert(pProxy != null);

            this.pProxy = pProxy;

            this.pColObj = new CollisionObject(this.pProxy);
            Debug.Assert(this.pColObj != null);
        }

        protected GameObject(Component.Type type, GameObject.ObjectName objName, Sprite.SpriteName spriteName, float x, float y) 
            : base(type)
        {
            this.name = objName;
            this.x = x;
            this.y = y;

            this.isDead = false;
            this.spriteName = spriteName;
            this.pProxy = SpriteProxyMan.AddProxy(this.spriteName);

            this.pColObj = new CollisionObject(this.pProxy);
            Debug.Assert(this.pColObj != null);
        }


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

        public void ActivateCollisionSprite(SpriteBatch pBatch)
        {
            Debug.Assert(pBatch != null);
            Debug.Assert(this.pColObj != null);

            pBatch.AttachBatch(this.pColObj.pCollisionSprite);
        }

        public void ActivateSprite(SpriteBatch pBatch)
        {
            Debug.Assert(pBatch != null);
            pBatch.AttachBatch(this.pProxy);
        }

        public void SetCollisionColor(float r, float g, float b)
        {
            Debug.Assert(this.pColObj != null);
            Debug.Assert(this.pColObj.pCollisionSprite != null);

            this.pColObj.pCollisionSprite.SetColor(r, g, b);
        }

        public CollisionObject GetCollisionObj()
        {
            Debug.Assert(this.pColObj != null);
            return this.pColObj;
        }

        protected void UpdateBoundingBox(Component pStart)
        {
            GameObject pNode = (GameObject)pStart;
            CollisionRect pColTotal = this.pColObj.pCollisionRect;

            //get first child
            pNode = (GameObject)ForwardIterator.GetChild(pNode);

            if (pNode != null)
            {
                //union to first block
                pColTotal.Set(pNode.pColObj.pCollisionRect);

                //loop thro siblings
                while (pNode != null)
                {
                    pColTotal.Union(pNode.pColObj.pCollisionRect);
                    pNode = (GameObject)ForwardIterator.GetSibling(pNode);
                }
            }

            this.x = this.pColObj.pCollisionRect.x;
            this.y = this.pColObj.pCollisionRect.y;
        }

        public virtual void Update()
        {
            //update proxy
            Debug.Assert(this.pProxy != null);
            this.pProxy.SetPos(this.x, this.y);

            //update collision obj
            Debug.Assert(this.pColObj != null);
            this.pColObj.UpdatePosition(this.x, this.y);

            //update collision sprite
            Debug.Assert(this.pColObj.pCollisionSprite != null);
            this.pColObj.pCollisionSprite.Update();
        }

        public virtual void Remove()
        {
            //remove from sprite batch
            //first find sprite node
            Debug.Assert(this.pProxy != null);
            SpriteNode pSpriteNode = this.pProxy.GetSpriteNode();

            //remove from batch man
            Debug.Assert(pSpriteNode != null);
            SpriteBatchMan.RemoveBatch(pSpriteNode);

            //remove collision sprite from sprite batch
            Debug.Assert(this.pColObj != null);
            Debug.Assert(this.pColObj.pCollisionSprite != null);
            pSpriteNode = this.pColObj.pCollisionSprite.GetSpriteNode();

            Debug.Assert(pSpriteNode != null);
            SpriteBatchMan.RemoveBatch(pSpriteNode);

            //remove from game obj man
            ObjectNodeMan.RemoveObject(this);

            //add to ghost man to recycle obj
            GhostMan.AttachObj(this);
        }


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

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

        public override void Ressurect()
        {
            this.isDead = false;
            this.pProxy = SpriteProxyMan.AddProxy(this.spriteName);

            this.pColObj = new CollisionObject(this.pProxy);
            Debug.Assert(this.pColObj != null);

            base.Ressurect();
        }

        public override void PrintComponent()
        {
            Debug.WriteLine("\nObject Name: {0} ({1})", this.name, this.GetHashCode());

            if (this.pProxy != null)
            {
                Debug.WriteLine("pProxySprite: {0}", this.pProxy.name);
                if(this.pProxy.GetSprite() == null)
                {
                    Debug.WriteLine(" pRealSprite: null");
                }
                else
                {
                    Debug.WriteLine(" pRealSprite: {0}", this.pProxy.GetSprite().GetName());
                }
            }
            else
            {
                Debug.WriteLine("pProxySprite: null");
                Debug.WriteLine(" pRealSprite: null");
            }
            
            Debug.WriteLine("(x,y): {0}, {1}", this.x, this.y);
            Debug.WriteLine("Collision rect (sx,sy): {0}, {1}", this.pColObj.pCollisionRect.width, this.pColObj.pCollisionRect.height);

            base.PrintStats();
        }


    }
}