GAM456-SpaceInvaders / SpaceInvaders / 3_Timer / TimerEventMan.cs
TimerEventMan.cs
Raw
using System;
using System.Diagnostics;


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

        bool isPaused;
        float currTime;
        float pausedTime;

        private TimerEvent poCompare;
        private static TimerEventMan poInstance = null;
        private static TimerEventMan pActiveMan = null;
        

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

        public TimerEventMan(int numReserve = 3, int growReserve = 2) 
            : base(new DLinkMan(), new DLinkMan(), numReserve, growReserve)
        {
            this.poCompare = new TimerEvent();
            Debug.Assert(this.poCompare != null);

            TimerEventMan.pActiveMan = null;
            this.currTime = 0.0f;

            this.isPaused = false;
            this.pausedTime = 0.0f;
        }


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

        public static void Create()
        {
            //LTN: own by timer event manager
            if(TimerEventMan.poInstance == null)
            {
                TimerEventMan.poInstance = new TimerEventMan();
                Debug.Assert(TimerEventMan.poInstance != null);
            }
        }

        public static void Destroy()
        {
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

            //TODO: do something here
        }

        public static void SetActiveMan(TimerEventMan pEventMan)
        {
            TimerEventMan pManager = TimerEventMan.GetInstance();
            Debug.Assert(pManager != null);
            //Debug.WriteLine("\nTimer pInstance: {0}", pManager.GetHashCode());

            Debug.Assert(pEventMan != null);
            TimerEventMan.pActiveMan = pEventMan;
            //Debug.WriteLine("Timer pActiveMan: {0}", pActiveMan.GetHashCode());
        }

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

            TimerEventMan.pActiveMan = null;
        }

        public static TimerEvent AddEvent(TimerEvent.EventName name, Command pCommand, float deltaTrigger)
        {
            //check command is valid
            Debug.Assert(pCommand != null);
            Debug.Assert(deltaTrigger >= 0.0f);

            //get instance
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

            //get node from reserve pool
            TimerEvent pEvent = (TimerEvent)pManager.GetReserve();  
            Debug.Assert(pEvent != null);

            //set event & add by priority
            pEvent.SetEvent(name, pCommand, deltaTrigger);
            pManager.AddByPriority(pEvent);

            return pEvent;
        }

        public static TimerEvent FindEvent(TimerEvent.EventName name)
        {
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.poCompare.name = name;
            TimerEvent pEvent = (TimerEvent)pManager.Find(pManager.poCompare);

            return pEvent;
        }

        public static void RemoveEvent(TimerEvent pNode)
        {
            //check img ref
            Debug.Assert(pNode != null);

            //get instance
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.Remove(pNode);
        }

        public static void PauseAllEvents()
        {
            //get curr active instance
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.isPaused = true;
            pManager.pausedTime = pManager.currTime;
        }

        public static void UnpauseEvents()
        {
            //get curr active instance
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

            //update delta
            pManager.isPaused = false;
            float delta = Simulation.GetTotalTime() - pManager.pausedTime;

            //walk list & update time
            Iterator pIterator = pManager.GetActiveIterator();
            Debug.Assert(pIterator != null);

            TimerEvent pEvent = (TimerEvent)pIterator.First();
            while(!pIterator.IsDone())
            {
                pEvent.UpdateTriggerTime(delta);
                pEvent = (TimerEvent)pIterator.Next();
            }
        }

        public static void ProcessEvent(float totalTime)
        {
            //get instance
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

            if(pManager.isPaused)
            {
                return;
            }

            pManager.currTime = totalTime;

            //walk thro list & execute
            Iterator pIterator = pManager.GetActiveIterator();
            Debug.Assert(pIterator != null);

            TimerEvent pEvent = (TimerEvent)pIterator.First();
            TimerEvent pNextEvent;
            while(!pIterator.IsDone())
            {
                pNextEvent = (TimerEvent)pIterator.Next();
                
                if(pManager.currTime >= pEvent.GetTriggerTime())
                {
                    pEvent.ProcessEvent();      //execute command
                    pManager.Remove(pEvent);    //remove from list
                }

                pEvent = pNextEvent;
            }
        }

        public static void DetachEvents()
        {
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

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

            TimerEvent pEvent = (TimerEvent)pIterator.First();
            TimerEvent pNextEvent;
            while(!pIterator.IsDone())
            {
                pNextEvent = (TimerEvent)pIterator.Next();

                pManager.Remove(pEvent);

                pEvent = pNextEvent;
            }
        }

        public static float GetCurrTime()
        {
            //get instance
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            
            //upon start up
            if(pManager == null)
            {
                pManager = TimerEventMan.poInstance;
            }

            //return time
            return pManager.currTime;
        }

        public static void PrintEvent()
        {
            TimerEventMan pManager = TimerEventMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.PrintStats();
        }


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

        protected override Node derivedCreate()
        {
            //LTN: own by timer even manager
            Node pNode = new TimerEvent();
            Debug.Assert(pNode != null);

            return pNode;
        }


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

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

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


    } //end class

} //end namespace