// Checks if the mouse is over one of the buttons in a menu. // Parameter: button - The index of the button to check // Parameter: level - The level of the menu // Parameter: xOffset - The xOffset of the menu // Returns: True if the mouse is over the button private static bool IsMouseOverMenu(int button, int level, int xOffset) { int btnTop = MENU_TOP - (MENU_GAP + BUTTON_HEIGHT) * level; int btnLeft = MENU_LEFT + BUTTON_SEP * (button + xOffset); return(UtilityFunctions.IsMouseInRectangle(btnLeft, btnTop, BUTTON_WIDTH, BUTTON_HEIGHT)); }
/// <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.vk_ESCAPE)) { GameController.AddNewState(GameState.ViewingGameMenu); } if (SwinGame.KeyTyped(KeyCode.vk_UP) | SwinGame.KeyTyped(KeyCode.vk_DOWN)) { _currentDirection = Direction.UpDown; } if (SwinGame.KeyTyped(KeyCode.vk_LEFT) | SwinGame.KeyTyped(KeyCode.vk_RIGHT)) { _currentDirection = Direction.LeftRight; } if (SwinGame.KeyTyped(KeyCode.vk_r)) { GameController.HumanPlayer.RandomizeDeployment(); GameController.ComputerPlayer.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(); GameController.ComputerPlayer.RandomizeDeployment(); } } }
// Summary: Gets the ship that the mouse is currently over in the selection panel. // Returns: The ship selected or none private static ShipName GetShipMouseIsOver() { foreach (ShipName sn in Enum.GetValues(typeof(ShipName))) { int i = 0; i = ((int)sn) - 1; if (UtilityFunctions.IsMouseInRectangle(SHIPS_LEFT, SHIPS_TOP + i * SHIPS_HEIGHT, SHIPS_WIDTH, SHIPS_HEIGHT)) { return(sn); } } return(ShipName.None); }
/// <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.vk_ESCAPE)) { GameController.AddNewState(GameState.ViewingGameMenu); } if (SwinGame.MouseClicked(MouseButton.LeftButton)) { if (UtilityFunctions.IsMouseInRectangle (EXIT_BUTTON_LEFT, TOP_BUTTONS_TOP, PLAY_BUTTON_WIDTH, TOP_BUTTONS_HEIGHT)) { GameController.AddNewState (GameState.ViewingGameMenu); } DoAttack(); } }
// Summary: Draws the game during the attack phase. // Remarks: Isuru - Updated keycodes public static void DrawDiscovery() { const int SCORES_LEFT = 172; const int SHOTS_TOP = 157; const int HITS_TOP = 206; const int SPLASH_TOP = 256; const int BACK_BUTTON_LEFT = 710; const int BACK_BUTTON_TOP = 5; const int BACK_BUTTON_WIDTH = 60; const int TOP_BUTTONS_HEIGHT = 46; //sets parameter to true if the player presses key combination of Shift + C if ((SwinGame.KeyDown(KeyCode.vk_RSHIFT) | SwinGame.KeyDown(KeyCode.vk_LSHIFT)) & SwinGame.KeyDown(KeyCode.vk_c)) { UtilityFunctions.DrawField(GameController.HumanPlayer.EnemyGrid, GameController.ComputerPlayer, true, false); } else { UtilityFunctions.DrawField(GameController.HumanPlayer.EnemyGrid, GameController.ComputerPlayer, true, true); } if (UtilityFunctions.IsMouseInRectangle(BACK_BUTTON_LEFT, BACK_BUTTON_TOP, BACK_BUTTON_WIDTH, TOP_BUTTONS_HEIGHT) && SwinGame.MouseClicked(MouseButton.LeftButton)) { GameController.AddNewState(GameState.ViewingGameMenu); } UtilityFunctions.DrawSmallField(GameController.HumanPlayer.PlayerGrid, GameController.HumanPlayer); UtilityFunctions.DrawMessage(); SwinGame.DrawBitmap(GameResources.GameImage("Back"), BACK_BUTTON_LEFT, BACK_BUTTON_TOP); 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); }
// Summary: Handles user input for the Deployment phase of the game. /* * Remarks: Involves selecting the ships, deloying ships, changing the direction * of the ships to add, randomising deployment, and then ending deployment * Isuru: Updated Keycodes */ public static void HandleDeploymentInput() { //Shows menu when esc key pressed if (SwinGame.KeyTyped(KeyCode.vk_ESCAPE)) { GameController.AddNewState(GameState.ViewingGameMenu); } //Moves ships direction to vertical when down or up key pressed if (SwinGame.KeyTyped(KeyCode.vk_UP) || SwinGame.KeyTyped(KeyCode.vk_DOWN)) { _currentDirection = Direction.UpDown; } //Moves ships direction to horizontal when left or right key pressed if (SwinGame.KeyTyped(KeyCode.vk_LEFT) || SwinGame.KeyTyped(KeyCode.vk_RIGHT)) { _currentDirection = Direction.LeftRight; } //Randomises ship deploymeny when R key pressed if (SwinGame.KeyTyped(KeyCode.vk_r)) { GameController.HumanPlayer.RandomizeDeployment(); } //Selects ship if ship placed at cursor location, deploys ship otherwise. //Also checks for button presses onscreen. if (SwinGame.MouseClicked(MouseButton.LeftButton)) { ShipName selected = default(ShipName); selected = GetShipMouseIsOver(); if (selected != ShipName.None) { _selectedShip = selected; } else { DoDeployClick(); } if (help_screen == false) { 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.UpDown; } 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(); } else if (UtilityFunctions.IsMouseInRectangle(CLEAR_BUTTON_LEFT, TOP_BUTTONS_TOP, CLEAR_BUTTON_WIDTH, CLEAR_BUTTON_HEIGHT)) { //clears board GameController.HumanPlayer.PlayerGrid.ClearBoard(); } else if (UtilityFunctions.IsMouseInRectangle(BACK_BUTTON_LEFT, BACK_BUTTON_TOP, BACK_BUTTON_WIDTH, TOP_BUTTONS_HEIGHT)) { //Goes back to main menu GameController.AddNewState(GameState.ViewingMainMenu); } else if (UtilityFunctions.IsMouseInRectangle(HELP_BUTTON_LEFT, TOP_BUTTONS_TOP, CLEAR_BUTTON_WIDTH, CLEAR_BUTTON_HEIGHT)) { if (help_screen) { help_screen = false; } else { help_screen = true; } } } else { help_screen = false; } } }