GAM456-SpaceInvaders / SpaceInvaders / 1_Sprite / SpriteBatch / SpriteNodeMan.cs
SpriteNodeMan.cs
Raw
using System;
using System.Diagnostics;


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

        private readonly SpriteNode poCompare;
        private SpriteBatch.BatchName name;

        private SpriteBatch pBackSpriteBatch;    //back pointer


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

        public SpriteNodeMan(int initialReserve = 3, int growReserve = 2) 
            : base(new DLinkMan(), new DLinkMan(), initialReserve, growReserve)
        {
            this.pBackSpriteBatch = null;

            //LTN: owned by sprite node manager
            this.poCompare = new SpriteNode();
            Debug.Assert(this.poCompare != null);
        }


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

        public void SetSpriteNode(SpriteBatch.BatchName name, int numReserve, int numGrow)
        {
            this.name = name;
            this.SetReserve(numReserve, numGrow);   //sets up reserve
        }

        public SpriteBatch GetSpriteBatch()
        {
            return this.pBackSpriteBatch;
        }

        public void SetSpriteBatch(SpriteBatch pBatch)
        {
            Debug.Assert(pBatch != null);
            this.pBackSpriteBatch = pBatch;
        }

        public SpriteNode AttachSpriteNode(SpriteBase pSpriteBase)
        {
            Debug.Assert(pSpriteBase != null);

            SpriteNode pNode = (SpriteNode)this.AddToFront();
            Debug.Assert(pNode != null);

            pNode.SetSpriteNode(pSpriteBase, this);

            return pNode;
        }

        public void DrawSpriteNode()
        {
            Iterator pIterator = this.GetActiveIterator();
            Debug.Assert(pIterator != null);

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

            //iterate & draw 
            while(!pIterator.IsDone())
            {
                pNode.GetSprite().Render();
                pNode = (SpriteNode)pIterator.Next();
            }
        }

        public void RemoveSpriteNode(SpriteNode pSpriteNode)
        {
            Debug.Assert(pSpriteNode != null);
            this.Remove(pSpriteNode);
        }

        public void PrintSpriteNode()
        {
            this.PrintStats();
        }


        //-------------------------------------------------------------------
        //  OVERRIDDEN METHODS
        //-------------------------------------------------------------------

        protected override Node derivedCreate()
        {
            //LTN: owned by Sprite Batch Man
            Node pNode = new SpriteNode();
            Debug.Assert(pNode != null);

            return pNode;
        }

    }
}