GAM456-SpaceInvaders / SpaceInvaders / 6_Collision / SystemObservers / PlayerPointsObserver.cs
PlayerPointsObserver.cs
Raw
using System;
using System.Diagnostics;

namespace SpaceInvaders
{
    public class PlayerPointsObserver : CollisionObserver
    {
        //-------------------------------------------------------------------
        //  CONSTRUCTION
        //-------------------------------------------------------------------

        Player pCurrPlayer;
        private Font.FontName pFont;
        private GameObject pAlien;


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

        public PlayerPointsObserver()
        {
            this.pCurrPlayer = PlayerMan.GetActivePlayer();
            Debug.Assert(this.pCurrPlayer != null);

            //determine which player font to change
            if (this.pCurrPlayer.GetPlayerType() == Player.PlayerType.Player1)
            {
                pFont = Font.FontName.P1_Score;
            }
            else
            {
                pFont = Font.FontName.P2_Score;
            }
        }


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

        public override void Notify()
        {
            //get the collided alien
            this.pAlien = (Aliens)this.pSubject.pObjB;
            Debug.Assert(this.pAlien != null);

            Aliens forScore = (Aliens)this.pAlien;

            //add appropriate points
            this.pCurrPlayer.AddPoints(forScore.GetPoints());
            if(forScore.GetPoints() >= 1500)
            {
                this.pCurrPlayer.BonusLife();

                Font pLife = FontMan.FindFont(Font.FontName.PlayerLife);
                Debug.Assert(pLife != null);
                pLife.UpdateMessage(this.pCurrPlayer.GetLives().ToString());
            }

            //update font
            Font pScore = FontMan.FindFont(pFont);
            pScore.UpdateMessage(this.pCurrPlayer.GetPoints().ToString("D4"));
        }


    } // end class

} // end namespace