/// <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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // get current keyboard state and update burger KeyboardState keyboard = Keyboard.GetState(); burger.Update(gameTime, keyboard); // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) { //Getting collision info CollisionResolutionInfo CRI = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT, bears[i].Velocity, bears[i].CollisionRectangle, bears[j].Velocity, bears[j].CollisionRectangle); //checking for collision if (CRI != null) { //play bounce sound teddyBounce.Play(); //dealing with first teddy (i) if (CRI.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = CRI.FirstVelocity; bears[i].DrawRectangle = CRI.FirstDrawRectangle; } //dealing with second teddy (j) if (CRI.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = CRI.SecondVelocity; bears[j].DrawRectangle = CRI.SecondDrawRectangle; } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear teddy in bears) { //if burger and teddy collide if (teddy.Active && burger.CollisionRectangle.Intersects(teddy.CollisionRectangle) && burger.Health > 0) { //play sound burgerDamage.Play(); //decrease burger health and change health score burger.Health -= GameConstants.BEAR_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Health; if (!burgerDead) { CheckBurgerKill(); } //destroy teddy and explode him Explosion explosion = new Explosion(explosionSpriteStrip, teddy.Location.X, teddy.Location.Y, explosionSound); explosions.Add(explosion); teddy.Active = false; } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { //if projectile is an Active teddy bear and collides with burger if (projectile.Active && projectile.Type == ProjectileType.TeddyBear && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle) && burger.Health > 0) { //destroy projectile projectile.Active = false; //decrease burger health and change health score burger.Health -= GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Health; if (!burgerDead) { CheckBurgerKill(); } } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.FrenchFries && projectile.Active && bear.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { //deactivate colliding bear and projectile bear.Active = false; projectile.Active = false; //increase score score += GameConstants.BEAR_POINTS; scoreString = GameConstants.SCORE_PREFIX + score; //create new exploson object and add to the list Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosionSound); explosions.Add(explosion); } } } // 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.MAX_BEARS) { 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // get current keyboard state and update burger KeyboardState keyboard = Keyboard.GetState(); burger.Update(gameTime, keyboard); // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo cri = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (cri != null) { // resolve collision if (cri.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = cri.FirstVelocity; bears[i].DrawRectangle = cri.FirstDrawRectangle; teddyBounce.Play(); } if (cri.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = cri.SecondVelocity; bears[j].DrawRectangle = cri.SecondDrawRectangle; } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { if (bear.CollisionRectangle.Intersects(burger.CollisionRectangle) && bear.Active) { bear.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion)); burger.health -= GameConstants.BearDamage; healthString = GameConstants.HealthPrefix + burger.health; burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle) && projectile.Active) { projectile.Active = false; burger.health -= GameConstants.BearDamage; healthString = GameConstants.HealthPrefix + burger.health; burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.FrenchFries && bear.Active && projectile.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { bear.Active = false; projectile.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion)); explosion.Play(); score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; } } } // 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); 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { KeyboardState keyboard = Keyboard.GetState(); // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || keyboard.IsKeyDown(Keys.Escape)) { this.Exit(); } // get current mouse state and update burger //MouseState mouse = Mouse.GetState(); //burger.Update(gameTime, mouse); burger.Update(gameTime, keyboard); // 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 for (var i = 0; i < bears.Count; i++) { for (var j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo colision = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (colision != null) { teddyBounce.Play(); if (colision.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].DrawRectangle = colision.FirstDrawRectangle; bears[i].Velocity = colision.FirstVelocity; } if (colision.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].DrawRectangle = colision.SecondDrawRectangle; bears[j].Velocity = colision.SecondVelocity; } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { if (bear.Active && bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Healt = burger.Healt - GameConstants.BEAR_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Healt; CheckBurgerKill(); bear.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y, explosion)); burgerDamage.Play(); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear && projectile.Active && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Healt = burger.Healt - GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Healt; CheckBurgerKill(); projectile.Active = false; burgerDamage.Play(); } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { if (bear.Active && projectile.Active && projectile.Type == ProjectileType.FrenchFries && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { bear.Active = false; projectile.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y, explosion)); score += GameConstants.BEAR_POINTS; scoreString = GameConstants.SCORE_PREFIX + score; } } } // 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); } } //add new bears while (bears.Count < GameConstants.MAX_BEARS) { 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // get current mouse state and update burger //MouseState mouse = Mouse.GetState(); KeyboardState keyboard = Keyboard.GetState(); if (keyboard.IsKeyDown(Keys.Escape)) { Exit(); } burger.Update(gameTime, keyboard); // 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 for (int i = 0; i < bears.Count(); ++i) { for (int j = i + 1; j < bears.Count(); ++j) { if (bears[i].Active && bears[j].Active) { // make collision detection CollisionResolutionInfo colInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (colInfo != null) { // update first teddy bear according to collision info if (colInfo.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = colInfo.FirstVelocity; bears[i].DrawRectangle = colInfo.FirstDrawRectangle; } // update second teddy bear according to collision info if (colInfo.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = colInfo.SecondVelocity; bears[j].DrawRectangle = colInfo.SecondDrawRectangle; } teddyBounce.Play(); } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { if (bear.Active && bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { // update burger health burger.Health -= GameConstants.BEAR_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Health; // play sound and check if burger is still alive burgerDamage.Play(); CheckBurgerKill(); bear.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y, explosion)); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Active && projectile.Type == ProjectileType.TeddyBear && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { // update burger health burger.Health -= GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Health; // play sound and check if burger is still alive burgerDamage.Play(); CheckBurgerKill(); projectile.Active = false; } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { // check if projectile type is correct and if both objects are active if (projectile.Type == ProjectileType.FrenchFries && projectile.Active && bear.Active) { // check if the objects intersect if (projectile.CollisionRectangle.Intersects(bear.CollisionRectangle)) { // update score score += GameConstants.BEAR_POINTS; scoreString = GameConstants.SCORE_PREFIX + score; // set projectile and bear to inactive and create explosion projectile.Active = false; bear.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y, explosion)); } } } } // clean out inactive teddy bears and add new ones as necessary // clean out inactive teddies for (int i = bears.Count() - 1; i >= 0; --i) { if (!bears[i].Active) { bears.RemoveAt(i); } } // spawn new teddies while (bears.Count() < GameConstants.MAX_BEARS) { 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 for (int i = explosions.Count() - 1; i >= 0; --i) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // get current keyboard state and update burger KeyboardState keyboard = Keyboard.GetState(); burger.Update(gameTime, keyboard); // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { TeddyBear oneTeddy = bears[i]; TeddyBear theOtherTeddy = bears[j]; if (oneTeddy.Active && theOtherTeddy.Active) { CollisionResolutionInfo collisionResolutionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, oneTeddy.Velocity, oneTeddy.DrawRectangle, theOtherTeddy.Velocity, theOtherTeddy.DrawRectangle); if (null != collisionResolutionInfo) { // first teddy collision if (collisionResolutionInfo.FirstOutOfBounds) { oneTeddy.Active = false; } else { oneTeddy.Velocity = collisionResolutionInfo.FirstVelocity; oneTeddy.DrawRectangle = collisionResolutionInfo.FirstDrawRectangle; // adding sound effect teddyBounce.Play(); } // second teddy collision if (collisionResolutionInfo.SecondOutOfBounds) { theOtherTeddy.Active = false; } else { theOtherTeddy.Velocity = collisionResolutionInfo.SecondVelocity; theOtherTeddy.DrawRectangle = collisionResolutionInfo.SecondDrawRectangle; // adding sound effect teddyBounce.Play(); } } } } } // check and resolve collisions between burger and teddy bears int initialBurguerHealth = burger.Health; foreach (TeddyBear bear in bears) { if (bear.Active && bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.BearDamage; bear.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y, explosion)); // adding sound effect burgerDamage.Play(); // checking if burger was killed CheckBurgerKill(); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Active && ProjectileType.TeddyBear.Equals(projectile.Type) && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { projectile.Active = false; burger.Health -= GameConstants.TeddyBearProjectileDamage; // adding sound effect burgerDamage.Play(); // checking if burger was killed CheckBurgerKill(); } } // if health has changed, update health string if (initialBurguerHealth != burger.Health) { UpdateHealthString(); } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { // if projectile is french frye and projectile collisions with teddy, deactivate both of them if (ProjectileType.FrenchFries.Equals(projectile.Type) && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { // inactive teddy and projectile projectile.Active = false; bear.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y, explosion)); // update score score += GameConstants.BearPoints; UpdateScoreString(); } } } // 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); } } // add bears 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.Remove(explosions[i]); } } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // get current mouse state and update burger MouseState mouse = Mouse.GetState(); burger.Update(gameTime, mouse); // 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); } foreach (var bear in bears) { foreach (var projectile in projectiles) { if (bear.Active && projectile.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { // check and resolve collisions between teddy bears and projectiles if (projectile.Type == ProjectileType.FrenchFries) { bear.Active = false; projectile.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y)); } } } } // check and resolve collisions between teddy bears for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo resolutionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (resolutionInfo != null) { if (resolutionInfo.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].DrawRectangle = resolutionInfo.FirstDrawRectangle; bears[i].Velocity = resolutionInfo.FirstVelocity; } if (resolutionInfo.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].DrawRectangle = resolutionInfo.SecondDrawRectangle; bears[j].Velocity = resolutionInfo.SecondVelocity; } } } } } // check and resolve collisions between burger and teddy bears foreach (var bear in bears) { if (bear.Active && bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.BearDamage; bear.Active = false; Explosion explosion = new Explosion(explosionSpriteStrip, bear.DrawRectangle.X, bear.DrawRectangle.Y); explosions.Add(explosion); } } // check and resolve collisions between burger and projectiles foreach (var projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear && projectile.Active && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.TeddyBearProjectileDamage; projectile.Active = false; } } // 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { // get current mouse\keyboard state and update burger KeyboardState currentKeyboardState = Keyboard.GetState(); MouseState mouse = Mouse.GetState(); if (burger != null) { burger.Update(gameTime, mouse, currentKeyboardState); } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || currentKeyboardState.IsKeyDown(Keys.Escape)) { Exit(); } // // update other game objects if (bears.Any()) { 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 if (bears.Any() && burger != null) { for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo collisitionResInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (collisitionResInfo != null) { // 1 bear check and bounce if needed if (collisitionResInfo.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = collisitionResInfo.FirstVelocity; bears[i].DrawRectangle = collisitionResInfo.FirstDrawRectangle; teddyBounce.Play(); } // 2 bear check and bounce if needed if (collisitionResInfo.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = collisitionResInfo.SecondVelocity; bears[j].DrawRectangle = collisitionResInfo.SecondDrawRectangle; teddyBounce.Play(); } } } } } } //a collision between the burger and each projectile if (bears.Any() && burger != null) { foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear && burger.CollisionRectangle.Intersects(projectile.CollisionRectangle) && projectile.Active && !burgerDead) { projectile.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, burger.CollisionRectangle.Center.X, burger.CollisionRectangle.Center.Y)); explosion.Play(); burger.Health -= GameConstants.TeddyBearProjectileDamage; messages.Remove(healthMessage); healthMessage = new Message(GameConstants.HealthPrefix + burger.Health, font, GameConstants.HealthLocation); messages.Add(healthMessage); CheckBurgerKill(); } } } // bear collisisons check if (bears.Any() && burger != null) { for (int i = 0; i < bears.Count; i++) { if (bears[i].Active) { // check and resolve collisions between burger and teddy bears if (burger.CollisionRectangle.Intersects(bears[i].CollisionRectangle) && !burgerDead) { score++; burger.Health -= GameConstants.BearDamage; messages.Clear(); healthMessage = new Message(GameConstants.HealthPrefix + burger.Health, font, GameConstants.HealthLocation); scoreMessage = new Message(GetScoreString(score), font, GameConstants.ScoreLocation); messages.AddRange(new Message[] { healthMessage, scoreMessage }); bears[i].Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bears[i].Location.X, bears[i].Location.Y)); explosion.Play(); CheckBurgerKill(); } // check and resolve collisions between teddy bears and projectiles else { foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.FrenchFries && bears[i].DrawRectangle.Intersects(projectile.CollisionRectangle) && bears[i].Active && projectile.Active) { score += GameConstants.BearPoints; messages.Remove(scoreMessage); GetScoreString(score); scoreMessage = new Message(GetScoreString(score), font, GameConstants.ScoreLocation); messages.Add(scoreMessage); bears[i].Active = false; projectile.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bears[i].Location.X, bears[i].Location.Y)); explosion.Play(); } } } } } } // 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); } } // adding bear to max value while (bears.Count < GameConstants.MaxBears && burger != null) { 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.Remove(explosions[i]); } } lastKeyboardState = currentKeyboardState; 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // get current mouse state and update burger MouseState mouse = Mouse.GetState(); KeyboardState keyboard = Keyboard.GetState(); //burger.Update(gameTime, mouse); burger.Update(gameTime, keyboard); // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { // bear1 and bear2 active, resolve collisions if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo collisionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].CollisionRectangle, bears[j].Velocity, bears[j].CollisionRectangle); if (collisionInfo != null) { if (collisionInfo.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].DrawRectangle = collisionInfo.FirstDrawRectangle; bears[i].Velocity = collisionInfo.FirstVelocity; } if (collisionInfo.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].DrawRectangle = collisionInfo.SecondDrawRectangle; bears[j].Velocity = collisionInfo.SecondVelocity; } teddyBounce.Play(); } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { if (bear.Active && bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { // process burger damaged by teddy bear burger.Health -= GameConstants.BearDamage; bear.Active = false; Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosionSound); explosions.Add(explosion); healthString = GameConstants.HealthPrefix + burger.Health; burgerDamage.Play(); // check burger health CheckBurgerKill(); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear && projectile.Active && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { // process burger damaged by projectile projectile.Active = false; burger.Health -= GameConstants.TeddyBearProjectileDamage; healthString = GameConstants.HealthPrefix + burger.Health; burgerDamage.Play(); // check burger health CheckBurgerKill(); } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { if (bear.Active && projectile.Active && projectile.Type == ProjectileType.FrenchFries && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { // colliding, then disable both bear and projectile bear.Active = false; projectile.Active = false; int explosionX = bear.CollisionRectangle.Center.X; int explosionY = bear.CollisionRectangle.Center.Y; // Blow up teddy bear Explosion explosion = new Explosion(explosionSpriteStrip, explosionX, explosionY, explosionSound); explosions.Add(explosion); // score points score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; } } } // 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); } } // spawn enough teddy bears to fill the ranks 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // get current mouse state and update burger MouseState mouse = Mouse.GetState(); burger.Update(gameTime, mouse); // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo collision = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (collision != null) { // We have a collision to resolve if (collision.FirstOutOfBounds) { // Teddy1 was forced out of bounds so remove from the game bears[i].Active = false; } else { bears[i].Velocity = collision.FirstVelocity; bears[i].DrawRectangle = collision.FirstDrawRectangle; } if (collision.SecondOutOfBounds) { // Teddy2 was forced out of bounds so remove from the game bears[j].Active = false; } else { bears[j].Velocity = collision.SecondVelocity; bears[j].DrawRectangle = collision.SecondDrawRectangle; } } } } } // check and resolve collisions between burger and teddy bears // check and resolve collisions between burger and projectiles // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear teddy in bears) { // Only check for collisions if this teddy is active if (teddy.Active) { foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.FrenchFries && projectile.Active) { // Check for the collision if (teddy.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { // Set the teddy and projectile to inactive teddy.Active = false; projectile.Active = false; // Create a new explosion explosions.Add(new Explosion(explosionSpriteStrip, teddy.Location.X, teddy.Location.Y)); } } } } } // 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); } } // clean out inactive projectiles for (int i = projectiles.Count - 1; i >= 0; i--) { if (!projectiles[i].Active) { projectiles.RemoveAt(i); } } // clean out finished explosions for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // get current mouse state and update burger MouseState mouse = Mouse.GetState(); KeyboardState keyboard = Keyboard.GetState(); burger.Update(gameTime, keyboard); if (burger.Health <= 0) { gameState = GameState.END; } if (keyboard.IsKeyDown(Keys.P) && gameState != GameState.END) { // pause or unpause if (gameState == GameState.PLAY) { gameState = GameState.PAUSED; } else { gameState = GameState.PLAY; } } if (gameState == GameState.PLAY) { // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active && bears[i].CollisionRectangle.Intersects(bears[j].CollisionRectangle)) { teddyBounce.Play(); CollisionResolutionInfo bearCollsionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].CollisionRectangle, bears[j].Velocity, bears[j].CollisionRectangle); if (bearCollsionInfo != null) { if (bearCollsionInfo.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = bearCollsionInfo.FirstVelocity; bears[i].DrawRectangle = bearCollsionInfo.FirstDrawRectangle; } if (bearCollsionInfo.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = bearCollsionInfo.SecondVelocity; bears[j].DrawRectangle = bearCollsionInfo.SecondDrawRectangle; } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { if (bear.Active && bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.BearDamage; // check if burger is dead CheckBurgerKill(); bear.Active = false; // play sound burgerDamage.Play(); explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosionSound)); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear) { if (projectile.Active && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { projectile.Active = false; burger.Health -= GameConstants.TeddyBearProjectileDamage; // check if burger is dead CheckBurgerKill(); // update health of burger healthString = GameConstants.HealthPrefix + burger.Health; // play sound burgerDamage.Play(); } } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { // check to see if projectile is french fries (teddy bears dont kill teddy bears) if (projectile.Type == ProjectileType.FrenchFries) { // check for collision if (bear.Active && projectile.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { bear.Active = false; projectile.Active = false; // add explosions at the center of the bear Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosionSound); explosions.Add(explosion); // update score score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; // play sound explosionSound.Play(); } } } } // 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); } } // add bears once one or more teddy bears are inactive 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } } else { } 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) { 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); }
/// <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 keyboard state and update burger KeyboardState keyboard = Keyboard.GetState(); burger.Update(gameTime, keyboard); // 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 //nested for loop to check each bear collision for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { //if both bears are active if (bears[i].Active && bears[j].Active) { //create resolution info with check collision method CollisionResolutionInfo bearCollisionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); //if the collision info isn't null, resolve collision if (bearCollisionInfo != null) { // check if first bear is out of bounds if (bearCollisionInfo.FirstOutOfBounds) { //make bear inactive bears[i].Active = false; } else { //else, set velocity and draw rectangle bears[i].Velocity = bearCollisionInfo.FirstVelocity; bears[i].DrawRectangle = bearCollisionInfo.FirstDrawRectangle; } //do the same with the other bear if (bearCollisionInfo.SecondOutOfBounds) { //make bear inactive bears[j].Active = false; } else { //else, set velocity and draw rectangle bears[j].Velocity = bearCollisionInfo.SecondVelocity; bears[j].DrawRectangle = bearCollisionInfo.SecondDrawRectangle; } //play bounce sound teddyBounce.Play(); } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { //check if they are colliding and bears are active if (burger.CollisionRectangle.Intersects(bear.CollisionRectangle) && bear.Active) { //make bear inactive bear.Active = false; //add to new explosion to explosions list and play sound explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion)); //subtract health from burger and play sound burger.Health -= GameConstants.BearDamage; burgerDamage.Play(); //check burger kill CheckBurgerKill(); //update health string healthString = GameConstants.HealthPrefix + burger.Health; } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { //check if they are colliding with burger and are active if (burger.CollisionRectangle.Intersects(projectile.CollisionRectangle) && projectile.Active) { //check if projectiles are bear projectiles if (projectile.Type == ProjectileType.TeddyBear) { //make projectile inactive projectile.Active = false; //subtract health from burger and play sound burger.Health -= GameConstants.BearDamage; burgerDamage.Play(); //check burger kill CheckBurgerKill(); //update health string healthString = GameConstants.HealthPrefix + burger.Health; } } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { //check for collisions with the projectiles foreach (Projectile projectile in projectiles) { if (bear.CollisionRectangle.Intersects(projectile.CollisionRectangle) && bear.Active) { //check if projectiles are french fries if (projectile.Type == ProjectileType.FrenchFries) { //update score and score string score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; //make bear and projectile inactive bear.Active = false; projectile.Active = false; //add to new explosion to explosions list explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion)); } } } } // clean out inactive teddy bears and add new ones as necessary for (int i = bears.Count - 1; i >= 0; i--) { //if bears aren't active, remove them 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); // get current keyboard state and update burger burger.Update(gameTime, Keyboard.GetState()); // 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 bear1 in bears) { foreach(TeddyBear bear2 in bears) { if(bear1 != bear2 && bear1.Active && bear2.Active) { CollisionResolutionInfo collisionResolutionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bear1.Velocity, bear1.DrawRectangle, bear2.Velocity, bear2.DrawRectangle); if(null != collisionResolutionInfo) { if (collisionResolutionInfo.FirstOutOfBounds) { bear1.Active = false; } else { bear1.Velocity = collisionResolutionInfo.FirstVelocity; bear1.DrawRectangle = collisionResolutionInfo.FirstDrawRectangle; teddyBounce.Play(); } if (collisionResolutionInfo.SecondOutOfBounds) { bear2.Active = false; } else { bear2.Velocity = collisionResolutionInfo.SecondVelocity; bear2.DrawRectangle = collisionResolutionInfo.SecondDrawRectangle; teddyBounce.Play(); } } } } } // check and resolve collisions between burger and teddy bears foreach(TeddyBear bear in bears) { if (burger.CollisionRectangle.Intersects(bear.CollisionRectangle)) { burgerDamaged(GameConstants.BearDamage); explodeTeddy(bear); } } // check and resolve collisions between burger and projectiles foreach(Projectile projectile in projectiles) { if(projectile.Type == ProjectileType.TeddyBear && burger.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { projectile.Active = false; burgerDamaged(GameConstants.TeddyBearProjectileDamage); } } // check and resolve collisions between teddy bears and projectiles foreach(TeddyBear bear in bears) { if (bear.Active) { foreach(Projectile projectile in projectiles) { if(projectile.Active && projectile.Type == ProjectileType.FrenchFries && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { projectile.Active = false; explodeTeddy(bear); score += GameConstants.BearPoints; updateScoreString(); } } } } // 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } // check game end CheckBurgerKill(); 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) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // get current mouse state and update burger MouseState mouse = Mouse.GetState(); //KeyboardState keyboard = Keyboard.GetState(); burger.Update(gameTime, mouse); // update other game objects foreach (TeddyBear bear in bears) { bear.Update(gameTime); } foreach (Projectile projectile in projectiles) { projectile.Update(gameTime); } foreach (Explosion exploded in explosions) { exploded.Update(gameTime); } // check and resolve collisions between teddy bears for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo resolutionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, windowWidth, windowHeight, bears[i].Velocity, bears[i].CollisionRectangle, bears[j].Velocity, bears[j].CollisionRectangle); if (resolutionInfo != null) { if (resolutionInfo.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = resolutionInfo.FirstVelocity; bears[i].DrawRectangle = resolutionInfo.FirstDrawRectangle; } if (resolutionInfo.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = resolutionInfo.SecondVelocity; bears[j].DrawRectangle = resolutionInfo.SecondDrawRectangle; } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bearC in bears) { if (bearC.Active == true && bearC.CollisionRectangle.Intersects(burger.CollisionRectangle)) { //Scoring score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; burger.Health -= GameConstants.BearDamage; healthString = GameConstants.HealthPrefix + burger.Health; bearC.Active = false; Explosion bomba = new Explosion(explosionSpriteStrip, bearC.CollisionRectangle.X, bearC.CollisionRectangle.Y, explosion); explosions.Add(bomba); //burger sound effect. burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectileC in projectiles) { if (projectileC.Active == true && projectileC.Type == ProjectileType.TeddyBear && projectileC.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.TeddyBearProjectileDamage; healthString = GameConstants.HealthPrefix + burger.Health; projectileC.Active = false; //burger sound effect. burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bearB in bears) { foreach (Projectile projectileB in projectiles) { if (projectileB.Type == ProjectileType.FrenchFries && projectileB.Active && bearB.Active && (bearB.CollisionRectangle.Intersects(projectileB.CollisionRectangle))) { //Scoring score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; //Explosion occurs. Explosion explode = new Explosion(explosionSpriteStrip, bearB.CollisionRectangle.X, bearB.CollisionRectangle.Y, explosion); explosions.Add(explode); bearB.Active = false; projectileB.Active = false; } } } // clean out inactive teddy bears and add new ones as necessary for (int x = bears.Count - 1; x >= 0; x--) { if (!(bears[x].Active)) { bears.RemoveAt(x); while (bears.Count != GameConstants.MaxBears) { SpawnBear(); } } } // clean out inactive projectiles for (int y = projectiles.Count - 1; y >= 0; y--) { if (!(projectiles[y].Active)) { projectiles.RemoveAt(y); } } // clean out finished explosions for (int z = explosions.Count - 1; z >= 0; z--) { if ((explosions[z].Finished)) { explosions.RemoveAt(z); } } 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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // get current mouse state and update burger MouseState mouse = Mouse.GetState(); KeyboardState keyboard = Keyboard.GetState(); //burger.Update(gameTime, mouse); burger.Update(gameTime, keyboard); // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { //two bears are active and they collide if (bears[i].CollisionRectangle.Intersects(bears[j].CollisionRectangle) && bears[i].Active && bears[j].Active) { //use the collision utils to get the info CollisionResolutionInfo collisionInfoItem = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); //there was a collision to resolve if (collisionInfoItem != null) { //collision put a bear outside the window, deactivate it if (collisionInfoItem.FirstOutOfBounds) { bears[i].Active = false; } else { //change the draw rectangle and direction of the bear according to the util function bears[i].Velocity = collisionInfoItem.FirstVelocity; bears[i].DrawRectangle = collisionInfoItem.FirstDrawRectangle; teddyBounce.Play(); } //collision put a bear outside the window, deactivate it if (collisionInfoItem.SecondOutOfBounds) { bears[j].Active = false; } else { //change the draw rectangle and direction of the bear according to the util function bears[j].Velocity = collisionInfoItem.SecondVelocity; bears[j].DrawRectangle = collisionInfoItem.SecondDrawRectangle; teddyBounce.Play(); } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { if (bear.CollisionRectangle.Intersects(burger.CollisionRectangle) && bear.Active) { burger.Health -= GameConstants.BEAR_DAMAGE; CheckBurgerKill(); bear.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion)); healthString = GameConstants.HEALTH_PREFIX + burger.Health.ToString(); burgerDamage.Play(); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear && projectile.Active && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE; CheckBurgerKill(); projectile.Active = false; healthString = GameConstants.HEALTH_PREFIX + burger.Health.ToString(); burgerDamage.Play(); } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { //active french fry projectile hits an active bear actor if (projectile.Type == ProjectileType.FrenchFries && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle) && projectile.Active && bear.Active) { bear.Active = false; projectile.Active = false; //play the explosion at the bear's location explosions.Add(new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion)); score += GameConstants.BEAR_POINTS; scoreString = GameConstants.SCORE_PREFIX + score; } } } // 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); } } //later, we'll add bears up to the max again while (bears.Count < GameConstants.MAX_BEARS) { 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { // For Mobile devices, this logic will close the Game when the Back button is pressed // Exit() is obsolete on iOS #if !__IOS__ if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } #endif // get current keyboard state and update burger burger.Update(gameTime, Keyboard.GetState()); // 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 for (int i = 0; i < bears.Count; i++) { if (!bears[i].Active) { continue; } for (int j = i + 1; j < bears.Count; j++) { if (!bears[j].Active) { continue; } CollisionResolutionInfo collisionResult = CollisionUtils.CheckCollision( gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (collisionResult == null) { continue; } if (collisionResult.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = collisionResult.FirstVelocity; bears[i].DrawRectangle = collisionResult.FirstDrawRectangle; } if (collisionResult.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = collisionResult.SecondVelocity; bears[j].DrawRectangle = collisionResult.SecondDrawRectangle; } teddyBounce.Play(); } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { if (!bear.Active) { continue; } if (bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health = burger.Health - GameConstants.BearDamage; bear.Active = false; Explosion exp = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion); explosions.Add(exp); healthString = GameConstants.HealthPrefix + burger.Health; burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (!projectile.Active || projectile.Type != ProjectileType.TeddyBear) { continue; } if (projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { projectile.Active = false; burger.Health = burger.Health - GameConstants.TeddyBearProjectileDamage; healthString = GameConstants.HealthPrefix + burger.Health; burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear teddy in bears) { if (!teddy.Active) { continue; } foreach (Projectile projectile in projectiles) { if (!projectile.Active || projectile.Type != ProjectileType.FrenchFries) { continue; } if (teddy.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { teddy.Active = false; projectile.Active = false; Explosion exp = new Explosion(explosionSpriteStrip, teddy.Location.X, teddy.Location.Y, explosion); explosions.Add(exp); score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; } } } // 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); } } // Spawn new teddy bears 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } 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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // get current mouse state and update burger MouseState mouse = Mouse.GetState(); burger.Update(gameTime, mouse); // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count - 1; j++) { if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo teddyCollision = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT, bears[i].Velocity, bears[i].CollisionRectangle, bears[j].Velocity, bears[j].CollisionRectangle); if (teddyCollision != null) { if (teddyCollision.FirstOutOfBounds) { bears[i].Active = false; } else if (!teddyCollision.FirstOutOfBounds) { bears[i].Velocity = teddyCollision.FirstVelocity; bears[i].DrawRectangle = teddyCollision.FirstDrawRectangle; } if (teddyCollision.SecondOutOfBounds) { bears[j].Active = false; } else if (!teddyCollision.SecondOutOfBounds) { bears[j].Velocity = teddyCollision.SecondVelocity; bears[j].DrawRectangle = teddyCollision.SecondDrawRectangle; } } } } } // check and resolve collisions between burger and teddy bears // check and resolve collisions between burger and projectiles // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.FrenchFries && bear.Active && projectile.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { bear.Active = false; projectile.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y)); } } } // clean out inactive teddy bears and add new ones as necessary for (int i = bears.Count - 1; i >= 0; i--) { if (bears[i].Active == false) { bears.RemoveAt(i); } if (bears.Count < GameConstants.MAX_BEARS) { SpawnBear(); } } // clean out inactive projectiles for (int i = projectiles.Count - 1; i >= 0; i--) { if (projectiles[i].Active == false) { projectiles.RemoveAt(i); } } // clean out finished explosions 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) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // get current mouse state and update burger burger.Update(gameTime, Keyboard.GetState()); // 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 for (int bearOne = 0; bearOne < bears.Count; bearOne++) { for (int bearTwo = bearOne + 1; bearTwo < bears.Count; bearTwo++) { if (bears[bearOne].Active && bears[bearTwo].Active) { CollisionResolutionInfo resolution = CollisionUtils.CheckCollision( gameTime.ElapsedGameTime.Milliseconds, GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT, bears[bearOne].Velocity, bears[bearOne].DrawRectangle, bears[bearTwo].Velocity, bears[bearTwo].DrawRectangle); if (resolution != null) { if (resolution.FirstOutOfBounds) { bears[bearOne].Active = false; } else { bears[bearOne].DrawRectangle = resolution.FirstDrawRectangle; bears[bearOne].Velocity = resolution.FirstVelocity; teddyBounce.Play(); } if (resolution.SecondOutOfBounds) { bears[bearTwo].Active = false; } else { bears[bearTwo].DrawRectangle = resolution.SecondDrawRectangle; bears[bearTwo].Velocity = resolution.SecondVelocity; teddyBounce.Play(); } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear teddy in bears) { if (teddy.Active) { if (teddy.CollisionRectangle.Intersects(burger.CollisionRectangle)) { explosions.Add(new Explosion(explosionSpriteStrip, teddy.DrawRectangle.Center.X, teddy.DrawRectangle.Center.Y)); explosion.Play(); burgerDamage.Play(); teddy.Active = false; burger.Health = burger.Health - GameConstants.BEAR_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Health.ToString(); CheckBurgerKill(); } } } // check and resolve collisions between burger and projectiles foreach (Projectile shot in projectiles) { if (shot.Active && shot.Type == ProjectileType.TeddyBear) { if (shot.CollisionRectangle.Intersects(burger.CollisionRectangle)) { shot.Active = false; burger.Health -= GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Health.ToString(); burgerDamage.Play(); CheckBurgerKill(); } } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear teddy in bears) { if (teddy.Active) { foreach (Projectile shot in projectiles) { if (shot.Type == ProjectileType.FrenchFries && shot.Active) { if (teddy.CollisionRectangle.Intersects(shot.CollisionRectangle)) { explosions.Add(new Explosion(explosionSpriteStrip, teddy.DrawRectangle.Center.X, teddy.DrawRectangle.Center.Y)); score += GameConstants.BEAR_POINTS; scoreString = GameConstants.SCORE_PREFIX + score.ToString(); explosion.Play(); teddy.Active = false; shot.Active = false; } } } } } // 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); //we only care to have all five bears up //according to what is said in assingment so... while (bears.Count < GameConstants.MAX_BEARS) { 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } base.Update(gameTime); }
/// <summary> /// Returns the collision resolution info object associated with a detected collision or null if no collision /// is detected /// /// Ref: David M. Bourg, Physics for Game Developers, pages 207 and 209 /// </summary> /// <param name="timeStep">the time step, in milliseconds</param> /// <param name="windowWidth">the width of the game window</param> /// <param name="windowHeight">the height of the game window</param> /// <param name="firstVelocity">the velocity of the first object</param> /// <param name="firstDrawRectangle">the draw rectangle of the first object</param> /// <param name="secondVelocity">the velocity of the second object</param> /// <param name="secondDrawRectangle">the draw rectangle of the second object</param> /// <returns>the collision resolution info object for a detected collision or null if no collision is detected</returns> public static CollisionResolutionInfo CheckCollision(int timeStep, int windowWidth, int windowHeight, Vector2 firstVelocity, Rectangle firstDrawRectangle, Vector2 secondVelocity, Rectangle secondDrawRectangle) { Rectangle initialFirstAabr; Rectangle initialSecondAabr; Rectangle currentFirstAabr; Rectangle currentSecondAabr; Rectangle collisionRectangle; // overlap test bool collisionDetected = firstDrawRectangle.Intersects(secondDrawRectangle); if (collisionDetected) { // initialize non-changing properties currentFirstAabr.Width = firstDrawRectangle.Width; currentFirstAabr.Height = firstDrawRectangle.Height; currentSecondAabr.Width = secondDrawRectangle.Width; currentSecondAabr.Height = secondDrawRectangle.Height; // back up both objects to their locations before the time step float firstDx = (firstVelocity.X * timeStep); float firstDy = (firstVelocity.Y * timeStep); initialFirstAabr.X = (int)(firstDrawRectangle.X - firstDx); initialFirstAabr.Y = (int)(firstDrawRectangle.Y - firstDy); initialFirstAabr.Width = firstDrawRectangle.Width; initialFirstAabr.Height = firstDrawRectangle.Height; float secondDx = (secondVelocity.X * timeStep); float secondDy = (secondVelocity.Y * timeStep); initialSecondAabr.X = (int)(secondDrawRectangle.X - secondDx); initialSecondAabr.Y = (int)(secondDrawRectangle.Y - secondDy); initialSecondAabr.Width = secondDrawRectangle.Width; initialSecondAabr.Height = secondDrawRectangle.Height; // at fixed time step of 60 fps, time increment can only be 8, 4, 2, or 1 int timeIncrement = timeStep / 2; int collisionDt = timeStep; // we know we have a collision or we wouldn't be here int dt = timeIncrement; while (timeIncrement > 0) { // move both objects forward by dt from their initial positions firstDx = firstVelocity.X * dt; firstDy = firstVelocity.Y * dt; secondDx = secondVelocity.X * dt; secondDy = secondVelocity.Y * dt; // update axis-aligned bounding rectangles currentFirstAabr.X = (int)(initialFirstAabr.X + firstDx); currentFirstAabr.Y = (int)(initialFirstAabr.Y + firstDy); currentSecondAabr.X = (int)(initialSecondAabr.X + secondDx); currentSecondAabr.Y = (int)(initialSecondAabr.Y + secondDy); // cut time increment in half as we search for the time of collision timeIncrement /= 2; collisionDetected = currentFirstAabr.Intersects(currentSecondAabr); if (collisionDetected) { // collision detected, so save collision dt and reduce dt to make it earlier collisionDt = dt; dt -= timeIncrement; // save the collision rectangle in case we don't find any other collisions Rectangle.Intersect(ref currentFirstAabr, ref currentSecondAabr, out collisionRectangle); } else { // no collision detected, so increase dt to make it later dt += timeIncrement; } } // get rectangle locations at start of collision int collisionStartTime = collisionDt; firstDx = firstVelocity.X * collisionStartTime; firstDy = firstVelocity.Y * collisionStartTime; secondDx = secondVelocity.X * collisionStartTime; secondDy = secondVelocity.Y * collisionStartTime; currentFirstAabr.X = (int)(initialFirstAabr.X + firstDx); currentFirstAabr.Y = (int)(initialFirstAabr.Y + firstDy); currentSecondAabr.X = (int)(initialSecondAabr.X + secondDx); currentSecondAabr.Y = (int)(initialSecondAabr.Y + secondDy); // use square collision normals Rectangle intersection = Rectangle.Intersect(currentFirstAabr, currentSecondAabr); CollisionSide collisionSide = GetCollisionSide(currentSecondAabr, intersection, firstVelocity, secondVelocity); // move objects through complete time step int preCollisionDuration = collisionStartTime - 1; int postCollisionDuration = timeStep - collisionStartTime + 1; CollisionResolutionInfo cri = BounceObjects(firstVelocity, initialFirstAabr, secondVelocity, initialSecondAabr, preCollisionDuration, postCollisionDuration, collisionSide); // check out of bounds and return cri.FirstOutOfBounds = OutOfBounds(firstDrawRectangle, windowWidth, windowHeight); cri.SecondOutOfBounds = OutOfBounds(secondDrawRectangle, windowWidth, windowHeight); return(cri); } else { // no collision return(null); } }
/// <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 keyBoard = Keyboard.GetState(); burger.Update(gameTime, keyBoard); // 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 for (int i = 0; i < bears.Count; i++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) // checking if both bears are active { //getting the collision Resolution Information CollisionResolutionInfo collisionResoutionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].CollisionRectangle, bears[j].Velocity, bears[j].CollisionRectangle); if (collisionResoutionInfo != null) // if not null { teddyBounce.Play(); //Solving for First bear if (collisionResoutionInfo.FirstOutOfBounds) { //forced out of the game world bears[i].Active = false; } else { //Setting new Velocity and DrawRectangle bears[i].DrawRectangle = collisionResoutionInfo.FirstDrawRectangle; bears[i].Velocity = collisionResoutionInfo.FirstVelocity; } //Solving for Second bear if (collisionResoutionInfo.SecondOutOfBounds) { //forced out of the game world bears[j].Active = false; } else { //Setting new Velocity and DrawRectangle bears[j].DrawRectangle = collisionResoutionInfo.SecondDrawRectangle; bears[j].Velocity = collisionResoutionInfo.SecondVelocity; } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear bear in bears) { if (bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { bear.Active = false; burger.Health -= GameConstants.BearDamage; healthString = GameConstants.HealthPrefix + burger.Health; Explosion explosion = new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y, this.explosion); explosions.Add(explosion); burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { //Checking whether the Projectile Type if (projectile.Type == ProjectileType.TeddyBear) { if (burger.CollisionRectangle.Intersects(projectile.CollisionRectangle)) // Checking for collision { //Collision Activities when detected burger.Health -= GameConstants.TeddyBearProjectileDamage; healthString = GameConstants.HealthPrefix + burger.Health; projectile.Active = false; burgerDamage.Play(); CheckBurgerKill(); } } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { //Checking whether the Projectile Type if (projectile.Type == ProjectileType.FrenchFries) { if (bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) // Checking for collision { //Collision Activities when detected bear.Active = false; projectile.Active = false; score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; Explosion explosion = new Explosion(explosionSpriteStrip, bear.CollisionRectangle.Center.X, bear.CollisionRectangle.Center.Y, this.explosion); explosions.Add(explosion); } } } } // 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 for (int i = explosions.Count - 1; i >= 0; i--) { if (explosions[i].Finished) { explosions.RemoveAt(i); } } base.Update(gameTime); }