public override void Update(GameTime gameTime) { base.Update(gameTime); Gliding = false; Sinking = false; Moving = false; if (Timer > 0) { Timer -= gameTime.ElapsedGameTime.TotalSeconds; Color = Color.Red; } else { Timer = 0; Color = Color.White; } if (Keyboard.GetState().IsKeyDown(Keys.Right)) { animationState = AnimationEnum.Walking; LastFacing = Facing.Right; Direction.X = 1f; Effects = SpriteEffects.None; } else if (Keyboard.GetState().IsKeyDown(Keys.Left)) { animationState = AnimationEnum.Walking; LastFacing = Facing.Left; Direction.X = -1f; Effects = SpriteEffects.FlipHorizontally; } else { animationState = AnimationEnum.Standing; Direction.X = 0f; Velocity.X = 0f; } if (!Airborn && KeyMouseReader.KeyPressed(Keys.Up)) { Velocity.Y = -12; Airborn = true; } while (Airborn) { animationState = AnimationEnum.Jumping; break; } if (KeyMouseReader.KeyPressed(Keys.Space)) { ChangeFrameShooting = true; } if (Health <= 0) { NogardGame.GameOverManager.Lose(); } if (Velocity.Y >= 60) { Health = 0; } Velocity.Y += GravitySpeed; Velocity += Direction * (Speed / Math.Max(1, Math.Abs(Velocity.X))) * (float)gameTime.ElapsedGameTime.TotalSeconds; Velocity = new Vector2(MathHelper.Clamp(Velocity.X, -3, 3), MathHelper.Clamp(Velocity.Y, -20, 60)); frameTimer -= gameTime.ElapsedGameTime.TotalMilliseconds; PlayerFrames(gameTime); }
protected override void Update(GameTime gameTime) { KeyMouseReader.Update(); if (!gameOverSound && gameOver) { MediaPlayer.Stop(); MediaPlayer.IsRepeating = false; MediaPlayer.Play(gameOver_sfx); gameOverSound = true; } if (gameOver) { playerinfo = "Press enter to restart"; showPlayerInfo = true; if (Keyboard.GetState().IsKeyDown(Keys.Enter)) { Initialize(); } } if (KeyMouseReader.KeyPressed(Keys.P)) { paused = !paused; } if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } if (showPlayerInfo) { playerinfoTime += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (playerinfoTime > 9) { showPlayerInfo = false; playerinfoTime = 0; } audioEngine.Update(); if (gameOver || paused) { return; } this.Window.Title = "AsteroidKiller Score: " + playerScore.ToString(); player.Update(); bg1.Update(); bg2.Update(); bg3.Update(); //Difficulty diffLevel = playerTime1 / 15 + playerScore / 10000; maxAsteroids = 10 + diffLevel * 3; rMinSpeed = 1 + diffLevel / 8; rMaxSpeed = 4 + diffLevel / 4; if (SpawnRate > 1) { SpawnRate = 200 - diffLevel * 7; } if (SpawnRate < 1) { SpawnRate = 1; } //Bullet logic foreach (Bullet bullet in bullets) { bullet.Update(); foreach (Asteroid asteroid in asteroids) { asteroid.Collision(bullet.hitbox); if (!piercingBullets) { bullet.Collision(asteroid.hitbox); } } } if (KeyMouseReader.LeftClickPressed() && nextShot >= 60 && !player.dead) { ShootBullet(); nextShot = 0; } if (nextShot < 60) { nextShot += shotRate; } for (int i = bullets.Count - 1; i >= 0; i--) { if (bullets[i].bulletHitAsteroid) { playerScore += 100; } if (bullets[i].removeBullet) { bullets.RemoveAt(i); } } //Asteroid Logic foreach (Asteroid asteroid in asteroids) { asteroid.Update(); player.Collision(asteroid.hitbox); } foreach (Asteroid asteroid in asteroids) { foreach (Asteroid asteroid2 in asteroids) { if (asteroid2 != asteroid && asteroid.scale > 0.7 && asteroid2.scale > 0.7) //Collision with other asteroids { asteroid.Collision(asteroid2.hitbox); asteroid2.Collision(asteroid.hitbox); } } if (asteroid.destroyAsteroid) { asteroid.texture = explosion_texture; } } foreach (Asteroid asteroid in asteroids) { if (asteroid.spawnDaughters) //Logic for spawning "daughter" asteroids { rHitbox = new Rectangle((int)asteroid.pos.X, (int)asteroid.pos.Y, asteroid_texture.Width, asteroid_texture.Height); Vector2 daughterSpeed, daughterSpeed2; daughterSpeed = new Vector2(asteroid.speed.X, rand.Next(-2, 2)); daughterSpeed2 = new Vector2(rand.Next(-2, 2), asteroid.speed.Y); asteroids.Add(new Asteroid(asteroid_texture, asteroid.pos, daughterSpeed, rHitbox, asteroid.scale / 2)); asteroids.Add(new Asteroid(asteroid_texture, asteroid.pos, daughterSpeed2, rHitbox, asteroid.scale / 2)); asteroid.spawnDaughters = false; break; } } nextSpawn += rand.Next(1, 5); if (nextSpawn >= SpawnRate && asteroids.Count < maxAsteroids) { SpawnRandom(); //Randomize position/speed of asteroid rScale = (11 - rand.Next(1, 4)) * 0.12f; //Randomize size of asteroid rHitbox = new Rectangle((int)rPos.X, (int)rPos.Y, asteroid_texture.Width, asteroid_texture.Height); asteroids.Add(new Asteroid(asteroid_texture, rPos, rSpeed, rHitbox, rScale)); nextSpawn = 0; } for (int i = asteroids.Count - 1; i >= 0; i--) //Goes through list in reverse order to avoid rearrangement in list { if (asteroids[i].destroyAsteroid && asteroids[i].animationTime > 10) { if (asteroidSounds < 3) //Prevents sound overlap from more than 3 explosions on the same frame { soundBank.PlayCue("boom6"); ++asteroidSounds; } asteroids.RemoveAt(i); } } asteroidSounds = 0; //Player Logic if (player.dead) { player.texture = player_explode; if (player.animationTime == 1) { soundBank.PlayCue("boom1"); } if (player.animationTime >= 100) //Checks to see whether animation has ended { player.pos = playerStartPos; player.speed = Vector2.Zero; player.texture = player_texture; playerLives -= 1; player.invulnerable = true; player.dead = false; } if (player.animationTime >= 100) { player.animationTime = 0; } } if (playerLives <= 0) { gameOver = true; } if (player.invulnerable) { playerInvulnerableCount += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (playerInvulnerableCount >= playerInvulnerableTime) { player.invulnerable = false; playerInvulnerableCount = 0; } playerTime += (float)gameTime.ElapsedGameTime.TotalSeconds; playerTime1 = (int)playerTime; //Powerup logic powerup.Update(); if (!powerupExists) { powerupCounter += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (powerupCounter >= nextPowerup) { SpawnRandom(); rHitbox = new Rectangle((int)rPos.X, (int)rPos.Y, powerup_texture.Width, powerup_texture.Height); powerup = new Powerup(powerup_texture, rPos, rSpeed * 0.5f, rHitbox, rand.Next(1, 6)); powerupExists = true; nextPowerup = rand.Next(10, 20); powerupCounter = 0; } powerup.Collision(player.hitbox); if (powerup.playerGotPowerup) { switch (powerup.id) { case 1: playerLives += 1; DisplayPlayerInfo(" Life up!"); soundBank.PlayCue("se_bonus"); break; case 2: shotRate = 14; powerupTime = 0; powerupActive = true; DisplayPlayerInfo(" Fire rate doubled!"); soundBank.PlayCue("se_ch02"); break; case 3: foreach (Asteroid asteroid in asteroids) { asteroid.destroyAsteroid = true; } DisplayPlayerInfo(" KABOOM"); soundBank.PlayCue("se_slash"); break; case 4: player.invulnerable = true; playerInvulnerableCount = -7; DisplayPlayerInfo(" Invulnerability!"); soundBank.PlayCue("se_chargeup"); break; case 5: piercingBullets = true; shotRate = 5; powerupTime = 0; powerupActive = true; DisplayPlayerInfo(" Asteroids are made of cheese!"); soundBank.PlayCue("se_charge00"); break; } powerup = new Powerup(powerup_texture, Vector2.Zero, Vector2.Zero, new Rectangle(-10, -10, 0, 0), 0); powerupExists = false; } if (powerupActive) { powerupTime += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (powerupTime >= 10) { shotRate = 7; piercingBullets = false; powerupActive = false; powerupTime = 0; } 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) { KeyMouseReader.Update(); if (KeyMouseReader.RightClick()) { camera.Update(new Vector2(KeyMouseReader.mouseState.X - camera.CameraPos.X, KeyMouseReader.mouseState.Y - camera.CameraPos.Y)); } grid.Update(new Vector2(KeyMouseReader.mouseState.X, KeyMouseReader.mouseState.Y)); if (navigateTabs == NavigationTabs.ChooseObject) { if (chooseObject == ChooseObject.Player) { grid.tileType = TileGrid.TileType.AddPlayer; } else if (chooseObject == ChooseObject.Wall) { grid.tileType = TileGrid.TileType.AddWall; } else if (chooseObject == ChooseObject.Teleport) { grid.tileType = TileGrid.TileType.AddTeleport; } else if (chooseObject == ChooseObject.EnemySpawner) { grid.tileType = TileGrid.TileType.AddSpawner; } else if (chooseObject == ChooseObject.Pistol) { grid.tileType = TileGrid.TileType.AddPistol; } } hud.Update(camera.CameraPos); if (navigateTabs == NavigationTabs.SetGrid) { SetGrid(); } else if (navigateTabs == NavigationTabs.ChooseTool && chooseTool == ChooseTools.SaveLevel) { grid.SaveToJsonFile(); Console.WriteLine("SAVED"); chooseTool = ChooseTools.AddTiles; hud.selected = 0; } switch (navigateTabs) { case NavigationTabs.ChooseTool: if (KeyMouseReader.KeyPressed(Keys.Escape)) { navigateTabs = NavigationTabs.SetGrid; } if (KeyMouseReader.KeyPressed(Keys.Enter)) { if (hud.selected == 0) { chooseTool = ChooseTools.AddTiles; navigateTabs = NavigationTabs.ChooseCategory; } else if (hud.selected == 1) { chooseTool = ChooseTools.RemoveTiles; } else if (hud.selected == 2) { chooseTool = ChooseTools.SaveLevel; } hud.selected = 0; } break; case NavigationTabs.ChooseCategory: if (KeyMouseReader.KeyPressed(Keys.Escape)) { navigateTabs = NavigationTabs.ChooseTool; } if (KeyMouseReader.KeyPressed(Keys.Enter)) { if (hud.selected == 0) { chooseCategory = ChooseCategory.Characters; } else if (hud.selected == 1) { chooseCategory = ChooseCategory.Environment; } else if (hud.selected == 2) { chooseCategory = ChooseCategory.Weapons; } hud.selected = 0; navigateTabs = NavigationTabs.ChooseObject; } break; case NavigationTabs.ChooseObject: if (KeyMouseReader.KeyPressed(Keys.Escape)) { navigateTabs = NavigationTabs.ChooseCategory; } if (KeyMouseReader.KeyPressed(Keys.Enter)) { if (chooseCategory == ChooseCategory.Characters) { if (hud.selected == 0) { chooseObject = ChooseObject.Player; } else if (hud.selected == 1) { chooseObject = ChooseObject.EnemySpawner; } else if (hud.selected == 2) { chooseObject = ChooseObject.Enemy; } } else if (chooseCategory == ChooseCategory.Environment) { if (hud.selected == 0) { chooseObject = ChooseObject.Wall; } else if (hud.selected == 1) { chooseObject = ChooseObject.Teleport; } } else if (chooseCategory == ChooseCategory.Weapons) { if (hud.selected == 0) { chooseObject = ChooseObject.Pistol; } } } break; } // TODO: Add your update logic here base.Update(gameTime); }