Exemple #1
0
        /// <summary>
        /// The main menu was clicked, perform the button's action.
        /// </summary>
        /// <param name="button">the button pressed</param>
        private static void PerformMainMenuAction(int button)
        {
            switch (button)
            {
            case MAIN_MENU_PLAY_BUTTON:
            {
                GameController.StartGame();
                break;
            }

            case MAIN_MENU_SETUP_BUTTON:
            {
                GameController.AddNewState(GameState.AlteringSettings);
                break;
            }

            case MAIN_MENU_TOP_SCORES_BUTTON:
            {
                GameController.AddNewState(GameState.ViewingHighScores);
                break;
            }

            case MAIN_MENU_QUIT_BUTTON:
            {
                GameController.EndCurrentState();
                break;
            }
            }
        }
        /// <summary>
        /// The game menu was clicked, perform the button's action.
        /// <param name="button">the button pressed</param>
        /// </summary>
        private static void PerformGameMenuAction(int button)
        {
            switch (button)
            {
            case GAME_MENU_RETURN_BUTTON:
            {
                GameController.EndCurrentState();
                break;
            }

            case GAME_MENU_VOLUME_BUTTON:
            {
                GameController.AddNewState(GameState.AlteringVolume);
                break;
            }

            case GAME_MENU_SURRENDER_BUTTON:
            {
                GameController.EndCurrentState();         // end game menu
                GameController.EndCurrentState();         // end game
                break;
            }

            case GAME_MENU_QUIT_BUTTON:
            {
                GameController.AddNewState(GameState.Quitting);
                break;
            }
            }
        }
        /// <summary>
        /// Draws the game during the attack phase.
        /// </summary>s
        public static void DrawDiscovery()
        {
            const int SCORES_LEFT = 172;
            const int SHOTS_TOP   = 157;
            const int HITS_TOP    = 206;
            const int SPLASH_TOP  = 256;

            if ((SwinGame.KeyDown(KeyCode.vk_LSHIFT) | SwinGame.KeyDown(KeyCode.vk_RSHIFT)) & SwinGame.KeyDown(KeyCode.vk_c))
            {
                UtilityFunctions.DrawField(GameController.HumanPlayer.EnemyGrid, GameController.ComputerPlayer, true);
            }
            else
            {
                UtilityFunctions.DrawField(GameController.HumanPlayer.EnemyGrid, GameController.ComputerPlayer, false);
            }

            UtilityFunctions.DrawSmallField(GameController.HumanPlayer.PlayerGrid, GameController.HumanPlayer);
            UtilityFunctions.DrawMessage();

            SwinGame.DrawText(GameController.HumanPlayer.Shots.ToString(), Color.White, GameResources.GameFont("Menu"), SCORES_LEFT, SHOTS_TOP);
            SwinGame.DrawText(GameController.HumanPlayer.Hits.ToString(), Color.White, GameResources.GameFont("Menu"), SCORES_LEFT, HITS_TOP);
            SwinGame.DrawText(GameController.HumanPlayer.Missed.ToString(), Color.White, GameResources.GameFont("Menu"), SCORES_LEFT, SPLASH_TOP);

            SwinGame.DrawBitmap(GameResources.GameImage("ResetButton"), RESET_BUTTON_X, RESET_BUTTON_Y);

            if ((SwinGame.MouseClicked(MouseButton.LeftButton)) && (UtilityFunctions.IsMouseInRectangle(RESET_BUTTON_X, RESET_BUTTON_Y, RESET_BUTTON_WIDTH, RESET_BUTTON_WIDTH)))
            {
                //GameController.SwitchState(GameState.Discovering);
                GameController.AddNewState(GameState.ViewingGameMenu);
            }
        }
 /// <summary>
 /// Redraws the current game from the beggining
 /// </summary>s
 private static void ResetDiscovery()
 {
     if (UtilityFunctions.IsMouseInRectangle(RESET_BUTTON_X, RESET_BUTTON_Y, RESET_BUTTON_WIDTH, RESET_BUTTON_WIDTH))
     {
         GameController.AddNewState(GameState.ViewingGameMenu);
     }
 }
        // '' <summary>
        // '' Starts a new game.
        // '' </summary>
        // '' <remarks>
        // '' Creates an AI player based upon the _aiSetting.
        // '' </remarks>
        public static void StartGame()
        {
            if (_theGame)
            {
                IsNot;
                null;
                GameController.EndGame();
                // Create the game
                _theGame = new BattleShipsGame();
                // create the players
                switch (_aiSetting)
                {
                case AIOption.Medium:
                    _ai = new AIMediumPlayer(_theGame);
                    break;

                case AIOption.Hard:
                    _ai = new AIHardPlayer(_theGame);
                    break;

                default:
                    _ai = new AIHardPlayer(_theGame);
                    break;
                }
                _human = new Player(_theGame);
                // AddHandler _human.PlayerGrid.Changed, AddressOf GridChanged
                _ai.PlayerGrid.Changed   += new System.EventHandler(this.GridChanged);
                _theGame.AttackCompleted += new System.EventHandler(this.AttackCompleted);
                GameController.AddNewState(GameState.Deploying);
            }

            // '' <summary>
            // '' Stops listening to the old game once a new game is started
            // '' </summary>
        }
        public void TestGameState()
        {
            GameState state = GameState.Deploying;

            GameController.AddNewState(state);

            Assert.AreEqual(GameController.CurrentState, state);
        }
        /// <summary>
        /// Read the user's name for their highscore.
        /// <param name="value">the player's score.</param>
        /// <remarks>This verifies if the score is a highscore.</remarks>
        /// </summary>
        public static void ReadHighScore(int value)
        {
            const int ENTRY_TOP = 500;

            if (_Scores.Count == 0)
            {
                LoadScores();
            }

            // is it a highscore
            if (value > _Scores[_Scores.Count - 1].Value)
            {
                Score s = new Score();
                s.Value = value;

                GameController.AddNewState(GameState.ViewingHighScores);

                int x;
                x = SCORES_LEFT + SwinGame.TextWidth(GameResources.GameFont("Courier"), "Name: ");

                Rectangle nameEntryArea = new Rectangle
                {
                    X      = x,
                    Y      = ENTRY_TOP,
                    Width  = 20,
                    Height = 10
                };

                SwinGame.StartReadingText(Color.White, NAME_WIDTH, GameResources.GameFont("Courier"), nameEntryArea);

                // Read the text from the user
                while (SwinGame.ReadingText())
                {
                    SwinGame.ProcessEvents();

                    UtilityFunctions.DrawBackground();
                    Console.WriteLine("loaded");

                    DrawHighScores();
                    SwinGame.DrawText("Name: ", Color.White, GameResources.GameFont("Courier"), SCORES_LEFT, ENTRY_TOP);
                    SwinGame.RefreshScreen(60);
                }

                s.Name = SwinGame.TextReadAsASCII();

                if (s.Name.Length < 3)
                {
                    s.Name = s.Name + new string(' ', 3 - s.Name.Length);
                }

                _Scores.RemoveAt(_Scores.Count - 1);
                _Scores.Add(s);
                _Scores.Sort();

                GameController.EndCurrentState();
            }
        }
