/// <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); // 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 n = i + 1; n < bears.Count; n++) { if (bears[i].Active && bears[n].Active) { CollisionResolutionInfo collisionsBetweenTeddyBears = CollisionUtils.CheckCollision( gameTime.ElapsedGameTime.Milliseconds, GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT, bears[i].Velocity, bears[i].DrawRectangle, bears[n].Velocity, bears[n].DrawRectangle); if (collisionsBetweenTeddyBears != null) { if (collisionsBetweenTeddyBears.FirstOutOfBounds) bears[i].Active = false; else { bears[i].Velocity = collisionsBetweenTeddyBears.FirstVelocity; bears[i].DrawRectangle = collisionsBetweenTeddyBears.FirstDrawRectangle; teddyBounce.Play(); } if (collisionsBetweenTeddyBears.SecondOutOfBounds) bears[n].Active = false; else { bears[n].Velocity = collisionsBetweenTeddyBears.SecondVelocity; bears[n].DrawRectangle = collisionsBetweenTeddyBears.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 = burger.Health - GameConstants.BEAR_DAMAGE; bear.Active = false; burgerDamage.Play(); CheckBurgerKill(); healthString = GameConstants.HEALTH_PREFIX + burger.Health; Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, this.explosion); explosions.Add(explosion); } } // 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)) { projectile.Active = false; burger.Health = burger.Health - GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE; burgerDamage.Play(); CheckBurgerKill(); healthString = GameConstants.HEALTH_PREFIX + burger.Health; } } // 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; Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, this.explosion); explosions.Add(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); 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 mouse state and update burger MouseState mouse = Mouse.GetState(); KeyboardState key = Keyboard.GetState(); burger.Update(gameTime, key); // 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) { if (cri.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].DrawRectangle = cri.FirstDrawRectangle; bears[i].Velocity = cri.FirstVelocity; } if (cri.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].DrawRectangle = cri.SecondDrawRectangle; bears[j].Velocity = cri.SecondVelocity; } teddyBounce.Play(); } } } } // check and resolve collisions between burger and teddy bears foreach (var collisionBear in bears) { if (collisionBear.Active) { if (collisionBear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.BearDamage; collisionBear.Active = false; // Explode! Explosion e = new Explosion(explosionSpriteStrip, collisionBear.Location.X, collisionBear.Location.Y); explosions.Add(e); explosion.Play(); // Set burger health string healthString = GameConstants.HealthPrefix + burger.Health; // Sound! burgerDamage.Play(); CheckBurgerKill(); } } } // check and resolve collisions between burger and projectiles foreach (var projectile in projectiles) { if (projectile.Active && projectile.Type == ProjectileType.TeddyBear) { if (projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.TeddyBearProjectileDamage; projectile.Active = false; // Set burger health string healthString = GameConstants.HealthPrefix + burger.Health; // Sound! burgerDamage.Play(); } } } // check and resolve collisions between teddy bears and projectiles foreach (var bear in bears) { foreach (var proj in projectiles) { if (bear.CollisionRectangle.Intersects(proj.CollisionRectangle) && proj.Type == ProjectileType.FrenchFries) { bear.Active = false; proj.Active = false; // Explode! Explosion e = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y); explosions.Add(e); explosion.Play(); // Update score 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); } } // Add new 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 foreach (TeddyBear bear in bears) { if (bear.Active == true && bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.BEAR_DAMAGE; bear.Active = false; Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y); explosions.Add(explosion); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear && projectile.Active && burger.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { burger.Health -= GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE; projectile.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, burger.CollisionRectangle.Center.X, burger.CollisionRectangle.Center.Y)); } } // 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) { 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) { // 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 mouseState = Mouse.GetState(); //burger.Update(gameTime, mouseState); // get current keyboard state and update burger KeyboardState keyboardState = Keyboard.GetState(); burger.Update(gameTime, keyboardState); // 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 info = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WINDOW_WIDTH, GameConstants.WINDOW_HEIGHT, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (info != null) { if (info.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = info.FirstVelocity; bears[i].DrawRectangle = info.FirstDrawRectangle; teddyBounce.Play(); } if (info.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = info.SecondVelocity; bears[j].DrawRectangle = info.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)) { bear.Active = false; burger.Health -= GameConstants.BEAR_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + burger.Health; burgerDamage.Play(); CheckBurgerKill(); Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, this.explosion); explosions.Add(explosion); } } // check and resolve collisions between burger and projectiles foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.TeddyBear) { if (projectile.Active && burger.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { projectile.Active = false; burger.Health -= GameConstants.TEDDY_BEAR_PROJECTILE_DAMAGE; healthString = GameConstants.HEALTH_PREFIX + 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) { if (bear.Active && projectile.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { bear.Active = false; projectile.Active = false; Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, this.explosion); explosions.Add(explosion); score += GameConstants.BEAR_POINTS; scoreString = GameConstants.SCORE_PREFIX + score; } } } } // clean out inactive teddy bears for (int i = bears.Count - 1; i >= 0; i--) { if (!bears[i].Active) { bears.RemoveAt(i); } } // add new teddy bears as necessary 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 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 collisionResolutionInfo = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, new Vector2(bears[i].Velocity.X, bears[i].Velocity.Y), bears[i].DrawRectangle, new Vector2(bears[j].Velocity.X, bears[j].Velocity.Y), bears[j].DrawRectangle); if (collisionResolutionInfo != null) { if (collisionResolutionInfo.FirstOutOfBounds) { bears[i].Active = false; } else if (collisionResolutionInfo.FirstOutOfBounds == false) { bears[i].Velocity = collisionResolutionInfo.FirstVelocity; bears[i].DrawRectangle = collisionResolutionInfo.FirstDrawRectangle; } if (collisionResolutionInfo.SecondOutOfBounds) { bears[j].Active = false; } else if (collisionResolutionInfo.SecondOutOfBounds == false) { bears[j].Velocity = collisionResolutionInfo.SecondVelocity; bears[j].DrawRectangle = collisionResolutionInfo.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) { foreach (Projectile projectile in projectiles) { if (teddy.Active && projectile.Type == ProjectileType.FrenchFries && projectile.Active && teddy.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { teddy.Active = false; projectile.Active = false; Explosion explosion = new Explosion(explosionSpriteStrip, teddy.Location.X, teddy.Location.Y); 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); } } // 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 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 mouse state and update burger //MouseState mouse = Mouse.GetState(); //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) { var info0 = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, GameConstants.WindowWidth, GameConstants.WindowHeight, bears[i].Velocity, bears[i].DrawRectangle, bears[j].Velocity, bears[j].DrawRectangle); if (info0 != null) { if (info0.FirstOutOfBounds) { bears[i].Active = false; } else if (!info0.FirstOutOfBounds) { bears[i].Velocity = info0.FirstVelocity; bears[i].DrawRectangle = info0.FirstDrawRectangle; teddyBounce.Play(); } if (info0.SecondOutOfBounds) { bears[j].Active = false; } else if (!info0.SecondOutOfBounds) { bears[j].Velocity = info0.SecondVelocity; bears[j].DrawRectangle = info0.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)) { burger.Health = burger.Health - GameConstants.BearDamage; bear.Active = false; Explosion newExplosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion); explosions.Add(newExplosion); //burger health display healthString = GameConstants.HealthPrefix + burger.Health; //sound when hit 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 = burger.Health - GameConstants.TeddyBearProjectileDamage; projectile.Active = false; Explosion newExplosion = new Explosion(explosionSpriteStrip, projectile.CollisionRectangle.X, projectile.CollisionRectangle.Y, explosion); explosions.Add(newExplosion); //burger health display healthString = GameConstants.HealthPrefix + burger.Health; //sound when hit burgerDamage.Play(); } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { if (projectile.Type == 0 && projectile.Active && bear.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { bear.Active = false; projectile.Active = false; Explosion newExplosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y, explosion); explosions.Add(newExplosion); 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); } } 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); } } //burger takes damage if (burger.Health < 100) { 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) { 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 //burger.Update(gameTime, Mouse.GetState()); //Get keyboard stateand update burger position 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++) { for (int j = i + 1; j < bears.Count; j++) { if (bears[i].Active && bears[j].Active) { CollisionResolutionInfo collision; 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) { teddyBounce.Play(); if (collision.FirstOutOfBounds) { bears[i].Active = false; } else { bears[i].Velocity = collision.FirstVelocity; bears[i].DrawRectangle = collision.FirstDrawRectangle; } if (collision.SecondOutOfBounds) { bears[j].Active = false; } else { bears[j].Velocity = collision.SecondVelocity; bears[j].DrawRectangle = collision.SecondDrawRectangle; } } } } } // check and resolve collisions between burger and teddy bears foreach (TeddyBear teddy in bears) { if (teddy.CollisionRectangle.Intersects(burger.CollisionRectangle) && teddy.Active) { burger.Health = burger.Health - GameConstants.BearDamage; teddy.Active = false; Explosion newExplosion = new Explosion(explosionSpriteStrip, teddy.DrawRectangle.Center.X, teddy.DrawRectangle.Center.Y, explosion); explosions.Add(newExplosion); //Set the health display healthString = GameConstants.HealthPrefix + " " + burger.Health; burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between burger and projectiles foreach (Projectile proj in projectiles) { if (proj.Type == ProjectileType.TeddyBear && proj.Active && proj.CollisionRectangle.Intersects(burger.CollisionRectangle)) { proj.Active = false; burger.Health = burger.Health - GameConstants.TeddyBearProjectileDamage; //Set the health display healthString = GameConstants.HealthPrefix + " " + burger.Health; burgerDamage.Play(); CheckBurgerKill(); } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear teddy in bears) { foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.FrenchFries && projectile.Active && teddy.Active && teddy.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { teddy.Active = false; projectile.Active = false; Explosion newExplosion = new Explosion(explosionSpriteStrip, teddy.DrawRectangle.Center.X, teddy.DrawRectangle.Center.Y, explosion); explosions.Add(newExplosion); //SEt score message 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); } } // 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); } } //spawn new bear when one is destroyed while (bears.Count <= GameConstants.MaxBears) { SpawnBear(); } //Set the health display healthString = GameConstants.HealthPrefix + " " + burger.Health; 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 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); }
/// <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[j].Active && bears[i].Active) { CollisionResolutionInfo teddyCollision = CollisionUtils.CheckCollision(gameTime.ElapsedGameTime.Milliseconds, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, 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 foreach (TeddyBear bear in bears) { if (bear.Active == true && bear.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burger.Health -= GameConstants.BearDamage; healthString = GameConstants.HealthPrefix + burger.Health; bear.Active = false; Explosion explosion = new Explosion(explosionSpriteStrip, bear.Location.X, bear.Location.Y); explosions.Add(explosion); } } // check and resolve collisions between burger and projectiles foreach (TeddyBear bear in bears) { foreach (Projectile projectile in projectiles) { if (projectile.Active == true && projectile.Type == ProjectileType.TeddyBear && projectile.CollisionRectangle.Intersects(burger.CollisionRectangle)) { burgerDamage.Play(); burger.Health = burger.Health - GameConstants.TeddyBearProjectileDamage; projectile.Active = false; } } } // check and resolve collisions between teddy bears and projectiles foreach (TeddyBear bear in bears) { bear.Update(gameTime); foreach (Projectile projectile in projectiles) { if (projectile.Type == ProjectileType.FrenchFries && bear.Active && projectile.Active && bear.CollisionRectangle.Intersects(projectile.CollisionRectangle)) { bear.Active = false; score += GameConstants.BearPoints; scoreString = GameConstants.ScorePrefix + score; CheckBurgerKill(); projectile.Active = false; explosions.Add(new Explosion(explosionSpriteStrip, bear.DrawRectangle.Center.X, bear.DrawRectangle.Center.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); if (i < 5) { 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); } } //spawn new bears if needed while (bears.Count < GameConstants.MaxBears) { SpawnBear(); } base.Update(gameTime); }
/// <summary> /// Explodes a teddy bear. Sets bear to inactive, adds explosion on teddy bear location. /// </summary> /// <param name="bear">The bear to explode.</param> private void explodeTeddy(TeddyBear bear) { bear.Active = false; Point bearCenter = bear.CollisionRectangle.Center; Explosion explosion = new Explosion(explosionSpriteStrip, bearCenter.X, bearCenter.Y, this.explosion); explosions.Add(explosion); }
/// <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); }