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


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

        private readonly ObjectNode pCompare;
        private readonly NullObject pNullObj;
        private static GhostMan pInstance = null;
        private static GhostMan pActiveMan = null;


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

        public GhostMan(int numReserve = 3, int numGrow = 1)
            : base(new DLinkMan(), new DLinkMan(), numReserve, numGrow)
        {
            this.pCompare = new ObjectNode();
            this.pNullObj = new NullObject();
            this.pCompare.SetObject(this.pNullObj);

            GhostMan.pActiveMan = null;
        }


        //-------------------------------------------------------------------
        //  PUBLIC METHOD
        //-------------------------------------------------------------------

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

        public static void Destroy()
        { }

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

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

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

            GhostMan.pActiveMan = null;
        }

        public static ObjectNode AttachObj(GameObject pObject)
        {
            GhostMan pManager = GhostMan.pActiveMan;
            ObjectNode pNode = (ObjectNode)pManager.AddToFront();

            pNode.SetObject(pObject);
            return pNode;
        }

        public static ObjectNode FindObj(GameObject.ObjectName objName)
        {
            GhostMan pManager = GhostMan.pActiveMan;
            Debug.Assert(pManager.pCompare.GetObject() != null);

            pManager.pCompare.GetObject().name = objName;
            
            ObjectNode pNode = (ObjectNode)pManager.Find(pManager.pCompare);

            return pNode;
        }

        public static void RemoveObj(ObjectNode pNode)
        {
            Debug.Assert(pNode != null);

            GhostMan pManager = GhostMan.pActiveMan;
            pManager.Remove(pNode);
        }

  
        //-------------------------------------------------------------------
        //  PROTECTED METHOD
        //-------------------------------------------------------------------

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

            return pNode;
        }

        //-------------------------------------------------------------------
        //  PROTECTED METHOD
        //-------------------------------------------------------------------

        private static GhostMan GetInstance()
        {
            if (pInstance == null)
            {
                GhostMan.Create();
            }

            return pInstance;
        }


    }
}