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


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

        AnimateGrid pMoveGrid;
        TimerEvent pEvent;


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

        public AlienGridObserver() : base()
        {
            this.pMoveGrid = null;
            this.pEvent = null;
        }


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

        public override void Notify()
        {
            //grab 2 collided objects
            GameObject pGrid;
            Walls pWall;
            if (this.pSubject.pObjA.GetType() == typeof(AlienGrid))
            {
                pGrid = (AlienGrid)this.pSubject.pObjA;
                pWall = (Walls)this.pSubject.pObjB;
            }
            else
            {
                pGrid = (AlienGrid)this.pSubject.pObjB;
                pWall = (Walls)this.pSubject.pObjA;
            }
           

            // alien grid reaches player, game over
            if(pGrid.y < 170) 
            { 
                GameContext.GetCurrState().Handle();
                return;
            }

            float delta = ((State_Play)GameContext.GetCurrState()).GetCurrDelta();
            //collided with right wall
            if (pWall.GetWallType() == Walls.WallType.Right)
            {
                pGrid.Move(0.0f, -20.0f);   //move down
                pGrid.Move(-7.0f, 0.0f);    //reverse direction

                //remove current grid move event
                pEvent = TimerEventMan.FindEvent(TimerEvent.EventName.AnimateGrid);
                TimerEventMan.RemoveEvent(pEvent);

                //add updated event with new move direction
                pMoveGrid = new AnimateGrid(pGrid, -7.0f);
                TimerEventMan.AddEvent(TimerEvent.EventName.AnimateGrid, pMoveGrid, delta);
            }

            //collided with left wall
            if (pWall.GetWallType() == Walls.WallType.Left)
            {
                pGrid.Move(0.0f, -20.0f);   //move down
                pGrid.Move(7.0f, 0.0f);     //reverse direction

                //remove current grid move event
                pEvent = TimerEventMan.FindEvent(TimerEvent.EventName.AnimateGrid);
                TimerEventMan.RemoveEvent(pEvent);

                //add updated event with new move direction
                pMoveGrid = new AnimateGrid(pGrid, 7.0f);
                TimerEventMan.AddEvent(TimerEvent.EventName.AnimateGrid, pMoveGrid, delta);
            }
        }

    } // end class

} // end namespace