//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> /// Initialize the world and set up the game /// </summary> public void Initialize(string path) { //Create the controllers kinect = new KinectManager(gameHeight); mouse = new MouseController(); keyboard = new KeyController(); //Make sure there is a Kinect connected if (!kinect.Initialize()) { Console.WriteLine("No Kinect Sensor Connected!"); Environment.Exit(0); } //Load the map and retrieve the dimensions map = new Map(); map.LoadLevel(@"Levels\" + path + ".txt", content); gameHeight = map.GameDimensions[0]; gameWidth = map.GameDimensions[1]; kinect.GameHeight = gameHeight; //Create the objects and set up the world slingshot = new SlingShot(content, kinect, gameHeight); map.SetSlingShot = slingshot; player = new Player(kinect, mouse, content, slingshot); map.SetPlayer = player; slingshot.PlayerPos = player.GetSprite.GetPosition; pointer = new Pointer(content, kinect, mouse); //Create the Pause Button font = content.Load <SpriteFont>("pauseFont"); SpriteFont buttonFont = content.Load <SpriteFont>("buttonFont"); string output = "Play"; Vector2 textSize = buttonFont.MeasureString(output); unPause = new Button(new Vector2((screenWidth * 0.5f) - (textSize.X * 0.5f), screenHeight * 0.5f + textSize.Y), output, buttonFont, content, pointer); output = "Return To Menu"; textSize = buttonFont.MeasureString(output); returnToMenu = new Button(new Vector2((screenWidth * 0.5f) - (textSize.X * 0.5f), unPause.GetSprite.GetPosition.Y + 50), output, buttonFont, content, pointer); //Set up the background Texture2D bg = content.Load <Texture2D>("Background"); Background = new Sprite(new Rectangle(0, 0, bg.Width, screenHeight), bg); //Set up the designer & menu levelDesigner = new Designer(kinect, content, pointer, screenWidth, screenHeight); menu = new Menu(content, pointer, screenWidth, screenHeight, this); // Set up all of your camera options //Set the bounding rectangle around the whole World cam.SetLimits(new Rectangle(0, 0, gameWidth, gameHeight)); //Set the maximum factor to zoom in by, 3.0 is the default cam.SetZoom(1.0f); cam.SetMaxZoom(4.0f); //Set the starting World position of the camera, The below code sets it to the exact centre of the default screen size cam.SetPosition(player.GetSprite.GetPosition); //Set the origin of the camera, typically is the starting position of the camera cam.SetOrigin(cam.GetPosition()); //centre the camera on the player cam.LookAt(player.GetSprite.GetBounds); //If the game is reseting if (worldState == States.reset) { //Start the game worldState = States.play; } }
/// <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); }