/// <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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // get current mouse state and update burger KeyboardState currentState = Keyboard.GetState(); if (burger.getHealth() > 0) { burger.Update(gameTime, currentState); } // update other game objects foreach (TeddyBear bear in bears) { bear.Update(gameTime); } foreach (Projectile projectile in projectiles) { projectile.Update(gameTime); } foreach (Explosion explosion in explosions) { explosion.Update(gameTime); } // check and resolve collisions between teddy bears foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { if ((bear.CollisionRectangle.X + bear.CollisionRectangle.Size.X > projectile.CollisionRectangle.X) && (bear.CollisionRectangle.X < projectile.CollisionRectangle.X) && (bear.CollisionRectangle.Y <= projectile.CollisionRectangle.Y) && (bear.CollisionRectangle.Y + bear.CollisionRectangle.Size.Y > projectile.CollisionRectangle.Y)) { if (projectile.Type == ProjectileType.FrenchFries) { bear.Active = false; projectile.Active = false; Explosion newExplosion = new Explosion(explosionSpriteStrip, bear.CollisionRectangle.X, bear.CollisionRectangle.Y, explosion); explosions.Add(newExplosion); score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; } } } if (bear.Active && ((bear.CollisionRectangle.X + bear.CollisionRectangle.Size.X > burger.CollisionRectangle.X) && (bear.CollisionRectangle.X < burger.CollisionRectangle.X) && (bear.CollisionRectangle.Y <= burger.CollisionRectangle.Y) && (bear.CollisionRectangle.Y + bear.CollisionRectangle.Size.Y > burger.CollisionRectangle.Y))) { burger.setHealth(burger.getHealth() - GameConstants.BearDamage); bear.Active = false; Explosion bearExploded = new Explosion(explosionSpriteStrip, bear.CollisionRectangle.X, bear.CollisionRectangle.Y, explosion); explosions.Add(bearExploded); healthString = GameConstants.HealthPrefix + burger.getHealth(); burgerDamage.Play(); CheckBurgerKill(); } } for (int i = explosions.Count - 1; i > 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } // check and resolve collisions between burger and teddy bears // check and resolve collisions between burger and projectiles foreach (Projectile bearShot in projectiles) { if ((bearShot.Type == ProjectileType.TeddyBear) && (burger.CollisionRectangle.X + burger.CollisionRectangle.Size.X > bearShot.CollisionRectangle.X) && (burger.CollisionRectangle.X < bearShot.CollisionRectangle.X) && (burger.CollisionRectangle.Y <= bearShot.CollisionRectangle.Y) && (burger.CollisionRectangle.Y + burger.CollisionRectangle.Size.Y > bearShot.CollisionRectangle.Y)) { burger.setHealth(burger.getHealth() - GameConstants.BearDamage); bearShot.Active = false; Explosion bearShotExplosion = new Explosion(explosionSpriteStrip, bearShot.CollisionRectangle.X, bearShot.CollisionRectangle.Y, explosion); explosions.Add(bearShotExplosion); healthString = GameConstants.HealthPrefix + burger.getHealth(); burgerDamage.Play(); CheckBurgerKill(); } } for (int i = 0; i < bears.Count; i++) { TeddyBear thisBear = bears[i]; for (int j = i + 1; j < bears.Count; j++) { TeddyBear thatBear = bears[j]; if (thisBear.Active && thatBear.Active) { CollisionResolutionInfo collisionResolution = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, thisBear.Velocity, thisBear.DrawRectangle, thatBear.Velocity, thatBear.DrawRectangle); if (collisionResolution != null) { if (collisionResolution.FirstOutOfBounds) { thisBear.Active = false; } else { thisBear.Velocity = collisionResolution.FirstVelocity; thisBear.DrawRectangle = collisionResolution.FirstDrawRectangle; thatBear.Velocity = collisionResolution.SecondVelocity; thatBear.DrawRectangle = collisionResolution.SecondDrawRectangle; teddyBounce.Play(); } if (collisionResolution.SecondOutOfBounds) { thatBear.Active = false; } else { thatBear.Velocity = collisionResolution.SecondVelocity; thatBear.DrawRectangle = collisionResolution.SecondDrawRectangle; thisBear.Velocity = collisionResolution.FirstVelocity; thisBear.DrawRectangle = collisionResolution.FirstDrawRectangle; } } } } } // check and resolve collisions between teddy bears and projectiles // clean out inactive teddy bears and add new ones as necessary for (int i = bears.Count - 1; i >= 0; i--) { if (!bears[i].Active) { bears.RemoveAt(i); while (bears.Count < GameConstants.MaxBears) { SpawnBear(); } } } // clean out inactive projectiles for (int i = projectiles.Count - 1; i >= 0; i--) { if (!projectiles[i].Active) { projectiles.RemoveAt(i); } } // clean out finished explosions base.Update(gameTime); }