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


namespace SpaceInvaders
{
    class ShieldFactory
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private Composite pTree;
        private SpriteBatch pBatch;
        private SpriteBatch pCollisionBatch;

        private static ShieldFactory pInstance = null;


        //-------------------------------------------------------------------
        //  PRIVATE CONSTRUCTION
        //-------------------------------------------------------------------

        private ShieldFactory()
        {
            //default
            this.pBatch = null;
            this.pCollisionBatch = null;
            this.pTree = null;
        }


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

        public static GameObject CreateShield(float x, float y)
        {
            ShieldFactory pFactory = ShieldFactory.GetInstance();
            ShieldRoot pRoot;
            
            ObjectNode pObjNode = GhostMan.FindObj(GameObject.ObjectName.ShieldRoot);
            //if node = null, create new root
            if(pObjNode == null)
            {
                pRoot = new ShieldRoot(GameObject.ObjectName.ShieldRoot, Sprite.SpriteName.NullObject, 0.0f, 0.0f);
            }
            //else, grab from ghost man
            else
            {
                pRoot = (ShieldRoot)pObjNode.GetObject();
                GhostMan.RemoveObj(pObjNode);
                pRoot.Resurrect(0.0f, 0.0f);
            }

            ObjectNodeMan.AttachObject(pRoot);
            pFactory.SetShield(SpriteBatch.BatchName.Shields, SpriteBatch.BatchName.Boxes, pRoot);

            int i = 0;
            GameObject pColumn;

            //Col 1
            float startx = x;
            float starty = y;
            float offx = 0.0f;
            float brickWidth = 12.0f;
            float brickHeight = 6.0f;

            pColumn = pFactory.CreateBricks(pRoot, Shields.ShieldType.Column, GameObject.ObjectName.ShieldColumn_0 + i);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx, starty);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx, starty + brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx, starty + 2 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx, starty + 3 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx, starty + 4 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx, starty + 5 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx, starty + 6 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx, starty + 7 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Left_Top1, GameObject.ObjectName.ShieldBrick_LeftTop1, startx, starty + 8 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Left_Top0, GameObject.ObjectName.ShieldBrick_LeftTop0, startx, starty + 9 * brickHeight);

            //Col 2
            pColumn = pFactory.CreateBricks(pRoot,Shields.ShieldType.Column, GameObject.ObjectName.ShieldColumn_1);
            offx += brickWidth;
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 2 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 3 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 4 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 5 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 6 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 7 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 8 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 9 * brickHeight);

            //Col 3
            pColumn = pFactory.CreateBricks(pRoot, Shields.ShieldType.Column, GameObject.ObjectName.ShieldColumn_2);
            offx += brickWidth;
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Left_Bottom, GameObject.ObjectName.ShieldBrick_LeftBottom, startx + offx, starty + 2 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 3 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 4 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 5 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 6 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 7 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 8 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 9 * brickHeight);

            //Col 4
            pColumn = pFactory.CreateBricks(pRoot, Shields.ShieldType.Column, GameObject.ObjectName.ShieldColumn_3);
            offx += brickWidth;
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 3 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 4 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 5 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 6 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 7 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 8 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 9 * brickHeight);
                                  
            //Col 5
            pColumn = pFactory.CreateBricks(pRoot, Shields.ShieldType.Column, GameObject.ObjectName.ShieldColumn_4);
            offx += brickWidth;
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Right_Bottom, GameObject.ObjectName.ShieldBrick_RightBottom, startx + offx, starty + 2 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 3 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 4 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 5 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 6 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 7 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 8 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 9 * brickHeight);

            //col 6
            pColumn = pFactory.CreateBricks(pRoot, Shields.ShieldType.Column, GameObject.ObjectName.ShieldColumn_5);
            offx += brickWidth;
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 0 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 1 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 2 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 3 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 4 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 5 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 6 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 7 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 8 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 9 * brickHeight);

            //Col 7
            pColumn = pFactory.CreateBricks(pRoot, Shields.ShieldType.Column, GameObject.ObjectName.ShieldColumn_6);
            offx += brickWidth;
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 0 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 1 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 2 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 3 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 4 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 5 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 6 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Brick, GameObject.ObjectName.ShieldBrick, startx + offx, starty + 7 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Right_Top1, GameObject.ObjectName.ShieldBrick_RightTop1, startx + offx, starty + 8 * brickHeight);
            pFactory.CreateBricks(pColumn, Shields.ShieldType.Right_Top0, GameObject.ObjectName.ShieldBrick_RightTop0, startx + offx, starty + 9 * brickHeight);
                                  
            return pRoot;
        }

        public static void GhostShields(GameObject pShieldGroup)
        {   
            // Iterate through shields
            ShieldRoot pShieldRoot = (ShieldRoot)ForwardIterator.GetChild(pShieldGroup);
            while(pShieldRoot != null)
            {
                // Iterate through columns
                ShieldColumn pColumn = (ShieldColumn)ForwardIterator.GetChild(pShieldRoot);
                while(pColumn != null)
                {
                    // Iterate through bricks
                    ShieldBrick pBrick = (ShieldBrick)ForwardIterator.GetChild(pColumn);
                    while(pBrick != null)
                    {
                        // Remove brick 
                        pBrick.Remove();

                        pBrick = (ShieldBrick)ForwardIterator.GetChild(pColumn);
                    }

                    pColumn.Remove();

                    pColumn = (ShieldColumn)ForwardIterator.GetChild(pShieldRoot);
                }

                pShieldRoot.Remove();
                pShieldRoot = (ShieldRoot)ForwardIterator.GetChild(pShieldGroup);
            }
        }


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

        private static ShieldFactory GetInstance()
        {
            if (pInstance == null)
            {
                ShieldFactory.pInstance = new ShieldFactory();
            }

            return pInstance;
        }

        private void SetParent(GameObject pParent)
        {
            Debug.Assert(pParent != null);
            this.pTree = (Composite)pParent;
        }

        private void SetShield(SpriteBatch.BatchName spriteName, SpriteBatch.BatchName colName, Composite pTree)
        {
            //find correct batch, collision box, and set composite tree
            this.pBatch = SpriteBatchMan.FindBatch(spriteName);
            Debug.Assert(this.pBatch != null);

            this.pCollisionBatch = SpriteBatchMan.FindBatch(colName);
            Debug.Assert(this.pCollisionBatch != null);

            Debug.Assert(pTree != null);
            this.pTree = pTree;
        }

        private GameObject CreateBricks(GameObject pRootObj, Shields.ShieldType type, GameObject.ObjectName objName, float x = 0.0f, float y = 0.0f)
        {
            GameObject pShield = null;

            //check if ghost man has obj
            ObjectNode pObjNode = GhostMan.FindObj(objName);
            if (pObjNode != null)
            {
                pShield = pObjNode.GetObject();
                GhostMan.RemoveObj(pObjNode);

                switch (type)
                {
                    case Shields.ShieldType.Brick:
                        ((ShieldBrick)pShield).Resurrect(x, y);
                        break;
                    case Shields.ShieldType.Left_Bottom:
                        ((ShieldBrick)pShield).Resurrect(x, y);
                        break;
                    case Shields.ShieldType.Right_Bottom:
                        ((ShieldBrick)pShield).Resurrect(x, y);
                        break;
                    case Shields.ShieldType.Left_Top1:
                        ((ShieldBrick)pShield).Resurrect(x, y);
                        break;
                    case Shields.ShieldType.Left_Top0:
                        ((ShieldBrick)pShield).Resurrect(x, y);
                        break;
                    case Shields.ShieldType.Right_Top1:
                        ((ShieldBrick)pShield).Resurrect(x, y);
                        break;
                    case Shields.ShieldType.Right_Top0:
                        ((ShieldBrick)pShield).Resurrect(x, y);
                        break;
                    case Shields.ShieldType.Column:
                        ((ShieldColumn)pShield).Resurrect(x, y);
                        break;
                    default:
                        Debug.Assert(false);    //scream at me
                        break;
                }
            }
            //else create new one
            else
            {
                switch (type)
                {
                    case Shields.ShieldType.Brick:
                        pShield = new ShieldBrick(objName, Sprite.SpriteName.Brick, x, y);
                        break;
                    case Shields.ShieldType.Left_Top0:
                        pShield = new ShieldBrick(objName, Sprite.SpriteName.BrickLeft_Top0, x, y);
                        break;
                    case Shields.ShieldType.Left_Top1:
                        pShield = new ShieldBrick(objName, Sprite.SpriteName.BrickLeft_Top1, x, y);
                        break;
                    case Shields.ShieldType.Left_Bottom:
                        pShield = new ShieldBrick(objName, Sprite.SpriteName.BrickLeft_Bottom, x, y);
                        break;
                    case Shields.ShieldType.Right_Top0:
                        pShield = new ShieldBrick(objName, Sprite.SpriteName.BrickRight_Top0, x, y);
                        break;
                    case Shields.ShieldType.Right_Top1:
                        pShield = new ShieldBrick(objName, Sprite.SpriteName.BrickRight_Top1, x, y);
                        break;
                    case Shields.ShieldType.Right_Bottom:
                        pShield = new ShieldBrick(objName, Sprite.SpriteName.BrickRight_Bottom, x, y);
                        break;
                    case Shields.ShieldType.Column:
                        pShield = new ShieldColumn(objName, Sprite.SpriteName.NullObject, x, y);
                        break;
                    default:
                        Debug.Assert(false); //scream at me
                        break;
                }
            }

            //add to tree
            pRootObj.AddComponent(pShield);

            //attach to group
            pShield.ActivateSprite(this.pBatch);
            pShield.ActivateCollisionSprite(this.pCollisionBatch);

            return pShield;
        }


    } //end class

} //end namespace