Ejemplo n.º 1
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)
        {
            // Exit the game.
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // Play sound effect when the player shoots.
            if (Keyboard.GetState().IsKeyDown(Keys.Space) && player1.MySuperProjectile.ProjectileState == SuperProjectile.PROJECTILE_STATE.STILL)
            {
                playerFire.Play();
            }


            #region Handle behaviour between player and sentry.
            foreach (var item in sentries)
            {
                // The following if statement will ensure that the sentry will only carry out it's behaviour if it is still visible.
                if (item.Visible == true)
                {
                    // Follow the player's position.
                    item.Follow(player1);

                    // Ensure the sentry and the player collide if the sentry is still visible.
                    player1.CollisionSentry(item);


                    #region Handle the sentry's projectile colllsions with walls
                    foreach (var wall in projectileColliders)
                    {
                        item.SentrySuperProjectile.WallCollision(wall);
                    }
                    #endregion

                    #region what will happen when a sentry dies.
                    // If the sentry is killed...
                    if (item.Health <= 0)
                    {
                        if (item.Visible == true)
                        {
                            // ...Add to the killcount.
                            killCount++;

                            // Create an explosion where the sentry once was.
                            deathExplosion = new AnimateSheetSprite(this, item.PixelPosition, new List <TileRef>()
                            {
                                new TileRef(0, 0, 0),
                                new TileRef(1, 0, 0),
                                new TileRef(2, 0, 0),
                            }, 64, 64, 0f);
                        }

                        // Make the sentry invisible.
                        item.Visible = false;
                    }
                    #endregion
                }
            }
            #endregion

            #region Handle the player and player projectile's collisions with walls.
            // Ensure that the player collides with locks.
            foreach (Lock collisionLock in locks)
            {
                player1.CollisionLock(collisionLock);
            }

            // Ensure the player projectile collides with walls.
            foreach (var item in projectileColliders)
            {
                player1.MySuperProjectile.WallCollision(item);
            }
            #endregion

            #region  What to do when all sentries have been destroyed.
            if (killCount == sentryCount)
            {
                MediaPlayer.Volume -= 0.4f;

                foreach (Lock item in locks)
                {
                    item.Visible = false;
                }

                gameOverState = true;
            }
            #endregion

            #region Play background music when test timer is up.
            // Play background music.
            //timeSpan -= gameTime.ElapsedGameTime;

            // timeSpan < TimeSpan.Zero
            // Add the above line to the if statement below to test the countdown.

            if (MediaPlayer.Volume != 0.5f)
            {
                MediaPlayer.Play(backgroundMusic);
                MediaPlayer.Volume += 0.5f;
            }
            #endregion

            // What to do when the player reaches the exit tile.
            if (player1.BoundingRectangle.Intersects(exitTile.CollisionField) && player1.Visible == true)
            {
                victoryFanfare.Play();
                player1.Visible = false;
            }


            base.Update(gameTime);
            // TODO: Add your update logic here

            /// <summary>
            /// This is called when the game should draw itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
        }