GAM456-SpaceInvaders / SpaceInvaders / 1_Sprite / SpriteProxy / SpriteProxyMan.cs
SpriteProxyMan.cs
Raw
using System;
using System.Diagnostics;


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

        private readonly SpriteProxy poCompare;
        private static SpriteProxyMan poInstance = null;


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

        private SpriteProxyMan(int numReserve = 3, int growReserve = 1) 
            : base(new DLinkMan(), new DLinkMan(), numReserve, growReserve)
        {
            //LTN: owned by Sprite Proxy manager
            this.poCompare = new SpriteProxy();
            Debug.Assert(this.poCompare != null);

            this.poCompare.SetProxy(SpriteMan.FindSprite(Sprite.SpriteName.NullObject));
        }


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

        public static void Create(int numReserve = 3, int growReserve = 1)
        {
            //LTN: owned by sprite proxy manager
            if(SpriteProxyMan.poInstance == null)
            {
                SpriteProxyMan.poInstance = new SpriteProxyMan(numReserve, growReserve);
                Debug.Assert(SpriteProxyMan.poInstance != null);
            }

            SpriteProxy pProxy = SpriteProxyMan.AddProxy(Sprite.SpriteName.NullObject);
            Debug.Assert(pProxy != null);
        }

        public static void Destroy()
        {
            //TODO
        }

        public static SpriteProxy AddProxy(Sprite.SpriteName name)
        {
            //get instance
            SpriteProxyMan pManager = SpriteProxyMan.GetInstance();
            Debug.Assert(pManager != null);

            //add to list
            SpriteProxy pProxy = (SpriteProxy)pManager.AddToFront();
            Debug.Assert(pProxy != null);
            
            pProxy.SetProxy(name);
            return pProxy;
        }

        public static SpriteProxy FindProxy(Sprite.SpriteName name)
        {
            SpriteProxyMan pManager = SpriteProxyMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.poCompare.GetSprite().name = name;
            SpriteProxy pProxy = (SpriteProxy)pManager.Find(pManager.poCompare);

            return pProxy;
        }

        public static void RemoveProxy(SpriteProxy pNode)
        {
            //check incoming
            Debug.Assert(pNode != null);

            //get instance
            SpriteProxyMan pManager = SpriteProxyMan.GetInstance();
            Debug.Assert(pManager != null);

            //remove
            pManager.Remove(pNode);
        }


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

        protected override Node derivedCreate()
        {
            //LTN: owned by sprite proxy man, used by object node man?
            Node pNode = new SpriteProxy();
            Debug.Assert(pNode != null);

            return pNode;
        }


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

        private static SpriteProxyMan GetInstance()
        {
            Debug.Assert(SpriteProxyMan.poInstance != null);
            return SpriteProxyMan.poInstance;
        }


    } // end class

} //end namespace