GAM456-SpaceInvaders / SpaceInvaders / 0_Input / InputMan.cs
InputMan.cs
Raw
using System;
using System.Diagnostics;


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

        private static InputMan poInstance = null;

        //reset game key
        private InputSubject pKeyR;
        private bool prevKeyR;

        // UNIVERSAL KEYS
        //toggle keys
        private InputSubject pKeyT;
        private bool prevKeyT;

        //ship input keys
        private InputSubject pRightArrow;   //ship movement
        private InputSubject pLeftArrow;
        private InputSubject pSpace;        //shooting
        private bool prevSpaceKey;          //check prev press for shooting

        //player selection
        private InputSubject pKey1;         //Key #1: select player 1
        private InputSubject pKey2;         //Key #2: select player 2
        private bool prevKey1;              //store player selection?
        private bool prevKey2;


        //-------------------------------------------------------------------
        //  CONSTRUCTION
        //-------------------------------------------------------------------
        public InputMan()
        {
            this.pKeyR = new InputSubject();
            this.prevKeyR = false;

            this.pKeyT = new InputSubject();
            this.prevKeyT = false;

            this.pLeftArrow = new InputSubject();
            this.pRightArrow = new InputSubject();

            this.pSpace = new InputSubject();
            this.prevSpaceKey = false;

            this.pKey1 = new InputSubject();
            this.prevKey1 = false;

            this.pKey2 = new InputSubject();
            this.prevKey2 = false;
        }


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

        public static void Create()
        {
            if(InputMan.poInstance == null)
            {
                InputMan.poInstance = new InputMan();
                Debug.Assert(InputMan.poInstance != null);
            }
        }

        public static InputSubject GetLeftArrow()
        {
            InputMan pManager = InputMan.GetInstance();
            Debug.Assert(pManager != null);

            return pManager.pLeftArrow;
        }

        public static InputSubject GetRightArrow()
        {
            InputMan pManager = InputMan.GetInstance();
            return pManager.pRightArrow;
        }

        public static InputSubject GetSpace()
        {
            InputMan pManager = InputMan.GetInstance();
            return pManager.pSpace;
        }

        public static InputSubject GetKey1()
        {
            InputMan pManager = InputMan.GetInstance();
            return pManager.pKey1;
        }

        public static InputSubject GetKey2()
        {
            InputMan pManager = InputMan.GetInstance();
            return pManager.pKey2;
        }

        public static InputSubject GetKeyT()
        {
            InputMan pManager = InputMan.GetInstance();
            return pManager.pKeyT;
        }

        public static InputSubject GetKeyR()
        {
            InputMan pManager = InputMan.GetInstance();
            return pManager.pKeyR;
        }

        public static void Update()
        {
            InputMan pManager = InputMan.GetInstance();

            //Key R: (with key history))-----------------------------------------
            bool currRKey = Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_R);

            if (currRKey == true && pManager.prevKeyR == false)
            {
                pManager.pKeyR.Notify();
            }
            pManager.prevKeyR = currRKey;

            //Space Key: (with key history)-----------------------------------------
            bool currSpaceKey = Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_SPACE);

            if (currSpaceKey == true && pManager.prevSpaceKey == false)
            {
                // Note: can't shoot missile with no active ship
                if(PlayerMan.GetActivePlayer().GetLives() > 0)
                {
                    pManager.pSpace.Notify();
                }
            }
            pManager.prevSpaceKey = currSpaceKey;

            //Left Key--------------------------------------------------------------
            if (Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_ARROW_LEFT) == true)
            {
                pManager.pLeftArrow.Notify();
            }

            //Right Key-------------------------------------------------------------
            if (Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_ARROW_RIGHT) == true)
            {
                pManager.pRightArrow.Notify();
            }

            //#1 Key: (with key history)--------------------------------------------
            bool currKey1 = Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_1);

            if (currKey1 == true && pManager.prevKey1 == false)
            {
                GameContext.SetPlayMode(false);
                pManager.pKey1.Notify();
            }
            pManager.prevKey1 = currKey1;

            //#2 Key: (with key history)---------------------------------------------
            bool currKey2 = Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_2);

            if (currKey2 == true && pManager.prevKey2 == false)
            {
                GameContext.SetPlayMode(true);
                pManager.pKey2.Notify();
            }
            pManager.prevKey2 = currKey2;

            //Key T: (with key history)----------------------------------------------
            bool currKeyT = Azul.Input.GetKeyState(Azul.AZUL_KEY.KEY_T);

            if (currKeyT == true && pManager.prevKeyT == false)
            {
                pManager.pKeyT.Notify();
            }
            pManager.prevKeyT = currKeyT;
        }


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

        private static InputMan GetInstance()
        {
            if (InputMan.poInstance == null)
            {
                InputMan.poInstance = new InputMan();
                Debug.Assert(InputMan.poInstance != null);
            }

            return InputMan.poInstance;
        }


    } // end class

} // end namespace