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


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

        private ObjectNode pCompare = new ObjectNode();
        private NullObject pNullObj;
        private static ObjectNodeMan pInstance = null;
        private static ObjectNodeMan pActiveMan = null;


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

        public ObjectNodeMan(int numReserve = 3, int growReserve = 1)
            : base(new DLinkMan(), new DLinkMan(), numReserve, growReserve)
        {
            this.pNullObj = new NullObject();       //LTN: owned by object node man
            this.pCompare.SetObject(this.pNullObj);

            ObjectNodeMan.pActiveMan = null;
        }

        public static void Create()
        {
            //LTN: owned by object node man
           if(pInstance == null)
            {
                pInstance = new ObjectNodeMan();
            }
        }

        public static void Destroy()
        {
            //TODO: invalidate singleton, get rid of objs,stats
        }

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

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

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

            ObjectNodeMan.pActiveMan = null;
        }

        public static ObjectNode AttachObject(GameObject pObject)
        {
            ObjectNodeMan pManager = ObjectNodeMan.pActiveMan;
            Debug.Assert(pManager != null);

            ObjectNode pNode = (ObjectNode)pManager.AddToFront();
            Debug.Assert(pNode != null);

            pNode.SetObject(pObject);
            return pNode;
        }

        public static GameObject FindObject(GameObject.ObjectName name)
        {
            ObjectNodeMan pManager = ObjectNodeMan.pActiveMan;
            Debug.Assert(pManager != null);

            //check object node --> game object
            Debug.Assert(pManager.pCompare.GetObject() != null);
            pManager.pCompare.GetObject().name = name;

            // obj node = found node 
            ObjectNode pNode = (ObjectNode)pManager.Find(pManager.pCompare);
            Debug.Assert(pNode != null);

            return pNode.GetObject();
        }

        public static void UpdateObject()
        {
            ObjectNodeMan pManager = ObjectNodeMan.pActiveMan;
            Debug.Assert(pManager != null);

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

            ObjectNode pNode = (ObjectNode)pIterator.First();

            //walk thro list & update ea obj
            while (!pIterator.IsDone())
            {
                ReverseIterator pReverse = new ReverseIterator(pNode.GetObject());
                Component pComponent = pReverse.First();
                while(!pReverse.IsDone())
                {
                    GameObject pObj = (GameObject)pComponent;
                    pObj.Update();

                    pComponent = pReverse.Next();
                }

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

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

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

            pManager.Remove(pNode);
        }

        public static void RemoveObject(GameObject pNode)
        {
            //sanity check
            Debug.Assert(pNode != null);
            ObjectNodeMan pManager = ObjectNodeMan.pActiveMan;

            //get root 
            GameObject ptemp = pNode;
            GameObject pRoot = null;
            while (ptemp != null)
            {
                pRoot = ptemp;
                ptemp = (GameObject)ForwardIterator.GetParent(ptemp);
            }

            //walk list looking for root
            Iterator pIterator = pManager.GetActiveIterator();
            ObjectNode pTree = (ObjectNode)pIterator.First();
            while(!pIterator.IsDone())
            {
                if(pTree.GetObject() == pRoot)
                {
                    break;
                }

                pTree = (ObjectNode)pIterator.Next();
            }

            //remove target node from tree
            Debug.Assert(pTree != null);
            Debug.Assert(pTree.GetObject() != null);
            Debug.Assert(pTree.GetObject() != pNode);   

            GameObject pParent = (GameObject)ForwardIterator.GetParent(pNode);
            Debug.Assert(pParent != null);
            
            //make sure no child before delete (no dangling kids)
            GameObject pChild = (GameObject)ForwardIterator.GetChild(pNode);
            Debug.Assert(pChild == null);

            //remove & update
            pParent.RemoveComponent(pNode);
            pParent.Update();
        }

        public static void PrintObject()
        {
            ObjectNodeMan pManager = ObjectNodeMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.PrintStats();
        }

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

            return pInstance;
        }
        //-------------------------------------------------------------------
        //  OVERRIDDEN METHODS
        //-------------------------------------------------------------------

        protected override Node derivedCreate()
        {
            //LTN: own by object node manager
            Node pNode = new ObjectNode();
            Debug.Assert(pNode != null);

            return pNode;
        }
    }
}