Ejemplo n.º 1
0
        /// <summary>
        /// Animates each gem and checks to allows the player to collect them.
        /// </summary>
        private void UpdateGems(GameTime gameTime)
        {
            for (int i = 0; i < gems.Count; ++i)
            {
                Gem gem = gems[i];

                gem.Update(gameTime);

                if (gem.BoundingCircle.Intersects(Player.GetBoundingRectangle(Player.PlayerPositions[0])))
                {
                    gems.RemoveAt(i--);
                    OnGemCollected(gem, Player);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Animates each enemy and allow them to kill the player.
        /// </summary>
        private void UpdateEnemies(GameTime gameTime)
        {
            foreach (Enemy enemy in enemies)
            {
                enemy.Update(gameTime);

                // Touching an enemy instantly kills the player
                for (int i = 0; i < Player.PlayerPositions.Count; i++)
                {
                    if (enemy.BoundingRectangle.Intersects(Player.GetBoundingRectangle(Player.PlayerPositions[i])))
                    {
                        OnPlayerKilled(i, enemy);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Updates all objects in the world, performs collision between them,
        /// and handles the time limit with scoring.
        /// </summary>
        public void Update(
            GameTime gameTime,
            KeyboardState keyboardState,
            GamePadState gamePadState,
            DisplayOrientation orientation)
        {
            if (IsLoading)
            {
                return;
            }

            if (!initializedLevelStartTime)
            {
                totalTimeLevelStarted     = gameTime.TotalGameTime;
                initializedLevelStartTime = true;
            }

            // record high score at the end of level
            if (Player.PlayerStates[0] == PlayerState.ReachedExit && TimeRemaining == TimeSpan.Zero)
            {
                Player.ReplayData.HighScore = this.Score;
            }

            // Pause while the player is dead or time is expired.
            if (!Player.IsAlive || TimeRemaining == TimeSpan.Zero)
            {
                // Still want to perform physics on the player.
                Player.ApplyPhysics(gameTime);
            }
            else if (ReachedExit)
            {
                // Animate the time being converted into points.
                int milliSeconds = (int)Math.Round(gameTime.ElapsedGameTime.TotalMilliseconds * 100.0f);
                milliSeconds = Math.Min(milliSeconds, (int)Math.Ceiling(TimeRemaining.TotalMilliseconds));


                timeRemaining -= TimeSpan.FromMilliseconds(milliSeconds);
                score         += (int)Math.Round(milliSeconds * (PointsPerSecond / 1000.0));
            }
            else
            {
                timeRemaining -= gameTime.ElapsedGameTime;
                Player.Update(gameTime, keyboardState, gamePadState, orientation);
                UpdateGems(gameTime);

                // Falling off the bottom of the level kills the player.
                for (int i = 0; i < Player.PlayerPositions.Count; i++)
                {
                    if (Player.GetBoundingRectangle(Player.PlayerPositions[i]).Top >= Height * Tile.Height)
                    {
                        OnPlayerKilled(i, null);
                    }
                }

                UpdateEnemies(gameTime);

                // The player has reached the exit if they are standing on the ground and
                // his bounding rectangle contains the center of the exit tile. They can only
                // exit when they have collected all of the gems.
                for (int i = 0; i < Player.PlayerPositions.Count; i++)
                {
                    if (Player.PlayerStates[i] == PlayerState.Alive &&
                        Player.IsOnGround &&
                        Player.GetBoundingRectangle(Player.PlayerPositions[i]).Contains(exit))
                    {
                        OnExitReached(i);
                    }
                }
            }

            // Clamp the time remaining at zero.
            if (timeRemaining < TimeSpan.Zero)
            {
                timeRemaining = TimeSpan.Zero;
            }
        }