Beispiel #1
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);


            // load audio content
            burgerDamage = Content.Load <SoundEffect>("Audio//BurgerDamage");
            burgerDeath  = Content.Load <SoundEffect>("Audio//BurgerDeath");
            burgerShot   = Content.Load <SoundEffect>("Audio//BurgerShot");
            burger       = new Burger(Content, "Graphics//burger", GameConstants.WindowWidth / 2, (7 * GameConstants.WindowHeight) / 8, burgerShot);

            explosion   = Content.Load <SoundEffect>("Audio//Explosion");
            teddyBounce = Content.Load <SoundEffect>("Audio//TeddyBounce");
            teddyShot   = Content.Load <SoundEffect>("Audio//TeddyShot");

            // load sprite font
            font = Content.Load <SpriteFont>("Fonts//Arial20");


            // load projectile and explosion sprites
            teddyBearProjectileSprite = Content.Load <Texture2D>("Graphics//teddybearprojectile");
            explosionSpriteStrip      = Content.Load <Texture2D>("Graphics//explosion");
            frenchFriesSprite         = Content.Load <Texture2D>("Graphics//frenchfries");

            // add initial game objects
            for (int i = 0; i < GameConstants.MaxBears; i++)
            {
                SpawnBear();
            }



            // set initial health and score strings
            healthString = GameConstants.HealthPrefix + burger.getHealth();
            scoreString  = GameConstants.ScorePrefix + score;
        }
Beispiel #2
0
        /// <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);
        }