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

namespace SpaceInvaders
{
    public class SpriteBatchMan : Manager
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private readonly SpriteBatch poCompare;

        private static SpriteBatchMan poInstance = null;
        private static SpriteBatchMan pActiveMan = null;


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

        public SpriteBatchMan(int initialReserve = 3, int growReserve = 2) 
            : base(new DLinkMan(), new DLinkMan(), initialReserve, growReserve)
        {
            //LTN: owned by Batch Manager, used for all sprite batch comparisons
            this.poCompare = new SpriteBatch();
            Debug.Assert(this.poCompare != null);

            SpriteBatchMan.pActiveMan = null;
        }


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

        public static void Create()
        {
            if(SpriteBatchMan.poInstance == null)
            {
                SpriteBatchMan.poInstance = new SpriteBatchMan();
                Debug.Assert(SpriteBatchMan.poInstance != null);
            }
        }

        public static void SetActiveMan(SpriteBatchMan pBatchMan)
        {
            //check incoming
            Debug.Assert(pBatchMan != null);

            SpriteBatchMan pManager = SpriteBatchMan.GetInstance();
            Debug.Assert(pManager != null);
            //Debug.WriteLine("\nBatch pInstance: {0}", pManager.GetHashCode());

            SpriteBatchMan.pActiveMan = pBatchMan;
            //Debug.WriteLine("Batch Active: {0}", pActiveMan.GetHashCode());
        }

        public static void UnactivateMan()
        {
            SpriteBatchMan pManager = SpriteBatchMan.GetInstance();
            Debug.Assert(pManager != null);

            SpriteBatchMan.pActiveMan = null;
        }

        public static SpriteBatch AddBatch(SpriteBatch.BatchName name, int priority, int numReserve = 3, int numGrow = 2)
        {
            SpriteBatchMan pManager = SpriteBatchMan.pActiveMan;
            Debug.Assert(pManager != null);

            SpriteBatch pBatch = (SpriteBatch)pManager.GetReserve(); //get reserve node
            Debug.Assert(pBatch != null);

            pBatch.SetBatch(name, priority, numReserve, numGrow);   //set batch
            pManager.AddByPriority(pBatch);                         //add to active by priority

            return pBatch;
        }

        public static void RemoveBatch(SpriteBatch pBatch)
        {
            SpriteBatchMan pManager = SpriteBatchMan.pActiveMan;
            Debug.Assert(pManager != null);

            Debug.Assert(pBatch != null);

            pManager.Remove(pBatch);
        }

        public static void RemoveBatch(SpriteNode pBatchNode)
        {
            Debug.Assert(pBatchNode != null);
            SpriteNodeMan pNodeManager = pBatchNode.GetNodeMan();

            Debug.Assert(pNodeManager != null);
            pNodeManager.RemoveSpriteNode(pBatchNode);
        }

        public static void DrawBatch()
        {
            //get instance
            SpriteBatchMan pManager = SpriteBatchMan.pActiveMan;
            Debug.Assert(pManager != null);
            //Debug.WriteLine("pMan-->Active Draw: {0}", pManager.GetHashCode());

            //render list
            Iterator pIterator = pManager.GetActiveIterator();
            Debug.Assert(pIterator != null);

            SpriteBatch pBatch = (SpriteBatch)pIterator.First();
            while(!pIterator.IsDone())
            {
                if(pBatch.GetToggle())
                {
                    //get ref
                    SpriteNodeMan pNodeMan = pBatch.GetSpriteNodeMan();
                    Debug.Assert(pNodeMan != null);

                    //sprite node man draws
                    pNodeMan.DrawSpriteNode();
                }
              
                //get next on list
                pBatch = (SpriteBatch)pIterator.Next();
            }
        }

        public static SpriteBatch FindBatch(SpriteBatch.BatchName name)
        {
            SpriteBatchMan pManager = SpriteBatchMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.poCompare.name = name;
            SpriteBatch pBatch = (SpriteBatch)pManager.Find(pManager.poCompare);

            return pBatch;
        }

        public static void PrintBatch()
        {
            SpriteBatchMan pManager = SpriteBatchMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.PrintStats();
        }


        //-------------------------------------------------------------------
        //  PROTECTED METHODS
        //-------------------------------------------------------------------

        protected override Node derivedCreate()
        {
            //LTN: Owned by Batch Manager
            Node pNode = new SpriteBatch();
            Debug.Assert(pNode != null);

            return pNode;
        }


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

        private static SpriteBatchMan GetInstance()
        {
            if (SpriteBatchMan.poInstance == null)
            {
                SpriteBatchMan.Create();
            }

            Debug.Assert(SpriteBatchMan.poInstance != null);
            return SpriteBatchMan.poInstance;
        }


    } //end class

} //end namespace