GAM456-SpaceInvaders / SpaceInvaders / 6_Collision / CollisionPairMan.cs
CollisionPairMan.cs
Raw
using System;
using System.Diagnostics;


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

        private static readonly CollisionPair poCompare = new CollisionPair();
        private static CollisionPairMan poInstance = null;
        private static CollisionPairMan pActiveMan = null;

        private CollisionPair pActivePair;


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

        public CollisionPairMan(int numReserve = 3, int numGrow = 1)
            : base(new DLinkMan(), new DLinkMan(), numReserve, numGrow)
        {
            this.pActivePair = null;
            CollisionPairMan.pActiveMan = null;
        }


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

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

        public static void Destroy()
        { }

        public static void SetActiveMan(CollisionPairMan pColMan)
        {
            CollisionPairMan pManager = CollisionPairMan.GetInstance();
            Debug.Assert(pManager != null);
            //Debug.WriteLine("Col Man pInstance: {0}", pManager.GetHashCode());

            Debug.Assert(pColMan != null);
            CollisionPairMan.pActiveMan = pColMan;
            //Debug.WriteLine("Col pActive: {0}", pActiveMan.GetHashCode());
        }

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

            CollisionPairMan.pActiveMan = null;
        }

        public static CollisionPair AddPair(CollisionPair.CollideObjs name, GameObject rootA, GameObject rootB)
        {
            CollisionPairMan pManager = CollisionPairMan.pActiveMan;
            Debug.Assert(pManager != null);

            //reserve node --> active 
            CollisionPair pPair = (CollisionPair)pManager.AddToFront();
            Debug.Assert(pPair != null);

            //initialize pair
            pPair.SetPair(name, rootA, rootB);

            return pPair;
        }

        public static void ProcessCollision()
        {
            CollisionPairMan pManager = CollisionPairMan.pActiveMan;
            Debug.Assert(pManager != null);

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

            CollisionPair pNode = (CollisionPair)pIterator.First();
            while(!pIterator.IsDone())
            {
                pManager.pActivePair = pNode;

                //process each pair
                Debug.Assert(pNode != null);
                pNode.ProcessPair();

                pNode = (CollisionPair)pIterator.Next();
            }
        }

        public static CollisionPair GetActivePair()
        {
            CollisionPairMan pManager = CollisionPairMan.pActiveMan;
            return pManager.pActivePair;
        }

        public static CollisionPair FindPair(CollisionPair.CollideObjs name)
        {
            CollisionPairMan pManager = CollisionPairMan.pActiveMan;
            Debug.Assert(pManager != null);

            CollisionPairMan.poCompare.name = name;
            CollisionPair pPair = (CollisionPair)pManager.Find(CollisionPairMan.poCompare);

            return pPair;
        }

        public static void RemovePair(CollisionPair pNode)
        {
            Debug.Assert(pNode != null);

            CollisionPairMan pManager = CollisionPairMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.Remove(pNode);
        }

        public static void PrintCollision()
        {
            CollisionPairMan pManager = CollisionPairMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.PrintStats();
        }


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

        protected override Node derivedCreate()
        {
            Node pNode = new CollisionPair();
            Debug.Assert(pNode != null);

            return pNode;
        }


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

        private static CollisionPairMan GetInstance()
        {
            //Debug.Assert(pInstance != null);
            if (CollisionPairMan.poInstance == null)
            {
                CollisionPairMan.Create();
                Debug.Assert(CollisionPairMan.poInstance != null);
            }

            return CollisionPairMan.poInstance;
        }


    } // end class

} // end namespace