//Pre: The List of buttons, the gamestate, the camera object, the screen height and the camera displacement //Post: The scrolling buttons are updated with whether they were clicked or not and the new gamestate is returned //Desc: A method for updating the scrolling buttons and whether they were clicked or not public GameState UpdateScrollingButtons(List <Button> buttons, GameState gameState, Cam2D camera, int screenHeight, int cameraDisplacement) { //Sets the mouses' state mouse = Mouse.GetState(); //If the mouse button is released if (mouse.LeftButton == ButtonState.Released) { //Set it to not being held anymore mouseButtonHeld = false; } //Loop for every button for (int i = 0; i < buttons.Count; i++) { //Creates a rectangle that is used to store the location of the current button Rectangle tempRect; //If the current button is not the first button, the rectangle for it is set if (i != 0) { tempRect = new Rectangle(buttons[i].GetCurrentRectangle().X, (buttons[i].GetCurrentRectangle().Y - (int)((camera.GetPosition().Y + cameraDisplacement) - (int)(screenHeight / 2))), buttons[i].GetCurrentRectangle().Width, buttons[i].GetCurrentRectangle().Height); } //If the current button is the first button, the rectangle for it is set else { tempRect = buttons[i].GetCurrentRectangle(); } //If the current button is visible on the screen if (camera.IntersectsScreen(buttons[i].GetCurrentRectangle()) || buttons[i].GetText() == "Go Back") { //The current button is checked for hovering if (CheckButtonHovering(tempRect)) { //The current button is checked if it was pressed if (CheckButtonPressed(tempRect)) { //Changes the states depending on the state stored in the button class and sets the button to clicked gameState = buttons[i].GetGameState(); buttons[i].isClicked = true; } //If the current button was not pressed but is hovering, the current button is set to hovering else { buttons[i].SetHovering(true); } } //if the current button is hovering, the button is set to hovering else { buttons[i].SetHovering(false); } } } //Returns the game state return(gameState); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } switch (gameState) { case GameState.GameLogin: keyboardMouseInput.UpdateTextBoxes(loginTextboxs); for (int i = 0; i < loginTextboxs.Count; i++) { if (loginTextboxs[i].TextBoxClicked) { if (loginTextboxs[i].Text == "USER HERE" || loginTextboxs[i].Text == "PASS HERE") { loginTextboxs[i].Text = ""; } keyboardMouseInput.UpdateKeyboardTyping(keyboardTypingKeys, loginTextboxs[i].Text, false); loginTextboxs[i].Text = keyboardMouseInput.userText; } } gameState = keyboardMouseInput.UpdateButtons(loginButtons, gameState); if (gameState == GameState.MainMenu) { userTaken = false; if (profileHelper.CheckForProfile(loginTextboxs[0].Text, loginTextboxs[1].Text)) { currentUser = loginTextboxs[0].Text; } else { gameState = GameState.GameLogin; isAccountInvalid = true; } } else if (gameState == GameState.AccountCreator) { if (loginTextboxs[0].Text != "" && loginTextboxs[1].Text != "") { if (profileHelper.AddProfile(loginTextboxs[0].Text, loginTextboxs[1].Text)) { currentUser = loginTextboxs[0].Text; gameState = GameState.MainMenu; } else { userTaken = true; gameState = GameState.GameLogin; } } else { isAccountInvalid = true; gameState = GameState.GameLogin; } } break; case GameState.StartMapCreator: keyboardMouseInput.UpdateTextBoxes(startMapCreatorTextboxs); for (int i = 0; i < startMapCreatorTextboxs.Count; i++) { if (startMapCreatorTextboxs[i].TextBoxClicked) { if (startMapCreatorTextboxs[i].Text == "ROW NUM" || startMapCreatorTextboxs[i].Text == "COLUMN NUM") { startMapCreatorTextboxs[i].Text = ""; } keyboardMouseInput.UpdateKeyboardTyping(keyboardNumKeys, startMapCreatorTextboxs[i].Text, true); startMapCreatorTextboxs[i].Text = keyboardMouseInput.userText; } } gameState = keyboardMouseInput.UpdateButtons(startMapCreatorButtons, gameState); if (gameState == GameState.MapCreator) { if (Convert.ToInt32(startMapCreatorTextboxs[0].Text) <= 200 && Convert.ToInt32(startMapCreatorTextboxs[1].Text) <= 200 && Convert.ToInt32(startMapCreatorTextboxs[0].Text) >= 15 && Convert.ToInt32(startMapCreatorTextboxs[1].Text) >= 15) { newRowAmount = Convert.ToInt32(startMapCreatorTextboxs[0].Text); newColumnAmount = Convert.ToInt32(startMapCreatorTextboxs[1].Text); CreateMap(); } else { startMapCreatorTextboxs[0].Text = ""; startMapCreatorTextboxs[1].Text = ""; gameState = GameState.StartMapCreator; invalidMapSize = true; } } break; case GameState.MainMenu: gameState = keyboardMouseInput.UpdateButtons(mainMenuButtons, gameState); switch (gameState) { case GameState.StartMapCreator: startMapCreatorTextboxs[0].Text = "ROW NUM"; startMapCreatorTextboxs[1].Text = "COLUMN NUM"; break; case GameState.ChooseMap: //Download all the maps and display all the avialable maps to the user //After they click a map, if a load of it is possible ask the user if they want to load the map break; case GameState.ExitGame: this.Exit(); break; case GameState.SignOut: gameState = GameState.GameLogin; loginTextboxs[0].Text = "USER HERE"; loginTextboxs[1].Text = "PASS HERE"; for (int i = 0; i < loginTextboxs.Count; i++) { loginTextboxs[i].TextBoxClicked = false; } currentUser = ""; break; } break; case GameState.Game: if (!IsActive) { gameState = GameState.Pause; } gameState = keyboardMouseInput.UpdateGameControls(gameKeyboardKeys, player, gameState, gameTiles); player.Update(); camera.LookAt(player.GetBounds()); player.CalcRotation(keyboardMouseInput.mouse.X + (camera.GetPosition().X - (SCREEN_WIDTH / 2)), keyboardMouseInput.mouse.Y + (camera.GetPosition().Y - (SCREEN_HEIGHT / 2))); projectiles.AddRange(player.Projectiles); player.Projectiles.Clear(); enemiesInView.Clear(); tilesInView.Clear(); foreach (Tile tile in gameTiles) { if (camera.IntersectsScreen(tile.PositionRect)) { tilesInView.Add(tile); } } foreach (Enemy enemy in enemies) { if (camera.IntersectsScreen(enemy.GetBounds())) { enemiesInView.Add(enemy); } } for (int i = 0; i < enemiesInView.Count; i++) { enemiesInView[i].Update(player, gameTiles); projectiles.AddRange(enemiesInView[i].Projectiles); enemiesInView[i].Projectiles.Clear(); if (enemiesInView[i].Health <= 0) { enemies.Remove(enemiesInView[i]); enemiesInView[i] = null; enemiesInView.RemoveAt(i); } } for (int i = 0; i < projectiles.Count; i++) { projectiles[i].Update(gameTiles, new Vector2(camera.GetPosition().X + (SCREEN_WIDTH / 2), camera.GetPosition().Y + (SCREEN_HEIGHT / 2))); if (projectiles[i].NeedDestroy) { projectiles[i] = null; projectiles.RemoveAt(i); i--; } } quadTree.Clear(); for (int i = 0; i < projectiles.Count; i++) { quadTree.Insert(projectiles[i]); } CheckBulletCollision(); break; case GameState.Pause: break; case GameState.MapCreatorHelp: if (IsActive) { //Sets the game state using the returned value form the key checking class gameState = keyboardMouseInput.UpdateCreatorHelp(gameState); } break; case GameState.MapCreator: if (IsActive) { gameState = keyboardMouseInput.UpdateCreatorControls(gameKeyboardKeys, gameTiles, gameState, camera); } enemiesInView.Clear(); tilesInView.Clear(); foreach (Tile tile in gameTiles) { if (camera.IntersectsScreen(tile.PositionRect)) { tilesInView.Add(tile); } } foreach (Enemy enemy in enemies) { if (camera.IntersectsScreen(enemy.GetBounds())) { enemiesInView.Add(enemy); } } if (IsActive) { if (keyboardMouseInput.mouse.LeftButton == ButtonState.Pressed) { int row = (int)((keyboardMouseInput.mouse.X + (camera.GetPosition().X - (SCREEN_WIDTH / 2))) / Tile.TILE_X_SIZE); int column = (int)((keyboardMouseInput.mouse.Y + (camera.GetPosition().Y - (SCREEN_HEIGHT / 2))) / Tile.TILE_Y_SIZE); if (row >= 0 && column >= 0 && row <= gameTiles.GetLength(0) && column <= gameTiles.GetLength(1)) { if (!keyboardMouseInput.PlacingEnemy) { if (keyboardMouseInput.CurrentTileType == TileType.Spawn) { if (keyboardMouseInput.CheckMouse()) { if (startTile != null) { gameTiles[startTile.Row, startTile.Column].CurrentTileType = TileType.Blank; } startTile = gameTiles[row, column]; gameTiles[row, column].CurrentTileType = TileType.Spawn; } } else { if (gameTiles[row, column].CurrentTileType == TileType.Spawn) { startTile = null; gameTiles[row, column].CurrentTileType = keyboardMouseInput.CurrentTileType; } else { gameTiles[row, column].CurrentTileType = keyboardMouseInput.CurrentTileType; } } if (gameTiles[row, column].EnemyExists) { gameTiles[row, column].EnemyExists = false; enemies.Remove(gameTiles[row, column].EnemyPlaced); gameTiles[row, column].EnemyPlaced = null; } } else { if (!gameTiles[row, column].EnemyExists) { if (gameTiles[row, column].CurrentTileType == TileType.Spawn) { startTile = null; } gameTiles[row, column].CurrentTileType = TileType.Blank; enemies.Add(new Enemy(gameTiles, (row * Tile.TILE_X_SIZE), (column * Tile.TILE_Y_SIZE), HUMAN_WIDTH, HUMAN_HEIGHT, fullScreenSize.Width, fullScreenSize.Height)); gameTiles[row, column].EnemyExists = true; gameTiles[row, column].EnemyPlaced = enemies[enemies.Count - 1]; } } } } } break; default: break; } // TODO: Add your update logic here base.Update(gameTime); }