Esempio n. 1
0
        /// <summary>
        /// Detects and resolves collisions between the Player and the glidingPlatform nearest him.
        /// </summary>
        public void HandleGlidingPlatformCollisions()
        {
            // Did I just collide with a gliding platform?
            foreach (GlidingPlatform platform in Level.playerPlatforms)
            // To know this we have to check the list of platforms in
            // the camera's visible range
            // which we collected from the level calss
            {
                // We check if the player's feet intersect with the platform's surface.
                if (platform.Surface.Intersects(Feet))
                {
                    OnPlatform(platform); // If so, then this is the platform we want to save.
                }
            }

            // If I own a platform
            if (isOnPlatform == true)
            {
                // reset isOnPlatform for the purposes of evaluating it again.
                isOnPlatform = false;

                // Do they intersect?
                if (Feet.Intersects(platform.Surface))
                {
                    isOnPlatform = true;
                    //Yes they intersect. By how deep?
                    Vector2 depth = RectangleExtensions.GetIntersectionDepth(Feet, platform.Surface);

                    // Is it more than zero?
                    if (depth != Vector2.Zero)
                    {
                        // Yes they intersect and it is more than zero depth!
                        // Find out by how much.
                        float absDepthX = Math.Abs(depth.X);
                        float absDepthY = Math.Abs(depth.Y);

                        // Act as if we are on the ground if we were previously on the a platform.
                        if (previousBottom >= platform.Surface.Top)
                        {
                            isOnGround = true;
                        }

                        if (isOnPlatform)
                        {
                            // Resolve the collision along the Y axis.
                            Position = new Vector2(Position.X /*+ platform.Position.X*/, Position.Y - Math.Abs(depth.Y));
                        }
                    }
                }
            }

            // Check if the player's Y has moved through a platform's surface.
            foreach (GlidingPlatform platform in level.playerPlatforms)
            {
                if ((position.Y > platform.Surface.Top - 12) && (position.Y < platform.Surface.Top + 12) &&
                    (position.X > platform.Surface.Left - BoundingRectangle.Width / 2) &&
                    (position.X < platform.Surface.Right + BoundingRectangle.Width / 2) &&
                    (velocity.Y > 0)) // if the velocity is negative the player is jumping up.
                // If the player is jumping up, we don't want him to stick, only when moving down.

                // The speed the player moves with a MaxFallSpeed of 600.0f is 12 pixels per second.
                // If we don't check this condition, the player can jump right past the surface of the platform,
                // and the game will never know to make the player stick!
                {
                    OnPlatform(platform);
                    position.Y = platform.Surface.Top;
                }
            }

            previousBottom = BoundingRectangle.Bottom;
        }