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


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

        private GameObject pUFO;
        private readonly Random rand = new Random();


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

        public RemoveUFOObserver() : base()
        {
            this.pUFO = null;
        }

        public RemoveUFOObserver(RemoveUFOObserver a) : base()
        {
            Debug.Assert(a != null);
            this.pUFO = a.pUFO;
        }


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

        public override void Notify()
        {
            //Debug.WriteLine("Remove UFO: {0} {1}", this.pSubject.pObjA, this.pSubject.pObjB);
            this.pUFO = (UFOAlien)this.pSubject.pObjB;

            if(pUFO.isDead == false)
            {
                pUFO.isDead = true;
                RemoveUFOObserver pObserver = new RemoveUFOObserver(this);
                DelayObjectMan.AttachDelay(pObserver);
            }

            //remove curr event 
            TimerEvent pEvent = TimerEventMan.FindEvent(TimerEvent.EventName.AnimateUFO);
            if(pEvent != null)
            {
                TimerEventMan.RemoveEvent(pEvent);
            }

            //deploy new at random time
            TimerEventMan.AddEvent(TimerEvent.EventName.DeployUFO, new DeployUFO(), rand.Next(7, 25));
        }

        public override void Execute()
        {
            this.pUFO.Remove();
        }


    } // end class

} // end namespace