/// <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 state = Keyboard.GetState(); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (state.IsKeyDown(Keys.Enter)) { gameStarted = true; } if (gameStarted) { mapGenerator.Update(gameTime); if (state.IsKeyDown(Keys.Left)) { //player.Position = new Vector2(player.Position.X - scale, GraphicsDevice.Viewport.Height - (player.Size.Y)); player.MoveLeft(mapScale); } if (state.IsKeyDown(Keys.Right)) { //player.Position = new Vector2(player.Position.X + scale, GraphicsDevice.Viewport.Height - (player.Size.Y)); player.MoveRight(mapScale); } foreach (Plane enemy in enemies) { enemy.MoveForward(mapSpeed); } //if (state.IsKeyDown(Keys.Up)) // player.MoveForward(scale); //if (state.IsKeyDown(Keys.Down)) // player.MoveBackward(scale); if (state.IsKeyDown(Keys.Space)) { if (gameTime.TotalGameTime.CompareTo(player.LastBulletTime) >= 0) { player.AddBullet(); player.LastBulletTime = gameTime.TotalGameTime.Add(new TimeSpan(0, 0, 0, 0, 700)); } } for (int i = 0; i < player.Bullets.Count; i++) { player.Bullets[i].Position = new Vector2(player.Bullets[i].Position.X, player.Bullets[i].Position.Y - player.Bullets[i].Speed); if (enemies.Remove(enemies.Where(x => x.Intersects(player.Bullets[i].Rectangle)).FirstOrDefault())) { player.Bullets.Remove(player.Bullets[i]); } else if (player.Bullets[i].Position.Y + player.Bullets[i].Size.Y < 0) { player.Bullets.Remove(player.Bullets[i]); } } if (mapGenerator.Intersects(player)) { LoadGame(); } for (int i = 0; i < enemies.Count; i++) { if (enemies[i].Position.Y > GraphicsDevice.Viewport.Height) { enemies.Remove(enemies[i]); } else if (mapGenerator.Intersects(enemies[i])) { enemies.Remove(enemies[i]); } } foreach (Plane enemy in enemies) { int leftWall = mapGenerator.leftSide.Select(n => new { n, distance = Math.Abs(n.Top - enemy.Center.Y) }).OrderBy(p => p.distance).First().n.Right, rightWall = mapGenerator.rightSide.Select(n => new { n, distance = Math.Abs(n.Top - enemy.Center.Y) }).OrderBy(p => p.distance).First().n.Left; int danger = HelperClass.CalculateDanger(new Vector2(enemy.Center.X, enemy.Position.Y + enemy.Size.Y), player.Bullets, leftWall, rightWall); int newDanger = HelperClass.CalculateDanger(new Vector2(enemy.Center.X + enemySpeed, enemy.Center.Y), player.Bullets, leftWall, rightWall); if (danger < newDanger) { enemy.MoveLeft(enemySpeed); } else if (danger > newDanger) { enemy.MoveRight(enemySpeed); } } if (enemies.Count < 4) { if (gameTime.TotalGameTime.CompareTo(lastEnemyTime) >= 0) { enemies.Add(new Plane(this, new Vector2(enemySpawnChance.Next(mapGenerator.LastLeftRectangle.Right + 60, mapGenerator.LastRightRectangle.Left - 100), -60), false));// lastEnemyTime = gameTime.TotalGameTime.Add(new TimeSpan(0, 0, 0, 0, enemySpawnChance.Next(300, 1400))); } } } oldState = state; base.Update(gameTime); }