/// <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; } }
//Pre: The list of keys that need to be checked, the array of gametiles, the current gamestate, and the camera //Post: The new gamestate is returned //Desc: A method for updating the creator controls public GameState UpdateCreatorControls(List <Keys> keysToCheck, Tile[,] gameTiles, GameState gameState, Cam2D camera) { //Updates the current keyboard state keyboardState = Keyboard.GetState(); //Updates the mouse state mouse = Mouse.GetState(); //If the mouse button is released if (mouse.LeftButton == ButtonState.Released) { //Set it to not being held anymore mouseButtonHeld = false; } //Updates the keys which are pressed pressedKeys = keyboardState.GetPressedKeys(); //Loop for every key that was pressed for (int i = 0; i < pressedKeys.Length; i++) { //If the current key is the up key if (pressedKeys[i] == Keys.Up || pressedKeys[i] == Keys.W) { //Move the camera up mapCreatorPos.Y -= (Human.HUMAN_SPEED * 5); } //If the current key is the down key else if (pressedKeys[i] == Keys.Down || pressedKeys[i] == Keys.S) { //Move the camera down mapCreatorPos.Y += (Human.HUMAN_SPEED * 5); } //If the current key is the left key else if (pressedKeys[i] == Keys.Left || pressedKeys[i] == Keys.A) { //Move the camera to the left mapCreatorPos.X -= (Human.HUMAN_SPEED * 5); } //If the current key is the right key else if (pressedKeys[i] == Keys.Right || pressedKeys[i] == Keys.D) { //Move the camera to the right mapCreatorPos.X += (Human.HUMAN_SPEED * 5); } //If the X position is less then 0 if (mapCreatorPos.X < 0) { //Set the position equal to 0 mapCreatorPos.X = 0; } //If the X position is greater then the screen size if (mapCreatorPos.X > ((Rectangle)camera.GetLimits()).Width) { //Sets the position to the screen size mapCreatorPos.X = ((Rectangle)camera.GetLimits()).Width; } //If the Y position is less then 0 if (mapCreatorPos.Y < 0) { //Set the position equal to 0 mapCreatorPos.Y = 0; } //If the Y position is greater then the screen size if (mapCreatorPos.Y > ((Rectangle)camera.GetLimits()).Height) { //Sets the position to the screen size mapCreatorPos.Y = ((Rectangle)camera.GetLimits()).Height; } //Looks at the position camera.LookAt(mapCreatorPos); //If the key is something that can be used if (CheckOldKeys(i, keysToCheck)) { //If the current key is the one key if (pressedKeys[i] == Keys.NumPad1 || pressedKeys[i] == Keys.D1) { //Sets the tile that is currently used CurrentTileType = TileType.Blank; PlacingEnemy = false; } //If the current key is the two key else if (pressedKeys[i] == Keys.NumPad2 || pressedKeys[i] == Keys.D2) { //Sets the tile that is currently used CurrentTileType = TileType.Save; PlacingEnemy = false; } //If the current key is the three key else if (pressedKeys[i] == Keys.NumPad3 || pressedKeys[i] == Keys.D3) { //Sets the tile that is currently used CurrentTileType = TileType.Spawn; PlacingEnemy = false; } //If the current key is the four key else if (pressedKeys[i] == Keys.NumPad4 || pressedKeys[i] == Keys.D4) { //Sets the tile that is currently used CurrentTileType = TileType.Wall; PlacingEnemy = false; } //If the current key is the 5 key else if (pressedKeys[i] == Keys.NumPad5 || pressedKeys[i] == Keys.D5) { //Sets the enemy to be placing PlacingEnemy = true; } //If the current key is the escape key else if (pressedKeys[i] == Keys.Escape) { //Sets the game state gameState = GameState.SaveCreatorMap; } //If the current key is the H key else if (pressedKeys[i] == Keys.H) { //Sets the game state gameState = GameState.MapCreatorHelp; } } } //Set the previously pressed keys to the currently pressed keys prevPressedKeys = pressedKeys; //Returns the current game state return(gameState); }
protected override void Update(GameTime gameTime) { gameTimePublic = gameTime; if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } penumbra.Transform = playerCam.GetTransformation(); if (runOnce) { worlds[0].initialize(); ToolBelt.Initialize(); runOnce = false; } if (kb.IsKeyDown(Keys.Q)) { renderSize += 10; } else if (kb.IsKeyDown(Keys.E)) { playerCam.SetZoom(3f); renderSize = 600; } Zoom(); kb = Keyboard.GetState(); mouse = Mouse.GetState(); worlds[0].Update(); Player.Update(); ToolBelt.Update(); playerCam.LookAt(Player.rec); // TODO: Add your update logic here if (kb.IsKeyDown(Keys.R) && kbPre.IsKeyUp(Keys.R)) { if (penumbra.Visible == false) { penumbra.Visible = true; } else { penumbra.Visible = false; } } frames++; mousePre = mouse; kbPre = kb; base.Update(gameTime); }
/// <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); }