GAM456-SpaceInvaders / SpaceInvaders / 6_Collision / AlienObservers / RemoveAlienObserver.cs
RemoveAlienObserver.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class RemoveAlienObserver : CollisionObserver
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private GameObject pAlien;
        private GameObject pGridCol;


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

        public RemoveAlienObserver() : base()
        {
            this.pAlien = null;
            this.pGridCol = null;
        }

        public RemoveAlienObserver(RemoveAlienObserver a) : base()
        {
            Debug.Assert(a != null);
            this.pAlien = a.pAlien;
            this.pGridCol = a.pGridCol;
        }


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

        public override void Notify()
        {
            //Debug.WriteLine("Remove Alien Obs: {0} {1}", this.pSubject.pObjA, this.pSubject.pObjB);
            this.pAlien = (Aliens)this.pSubject.pObjB;
            Debug.Assert(this.pAlien != null);

            if (pAlien.isDead == false)
            {
                pAlien.isDead = true;

                RemoveAlienObserver pObserver = new RemoveAlienObserver(this);
                DelayObjectMan.AttachDelay(pObserver);
            }

            //TODO: increase alien speed (after certaim amount of aliens die)
            float gridDelta = ((State_Play)GameContext.GetCurrState()).GetCurrDelta() - 0.009f;
            if(gridDelta > 0.0f)
            {
                ((State_Play)GameContext.GetCurrState()).SetCurrDelta(gridDelta);
            }
        }

        public override void Execute()
        {
            //TODO: remove alien
            GameObject pA = (GameObject)this.pAlien;
            GameObject pB = (GameObject)ForwardIterator.GetParent(pA);

            pA.Remove();    //remove alien

            //if col is empty, remove
            if (ForwardIterator.GetChild(pB) == null)
            {
                GameObject pC = (GameObject)ForwardIterator.GetParent(pB);

                // mark for death & remove
                pB.isDead = true;
                pB.Remove();

                // grid is empty
                if(ForwardIterator.GetChild(pC) == null)
                {
                    // initiate level reset
                    GameContext.ResetLevel();
                }
            }
        }

    } // end class

} // end namespace