GAM456-SpaceInvaders / SpaceInvaders / 7_Player / PlayerMan.cs
PlayerMan.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class PlayerMan
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        static PlayerMan poInstnace = null;

        Player pCurrPlayer;
        Player poPlayer1;
        Player poPlayer2;


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

        private PlayerMan()
        {
            this.poPlayer1 = new Player(Player.PlayerType.Player1);
            Debug.Assert(this.poPlayer1 != null);

            this.poPlayer2 = new Player(Player.PlayerType.Player2);
            Debug.Assert(this.poPlayer2 != null);

            this.pCurrPlayer = this.poPlayer1;
        }


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

        public static Player GetActivePlayer()
        {
            PlayerMan pManager = PlayerMan.Instance();
            Debug.Assert(pManager != null);

            return pManager.pCurrPlayer;
        }
        
        public static Player GetPlayer(Player.PlayerType type)
        {
            PlayerMan pManager = PlayerMan.Instance();
            Debug.Assert(pManager != null);

            Player pPlayer = null;
            switch (type)
            {
                case Player.PlayerType.Player1:
                    pPlayer = pManager.poPlayer1;
                    break;
                case Player.PlayerType.Player2:
                    pPlayer = pManager.poPlayer2;
                    break;
                default:
                    Debug.Assert(false);
                    break;
            }

            return pPlayer;
        }

        public static void SwitchPlayers()
        {
            PlayerMan pManager = PlayerMan.Instance();
            Debug.Assert(pManager != null);

            if(pManager.pCurrPlayer.GetPlayerType() == Player.PlayerType.Player1)
            {
                //switch player 1 --> player 2
                pManager.pCurrPlayer = pManager.poPlayer2;
            }
            else if(pManager.pCurrPlayer.GetPlayerType() == Player.PlayerType.Player2)
            {
                //switch player 2 --> player 1
                pManager.pCurrPlayer = pManager.poPlayer1;
            }
            else
            {
                //shouldn't be hit
                Debug.Assert(false);
            }
        }

        public static void ResetPlayers()
        {
            PlayerMan pManager = PlayerMan.Instance();
            Debug.Assert(pManager != null);

            pManager.poPlayer1.Reset();
            pManager.poPlayer2.Reset();
        }


        //-------------------------------------------------------------------
        //  PRIVATE METHODS
        //-------------------------------------------------------------------

        private static PlayerMan Instance()
        {
            if (PlayerMan.poInstnace == null)
            {
                PlayerMan.poInstnace = new PlayerMan();
                Debug.Assert(PlayerMan.poInstnace != null);
            }

            return PlayerMan.poInstnace;
        }

    }
}