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


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

        private SLinkMan pSLinkMan;
        private static DelayObjectMan pInstance = null;


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

        private DelayObjectMan()
        {
            this.pSLinkMan = new SLinkMan();
            Debug.Assert(this.pSLinkMan != null);
        }


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

        public static void AttachDelay(CollisionObserver pObserver)
        {
            Debug.Assert(pObserver != null);
            
            DelayObjectMan pManager = DelayObjectMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.pSLinkMan.AddToFront(pObserver);
        }

        public static void ProcessDelay()
        {
            DelayObjectMan pManager = DelayObjectMan.GetInstance();
            Iterator pIterator = pManager.pSLinkMan.GetIterator();
            CollisionObserver pNode = (CollisionObserver)pIterator.First();

            //fire off each listener
            while(!pIterator.IsDone())
            {
                pNode.Execute();
                pNode = (CollisionObserver)pIterator.Next();
            }

            //now remove
            CollisionObserver pTemp = null;
            pIterator = pManager.pSLinkMan.GetIterator();
            pNode = (CollisionObserver)pIterator.First();

            while(!pIterator.IsDone())
            {
                pTemp = pNode;
                pNode = (CollisionObserver)pIterator.Next();

                pManager.pSLinkMan.Remove(pTemp);
            }
        }


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

        private static DelayObjectMan GetInstance()
        {
            //initialize here
            if (DelayObjectMan.pInstance == null)
            {
                DelayObjectMan.pInstance = new DelayObjectMan();
                Debug.Assert(DelayObjectMan.pInstance != null);
            }

            return pInstance;
        }

    }
}