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


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

        private Sprite poCompare;
        private static SpriteMan poInstance = null;


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

        private SpriteMan(int initialReserve = 3, int growReserve = 2) 
            : base(new DLinkMan(), new DLinkMan(), initialReserve, growReserve)
        {
            //LTN: owned by sprite manager
            this.poCompare = new Sprite();
            Debug.Assert(this.poCompare != null);
        }


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

        public static void Create(int initialReserve = 3, int growReserve = 2)
        {
            //LTN: instance owned by sprite manager
            if(SpriteMan.poInstance == null)
            {
                SpriteMan.poInstance = new SpriteMan(initialReserve, growReserve);
                Debug.Assert(SpriteMan.poInstance != null);
            }

            //null sprite
            SpriteMan.AddSprite(Sprite.SpriteName.NullObject, Image.ImageName.HotPink, 0.0f, 0.0f, 0.0f, 0.0f);
        }

        public static void Destroy()
        {
            SpriteMan pManager = SpriteMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.poCompare = null;
            SpriteMan.poInstance = null;

            //TODO: stats...etc?
        }

        public static Sprite AddSprite(Sprite.SpriteName name, Image.ImageName imageName, float x, float y, 
            float sx, float sy, Azul.Color pColor = null)
        {
            SpriteMan pManager = SpriteMan.GetInstance();
            Debug.Assert(pManager != null);

            Image pImg = ImageMan.FindImage(imageName);
            Debug.Assert(pImg != null);

            Sprite pSprite = (Sprite)pManager.AddToFront();
            Debug.Assert(pSprite != null);

            pSprite.SetSprite(name, pImg, x, y, sx, sy, pColor);
            return pSprite;
        }

        public static void RemoveSprite(Sprite pSprite)
        {
            Debug.Assert(pSprite != null);

            SpriteMan pManager = SpriteMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.Remove(pSprite);
        }

        public static Sprite FindSprite(Sprite.SpriteName name)
        {
            SpriteMan pManager = SpriteMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.poCompare.name = name;
            Sprite pSprite = (Sprite)pManager.Find(pManager.poCompare);

            return pSprite;
        }

        public static void PrintSprite()
        {
            SpriteMan pManager = SpriteMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.PrintStats();
        }


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

        protected override Node derivedCreate()
        {
            //LTN: owned by sprite node manager
            Node pNode = new Sprite();
            Debug.Assert(pNode != null);

            return pNode;
        }


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

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

    } //end class

} //end namespace