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


namespace SpaceInvaders
{
    public class AlienFactory
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private SpriteBatch pSpriteBatch;
        private SpriteBatch pCollisionBatch;
        private Composite pTree;

        private static AlienFactory poInstance = null;


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

        private AlienFactory()
        {
            this.pSpriteBatch = null;
            this.pCollisionBatch = null;
            this.pTree = null;
        }


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

        public static GameObject CreateGrid(float x, float y)
        {
            AlienFactory pFactory = AlienFactory.GetInstance();
            AlienGrid pGrid;
            if (pFactory.pTree == null)
            {
                pGrid = new AlienGrid(GameObject.ObjectName.AlienGrid, Sprite.SpriteName.NullObject, 0.0f, 0.0f);
                ObjectNodeMan.AttachObject(pGrid);
                pFactory.SetAliens(SpriteBatch.BatchName.Aliens, SpriteBatch.BatchName.Boxes, pGrid);
            }
            else
            {
                pGrid = (AlienGrid)ObjectNodeMan.FindObject(GameObject.ObjectName.AlienGrid);
            }

            pFactory.SetParent(pGrid);
            pGrid.ActivateSprite(pFactory.pSpriteBatch);
            pGrid.ActivateCollisionSprite(pFactory.pCollisionBatch);

            // Create Grid Columns
            GameObject pCol;

            //iterate and create each column
            for (int i = 0; i < 11; i++)
            {
                ObjectNode pObjNode = GhostMan.FindObj(GameObject.ObjectName.AlienColumn_0 + i);
                if(pObjNode ==  null)
                {
                    pCol = new AlienColumn(GameObject.ObjectName.AlienColumn_0 + i, Sprite.SpriteName.NullObject, 0.0f, 0.0f);
                }
                else
                {
                    pCol = (AlienColumn)pObjNode.GetObject();
                    GhostMan.RemoveObj(pObjNode);
                    pCol.Ressurect();
                }

                CreateColumn(pCol, x + i * 66.0f, y);
                pGrid.AddComponent(pCol);
            }

            return pGrid;
        }

        public static void GhostGrid(GameObject pGrid)
        {
            //ghost cols
            AlienColumn pCol = (AlienColumn)ForwardIterator.GetChild(pGrid);
            while (pCol != null)
            {
                //ghost aliens
                Aliens pAlien = (Aliens)ForwardIterator.GetChild(pCol);
                while (pAlien != null)
                {
                    pAlien.Remove();
                    pAlien = (Aliens)ForwardIterator.GetChild(pCol);    //get next child
                }

                pCol.Remove();
                pCol = (AlienColumn)ForwardIterator.GetChild(pGrid);
            }

        }

        public static void GhostUFOs(GameObject pUFORoot)
        {
            UFOAlien pAliens = (UFOAlien)ForwardIterator.GetChild(pUFORoot);
            while(pAliens != null)
            {
                // ghost them
                pAliens.Remove();
                pAliens = (UFOAlien)ForwardIterator.GetChild(pUFORoot);
            }
        }

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

        private static AlienFactory GetInstance()
        {
            if (AlienFactory.poInstance == null)
            {
                AlienFactory.poInstance = new AlienFactory();
                Debug.Assert(AlienFactory.poInstance != null);
            }

            return AlienFactory.poInstance;
        }

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

        private static void CreateColumn(GameObject pCol, float x, float y)
        {
            //helper: create column and attach
            AlienFactory pFactory = AlienFactory.GetInstance();
            pFactory.SetParent(pCol);
            pCol.ActivateSprite(pFactory.pSpriteBatch);
            pCol.ActivateCollisionSprite(pFactory.pCollisionBatch);

            pFactory.CreateAliens(Aliens.AlienType.Squid, GameObject.ObjectName.Squid, x, y);
            pFactory.CreateAliens(Aliens.AlienType.Crab, GameObject.ObjectName.Crab, x, y - 66.0f);
            pFactory.CreateAliens(Aliens.AlienType.Crab, GameObject.ObjectName.Crab, x, y - 132.0f);
            pFactory.CreateAliens(Aliens.AlienType.Octopus, GameObject.ObjectName.Octopus, x, y - 198.0f);
            pFactory.CreateAliens(Aliens.AlienType.Octopus, GameObject.ObjectName.Octopus, x, y - 264.0f);
        }

        private GameObject CreateAliens(Aliens.AlienType type, GameObject.ObjectName name, float x = 0.0f, float y = 0.0f)
        {

            GameObject pAliens = null;

            ObjectNode pObjNode = GhostMan.FindObj(name);
            if (pObjNode != null)
            {
                pAliens = pObjNode.GetObject();
                GhostMan.RemoveObj(pObjNode);

                switch (type)
                {
                    case Aliens.AlienType.Squid:
                        ((SquidAlien)pAliens).Resurrect(x, y);
                        break;
                    case Aliens.AlienType.Crab:
                        ((CrabAlien)pAliens).Resurrect(x, y);
                        break;
                    case Aliens.AlienType.Octopus:
                        ((OctopusAlien)pAliens).Resurrect(x, y);
                        break;
                    default:
                        Debug.Assert(false);
                        break;
                }
            }
            else
            {
                switch (type)
                {
                    case Aliens.AlienType.Squid:
                        pAliens = new SquidAlien(Sprite.SpriteName.Squid, x, y);
                        break;
                    case Aliens.AlienType.Crab:
                        pAliens = new CrabAlien(Sprite.SpriteName.Crab, x, y);
                        break;
                    case Aliens.AlienType.Octopus:
                        pAliens = new OctopusAlien(Sprite.SpriteName.Octopus, x, y);
                        break;
                    default:
                        Debug.Assert(false);    //halt, something went wrong
                        break;
                }
            }

            //add to composite
            this.pTree.AddComponent(pAliens);

            //attach to batch
            pAliens.ActivateSprite(this.pSpriteBatch);
            pAliens.ActivateCollisionSprite(this.pCollisionBatch);

            return pAliens;
        }

        private void SetAliens(SpriteBatch.BatchName spriteName, SpriteBatch.BatchName collisionBatch, Composite pTree)
        {
            this.pSpriteBatch = SpriteBatchMan.FindBatch(spriteName);
            Debug.Assert(this.pSpriteBatch != null);

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

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


    } // end class

} // end namespace