GAM456-SpaceInvaders / SpaceInvaders / 6_Collision / OtherObservers / RemoveBrickObserver.cs
RemoveBrickObserver.cs
Raw
using System;
using System.Diagnostics;


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

        private GameObject pBrick;


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

        public RemoveBrickObserver() : base()
        {
            this.pBrick = null;
        }

        public RemoveBrickObserver(RemoveBrickObserver b) : base()
        {
            Debug.Assert(b != null);
            this.pBrick = b.pBrick;
        }


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

        public override void Notify()
        {
            //delete brick
            if(this.pSubject.pObjA.GetType() == typeof(ShieldBrick))
            {
                this.pBrick = (ShieldBrick)this.pSubject.pObjA;
            }
            else
            {
                this.pBrick = (ShieldBrick)this.pSubject.pObjB;
            }
            Debug.Assert(this.pBrick != null);

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

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

        public override void Execute()
        {
            //Debug.WriteLine("brick: {1}  parent: {1}", this.pBrick, this.pBrick.pParent); 
            GameObject pA = (GameObject)this.pBrick;
            GameObject pB = (GameObject)ForwardIterator.GetParent(pA);

            pA.Remove();    //remove brick

            //remove col if empty
            if (ForwardIterator.GetChild(pB) == null)
            {
                GameObject pC = (GameObject)ForwardIterator.GetParent(pB);
                pB.isDead = true;
                pB.Remove();    //remove col

                //remove shield root if empty?
                if (ForwardIterator.GetChild(pC) == null)
                {
                    pC.isDead = true;
                    pC.Remove();
                }
            }
        }

    } // end class

} // end namespace