Exemple #8
0
        /// <summary>
        /// Handles input during the discovery phase of the game.
        /// </summary>
        /// <remarks>
        /// Escape opens the game menu. Clicking the mouse will
        /// attack a location.
        /// </remarks>
        public static void HandleDiscoveryInput()
        {
            if (SwinGame.KeyTyped(KeyCode.EscapeKey))
            {
                GameController.AddNewState(GameState.ViewingGameMenu);
            }

            if (SwinGame.MouseClicked(MouseButton.LeftButton))
            {
                DoAttack();
            }
        }
        public void TestEndCurrentState()
        {
            GameState state1 = GameState.Deploying;
            GameState state2 = GameState.Discovering;

            GameController.AddNewState(state1);
            GameController.AddNewState(state2);

            GameController.EndCurrentState();

            Assert.AreEqual(GameController.CurrentState, state1);
        }
        /// <summary>
        /// Handles user input for the Deployment phase of the game.
        /// </summary>
        /// <remarks>
        /// Involves selecting the ships, deloying ships, changing the direction
        /// of the ships to add, randomising deployment, end then ending
        /// deployment
        /// </remarks>
        public static void HandleDeploymentInput()
        {
            if (SwinGame.KeyTyped(KeyCode.EscapeKey))
            {
                GameController.AddNewState(GameState.ViewingGameMenu);
            }

            if (SwinGame.KeyTyped(KeyCode.UpKey) | SwinGame.KeyTyped(KeyCode.DownKey))
            {
                _currentDirection = Direction.UpDown;
            }
            if (SwinGame.KeyTyped(KeyCode.LeftKey) | SwinGame.KeyTyped(KeyCode.RightKey))
            {
                _currentDirection = Direction.LeftRight;
            }

            if (SwinGame.KeyTyped(KeyCode.RKey))
            {
                GameController.HumanPlayer.RandomizeDeployment();
            }

            if (SwinGame.MouseClicked(MouseButton.LeftButton))
            {
                ShipName selected = default(ShipName);
                selected = GetShipMouseIsOver();
                if (selected != ShipName.None)
                {
                    _selectedShip = selected;
                }
                else
                {
                    DoDeployClick();
                }

                if (GameController.HumanPlayer.ReadyToDeploy & UtilityFunctions.IsMouseInRectangle(PLAY_BUTTON_LEFT, TOP_BUTTONS_TOP, PLAY_BUTTON_WIDTH, TOP_BUTTONS_HEIGHT))
                {
                    GameController.EndDeployment();
                }
                else if (UtilityFunctions.IsMouseInRectangle(UP_DOWN_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT))
                {
                    _currentDirection = Direction.LeftRight;
                }
                else if (UtilityFunctions.IsMouseInRectangle(LEFT_RIGHT_BUTTON_LEFT, TOP_BUTTONS_TOP, DIR_BUTTONS_WIDTH, TOP_BUTTONS_HEIGHT))
                {
                    _currentDirection = Direction.LeftRight;
                }
                else if (UtilityFunctions.IsMouseInRectangle(RANDOM_BUTTON_LEFT, TOP_BUTTONS_TOP, RANDOM_BUTTON_WIDTH, TOP_BUTTONS_HEIGHT))
                {
                    GameController.HumanPlayer.RandomizeDeployment();
                }
            }
        }
        /// <summary>
        /// Read the user's name for their highsSwinGame.
        /// </summary>
        /// <param name="value">the player's sSwinGame.</param>
        /// <remarks>
        /// This verifies if the score is a highsSwinGame.
        /// </remarks>
        public static void ReadHighScore(int value)
        {
            const int ENTRY_TOP = 500;

            if (_Scores.Count == 0)
            {
                LoadScores();
            }

            //is it a high score
            if (value > _Scores[_Scores.Count - 1].Value)
            {
                Score s = new Score();
                s.Value = value;

                GameController.AddNewState(GameState.ViewingHighScores);

                int x = 0;

                x = SCORES_LEFT + SwinGame.TextWidth(GameResources.GameFont("Courier"), "Name: ");

                SwinGame.StartReadingText(Color.White, 4 * NAME_WIDTH, GameResources.GameFont("Courier"), x, ENTRY_TOP);

                //Read the text from the user
                while (SwinGame.ReadingText())
                {
                    SwinGame.ProcessEvents();

                    UtilityFunctions.DrawBackground();
                    DrawHighScores();
                    SwinGame.DrawText("Name: ", Color.White, GameResources.GameFont("Courier"), SCORES_LEFT, ENTRY_TOP);
                    SwinGame.RefreshScreen();
                }

                s.Name = SwinGame.TextReadAsASCII();

                if (s.Name.Length < 3)
                {
                    s.Name = s.Name + new string(Convert.ToChar(" "), 3 - s.Name.Length);
                }
                _Scores.RemoveAt(_Scores.Count - 1);
                _Scores.Add(s);
                _Scores.Sort();

                SaveScores();

                GameController.EndCurrentState();
            }
        }
 // '' <summary>
 // '' End the current state and add in the new state.
 // '' </summary>
 // '' <param name="newState">the new state of the game</param>
 public static void SwitchState(GameState newState)
 {
     GameController.EndCurrentState();
     GameController.AddNewState(newState);
 }