public override void Update(GameTime gameTime) { PlayerInput.Update(); if (PlayerInput.anyPressed()) { Game1.levelManager.setLevel(4); } displayGraphicsQuality(); sTriangle.Update(gameTime); buttonMinus.Update(); buttonPlus.Update(); buttonMed.Update(); buttonLow.Update(); buttonMedium.Update(); buttonHigh.Update(); }
/// <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) { // TODO: Add your update logic here InputManager.Instance.Update(); //deltaTime is in seconds, so calculations = pixels per second. deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; //Press enter to start game/level if (!startGame) { if (InputManager.Instance.KeyPressed(Keys.Enter)) { startGame = true; } return; } timer += deltaTime; //update player spritePlayer.Update(gameTime); //update backgrounds scrollBack.Update(gameTime); scrollFore.Update(gameTime); scrollBack.setScrollSpeed(playerSpeeeeeed - 2); scrollFore.setScrollSpeed(playerSpeeeeeed); //limit scroll speed if (playerSpeeeeeed > -1) { playerSpeeeeeed = -1; } else if (playerSpeeeeeed < -10) { playerSpeeeeeed = -10; } //Every 2 seconds, spawn an enemy randomly. if (timer > spawnInterval) { AddEnemy(enemyList); timer = 0; } //inputs if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (InputManager.Instance.KeyPressed(Keys.B)) { showbb = !showbb; } //PLAYER MOVEMENT if (InputManager.Instance.KeyDown(Keys.Left)) { if (spritePlayer.getPosX() > playArea.Left) { spritePlayer.setPosX(spritePlayer.getPosX() - playerSpeed); } playerSpeeeeeed += 0.1f; } if (InputManager.Instance.KeyDown(Keys.Right)) { if (spritePlayer.getPosX() < playArea.Right - spritePlayer.getWidth()) { spritePlayer.setPosX(spritePlayer.getPosX() + playerSpeed); } playerSpeeeeeed += -0.1f; } if (InputManager.Instance.KeyDown(Keys.Up)) { if (spritePlayer.getPosY() > playArea.Bottom) { spritePlayer.setPosY(spritePlayer.getPosY() - playerSpeed); } } if (InputManager.Instance.KeyDown(Keys.Down)) { if (spritePlayer.getPosY() < playArea.Top - spritePlayer.getHeight()) { spritePlayer.setPosY(spritePlayer.getPosY() + playerSpeed); } } //PLAYER SHOOT if (InputManager.Instance.KeyPressed(Keys.Space)) { AddBullet((int)spritePlayer.getPosX(), (int)spritePlayer.getPosY()); } //Update all enemies and bullets to move. also inactive if outofbounds //In the past I called this instead of having an extended class with custom update // enemyList.moveDeltaX(-3); //Update/remove enemies enemyList.Update(gameTime); enemyList.removeIfOutside(new Rectangle(-100, -100, SCREEN_WIDTH + 200, SCREEN_HEIGHT + 200)); //update/remove bullets playerBulletList.moveDeltaX(5); playerBulletList.removeIfOutside(new Rectangle(-100, -100, SCREEN_WIDTH + 200, SCREEN_HEIGHT + 200)); //If player collides with enemy int collisionIndex = enemyList.collisionAA(spritePlayer); if (collisionIndex != -1) { Sprite3 temp = enemyList.getSprite(collisionIndex); //player collides with boss if (temp.getName() == "boss") { } else { //enemy dies? temp.active = false; temp.visible = false; AddExplosion((int)temp.getPosX(), (int)temp.getPosY()); score -= 5; textScore.text = "Score : " + score.ToString(); } } //if a bullet collides with enemy for (int i = 0; i < playerBulletList.count(); i++) { Sprite3 currentBullet = playerBulletList.getSprite(i); if (currentBullet == null) { continue; } if (currentBullet.active != true) { continue; } if (currentBullet.visible != true) { continue; } //if collision success int collision = enemyList.collisionAA(currentBullet); if (collision != -1) { Sprite3 currentEnemy = enemyList.getSprite(collision); //kill both enemy and bullet currentEnemy.active = false; currentEnemy.visible = false; currentBullet.active = false; currentBullet.visible = false; //add particle AddExplosion((int)currentEnemy.getPosX(), (int)currentEnemy.getPosY()); //add score score += 10; textScore.text = "Score : " + score.ToString(); } } //update particles particleList.animationTick(gameTime); base.Update(gameTime); }