/// <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) { KeyboardState ks = Keyboard.GetState(); GamePadState gs = GamePad.GetState(PlayerIndex.One); // For Mobile devices, this logic will close the Game when the Back button is pressed if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { Exit (); } if(ks.IsKeyDown(Keys.Escape)) this.Exit(); if (gameState == GameState.Menu) { if ((ks.IsKeyDown(Keys.Enter) || gs.Buttons.Start == ButtonState.Pressed) && menu.state == Menu.arrowKey.start) { gameState = GameState.Game; Pause.pauseKeyDown = true; } if ((ks.IsKeyDown(Keys.Enter) || gs.Buttons.Start == ButtonState.Pressed) && menu.state == Menu.arrowKey.credits) { gameState = GameState.Credits; } menu.Update(gameTime); } if (gameState == GameState.Game || gameState == GameState.Alert) { // Check to see if the user has paused or unpaused Pause.checkPauseKey(ks, gs); Pause.checkPauseGuide(); // If the user hasn't paused, Update normally if (!Pause.paused) { if (ks.IsKeyDown(Keys.Q)) { for (int i = 0; i <= currentLevel.itemLocation.Length - 1; i++) { for (int j = -1; j <= 1; j++) { for (int k = -1; k <= 1; k++) { if (player.tileX + j == currentLevel.itemLocation[i].X && player.tileY + k == currentLevel.itemLocation[i].Y) { player.addGold(); currentLevel.currentMap.mapCell[currentLevel.itemLocation[i].X, currentLevel.itemLocation[i].Y].RemoveBaseTile(229); currentLevel.currentMap.mapCell[currentLevel.itemLocation[i].X, currentLevel.itemLocation[i].Y].RemoveObject("Gold2"); sound.coinSound.Play(); currentLevel.itemLocation[i] = new Point(-1, -1); } } } } } player.Update(currentLevel.currentMap, squaresAcross, squaresDown, gameTime, drawnRectangles); //check to see if there are no guards left if (currentLevel.guards.ElementAt(currentLevel.currentMapIndex).Count <= 0) { gameState = GameState.Game; } for (int i = 0; i < currentLevel.guards.ElementAt(currentLevel.currentMapIndex).Count; i++) { List<Npc> guardList = currentLevel.guards.ElementAt(currentLevel.currentMapIndex); if (guardList.ElementAt(i).isColliding(player) && !ks.IsKeyDown(Keys.Tab)) { gameOver = new GameOverScreen(this); gameOver.initialize(blackTexture, gameOverFont, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, bestScore.ToString(), "Sorry Tim."); gameState = GameState.Over; break; } if (guardList.ElementAt(i).ISeeYou) { //Console.WriteLine(guard.objectID); gameState = GameState.Alert; } if (guardList.ElementAt(i).isDead()) { currentLevel.guards.ElementAt(currentLevel.currentMapIndex).RemoveAt(i); sound.fallSound.Play(); } else currentLevel.guards.ElementAt(currentLevel.currentMapIndex).ElementAt(i).Update(player.whereOnTile, gameTime); } // If all guards can't see me change gamestate from alert to game int count = 0; foreach (Npc guard in currentLevel.guards.ElementAt(currentLevel.currentMapIndex)) { if (!guard.ISeeYou) { count++; } if (count >= currentLevel.guards.ElementAt(currentLevel.currentMapIndex).Count) { gameState = GameState.Game; } } if (player.isDead()) { sound.weakScreamSound.Play(); gameOver = new GameOverScreen(this); gameOver.initialize(blackTexture, gameOverFont, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, bestScore.ToString(), "Sorry Tim."); gameState = GameState.Over; } timer += gameTime.ElapsedGameTime.Milliseconds; if (gameState == GameState.Alert) { if (MediaPlayer.Queue.ActiveSong != sound.Alert) { MediaPlayer.Play(sound.Alert); waitTimer = timer; } } if (gameState == GameState.Game) { if (MediaPlayer.State != MediaState.Playing || (MediaPlayer.Queue.ActiveSong == sound.Alert && timer > waitTimer + 9000f)) { waitTimer = 0; MediaPlayer.Play(sound.normalMusic); } } if (player.tileX == currentLevel.currentGoal.X && player.tileY == currentLevel.currentGoal.Y && currentLevel.currentMapIndex + 1 < currentLevel.winGoal) { currentLevel.currentMapIndex++; currentLevel.currentMap = currentLevel.maps[currentLevel.currentMapIndex]; currentLevel.currentGoal = currentLevel.goal[currentLevel.currentMapIndex]; int currNumGold = player.numGold; player.initialize(start, 0, timTexture, currentLevel.start[currentLevel.currentMapIndex].X, currentLevel.start[currentLevel.currentMapIndex].Y, "Player", currentLevel.currentMap); player.numGold = currNumGold; // player.Location.X = currentLevel.start[currentLevel.currentMapIndex].X * tileSize - (6 * tileSize); //player.Location.Y = currentLevel.start[currentLevel.currentMapIndex].Y * tileSize - (4 * tileSize); loadGuards("lvl1map" + (currentLevel.currentMapIndex + 1) + "guards.csv"); } else if (player.tileX == currentLevel.currentGoal.X && player.tileY == currentLevel.currentGoal.Y && currentLevel.currentMapIndex + 1 == currentLevel.winGoal) { gameOver = new GameOverScreen(this); int score = ((int)hud.timer / 1000); if (score < bestScore) bestScore = score; gameOver.initialize(blackTexture, gameOverFont, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, bestScore.ToString(), "Congratulations"); gameState = GameState.Over; } hud.Update(gameTime); } } if (gameState == GameState.Over) { MediaPlayer.Stop(); if (ks.IsKeyDown(Keys.Enter) || gs.Buttons.Start == ButtonState.Pressed) { gameState = GameState.Game; Pause.pauseKeyDown = true; this.Initialize(); } } if (gameState == GameState.Credits) { credits.Update(gameTime); if (ks.IsKeyDown(Keys.Space) || gs.Buttons.Y == ButtonState.Pressed) gameState = GameState.Menu; } // TODO: Add your update logic here base.Update (gameTime); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // +2 to compensate for tiles off screen squaresAcross = GraphicsDevice.Viewport.Width / 64 + 2; squaresDown = GraphicsDevice.Viewport.Height / 64 + 2; SpriteFont font = Content.Load<SpriteFont>("SpriteFont1"); Texture2D torchTexture = Content.Load<Texture2D> ("torch"); Texture2D gameOverTexture = Content.Load<Texture2D>("GameOver"); Texture2D hudTexture = Content.Load<Texture2D>("hud"); hud = new hud(this); hud.initialize(hudTexture, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, GraphicsDevice.Viewport.Width - 50, GraphicsDevice.Viewport.Height - 50,font); gameOver = new GameOverScreen(this); gameOver.initialize(gameOverTexture, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); gameState = GameState.Game; //Load guards loadGuards(); Vector2 start = new Vector2(GraphicsDevice.Viewport.Width/2, GraphicsDevice.Viewport.Height/2); Texture2D texture = Content.Load<Texture2D>("sprite"); player = new Player(this); player.initialize(start, 0, texture, 10, 10, "Player", myMap); testObject = new torch(this); testObject.initialize(myMap, 1, 1, 0, 0, torchTexture, "torch"); base.Initialize (); }