GAM456-SpaceInvaders / SpaceInvaders / 6_Collision / ShipObservers / ShipDeathObserver.cs
ShipDeathObserver.cs
Raw
using System;
using System.Diagnostics;

namespace SpaceInvaders
{
    class ShipDeathObserver : CollisionObserver
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        GameObject pShip;

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

        public ShipDeathObserver() : base()
        {
            this.pShip = null;
        }

        public ShipDeathObserver(ShipDeathObserver s) : base()
        {
            Debug.Assert(s != null);
            this.pShip = s.pShip;
        }


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

        public override void Notify()
        {
            // Get current player
            Player pActivePlayer = PlayerMan.GetActivePlayer();
            Debug.Assert(pActivePlayer != null);

            //decrement life
            pActivePlayer.LoseLife();

            //update player lives UI
            Font pFont = FontMan.FindFont(Font.FontName.PlayerLife);
            Debug.Assert(pFont != null);
            pFont.UpdateMessage(pActivePlayer.GetLives().ToString());

            //remove ship
            this.pShip = (Ship)this.pSubject.pObjA;
            if(this.pShip.isDead == false)
            {
                // mark for death
                this.pShip.isDead = true;

                // create new observer for next ship
                ShipDeathObserver pObserver = new ShipDeathObserver(this);

                // add to DelayObjectMan -- deferred removal
                DelayObjectMan.AttachDelay(pObserver);
            }
        }

        public override void Execute()
        {
            // Get current player
            Player pActivePlayer = PlayerMan.GetActivePlayer();
            Debug.Assert(pActivePlayer != null);

            this.pShip.Remove();

            if (pActivePlayer.GetLives() > 0)
            {
                //if player has lives left, add command to revive ship
                TimerEventMan.AddEvent(TimerEvent.EventName.ReviveShip, new ReviveShip(), 1.0f);
            }
            else if (pActivePlayer.GetLives() == 0)
            {
                //player has no lives, switch game over screen
                GameContext.GetCurrState().Handle();
            }
        }


    } // end class

} // end namespace