GAM456-SpaceInvaders / SpaceInvaders / 3_Timer / Commands / AnimateSprite.cs
AnimateSprite.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class AnimateSprite : Command
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private Sprite pSprite;
        private Iterator pIterator;
        private SLinkMan poSLink;


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

        public AnimateSprite(Sprite.SpriteName name)
        {
            this.pSprite = SpriteMan.FindSprite(name);
            Debug.Assert(this.pSprite != null);

            //LTN: own by animate sprite?
            this.poSLink = new SLinkMan();
            Debug.Assert(this.poSLink != null);

            this.pIterator = this.poSLink.GetIterator();
            Debug.Assert(this.pIterator != null);
        }


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

        //attach image for animation
        public void Attach(Image.ImageName name)
        {
            //get image
            Image pImage = ImageMan.FindImage(name);
            Debug.Assert(pImage != null);

            //create holder
            ImageNode pImgHolder = new ImageNode(pImage);
            Debug.Assert(pImgHolder != null);

            //attach
            this.poSLink.AddToFront(pImgHolder);

            //update iterator
            this.pIterator = this.poSLink.GetIterator();
        }

        public override void Execute(float deltaTime)
        {
            ImageNode pImgNode = (ImageNode)this.pIterator.Peek();

            //if get to end, return to first
            if(pIterator.Next() == null)
            {
                pIterator.First();
            }

            //change image
            this.pSprite.ChangeImage(pImgNode.GetImage());

            //add back to timer
            float delta = ((State_Play)GameContext.GetCurrState()).GetCurrDelta();
            TimerEventMan.AddEvent(TimerEvent.EventName.AnimateSprite, this, delta);
        }


    } // end class

} // end namespace