Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private float DoJump(float velocityY, GameTime gameTime)
- {
- // If the player wants to jump
- if (isJumping)
- {
- // Begin or continue a jump
- if ((!wasJumping && IsOnGround) || jumpTime > 0.0f)
- {
- if (jumpTime == 0.0f)
- jumpSound.Play();
- jumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
- sprite.PlayAnimation(jumpAnimation);
- }
- if (isJumping && isOnLeft && numberOfJumps < 1)
- {
- numberOfJumps++;
- }
- // If we are in the ascent of the jump
- if (0.0f < jumpTime && jumpTime <= MaxJumpTime)
- {
- // Fully override the vertical velocity with a power curve that gives players more control over the top of the jump
- velocityY = JumpLaunchVelocity * (1.0f - (float)Math.Pow(jumpTime / MaxJumpTime, JumpControlPower));
- }
- else
- {
- // Reached the apex of the jump and has double jumps
- if (velocityY > -MaxFallSpeed * 0.5f && !wasJumping)
- {
- velocityY =
- JumpLaunchVelocity * (0.5f - (float)Math.Pow(jumpTime / MaxJumpTime, JumpControlPower));
- jumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
- }
- else
- {
- jumpTime = 0.0f;
- }
- }
- }
- else
- {
- // Continues not jumping or cancels a jump in progress
- jumpTime = 0.0f;
- }
- wasJumping = isJumping;
- return velocityY;
- }
- /// <summary>
- /// Detects and resolves all collisions between the player and his neighboring
- /// tiles. When a collision is detected, the player is pushed away along one
- /// axis to prevent overlapping. There is some special logic for the Y axis to
- /// handle platforms which behave differently depending on direction of movement.
- /// </summary>
- private void HandleCollisions()
- {
- // Get the player's bounding rectangle and find neighboring tiles.
- Rectangle bounds = BoundingRectangle;
- int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width);
- int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
- int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height);
- int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;
- // Reset flag to search for ground collision.
- isOnGround = false;
- isOnLeft = true;
- // For each potentially colliding tile,
- for (int y = topTile; y <= bottomTile; ++y)
- {
- for (int x = leftTile; x <= rightTile; ++x)
- {
- // If this tile is collidable,
- TileCollision collision = Level.GetCollision(x, y);
- if (collision != TileCollision.Passable)
- {
- // Determine collision depth (with direction) and magnitude.
- Rectangle tileBounds = Level.GetBounds(x, y);
- Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
- if (depth != Vector2.Zero)
- {
- float absDepthX = Math.Abs(depth.X);
- float absDepthY = Math.Abs(depth.Y);
- // Resolve the collision along the shallow axis.
- if (absDepthY < absDepthX || collision == TileCollision.Platform)
- {
- // If we crossed the top of a tile, we are on the ground.
- if (previousBottom <= tileBounds.Top)
- isOnGround = true;
- // Ignore platforms, unless we are on the ground.
- if (collision == TileCollision.Impassable || IsOnGround)
- {
- // Resolve the collision along the Y axis.
- Position = new Vector2(Position.X, Position.Y + depth.Y);
- isOnLeft = true;
- // Perform further collisions with the new bounds.
- bounds = BoundingRectangle;
- }
- }
- else if (collision == TileCollision.Impassable) // Ignore platforms.
- {
- // Resolve the collision along the X axis.
- Position = new Vector2(Position.X + depth.X, Position.Y);
- // Perform further collisions with the new bounds.
- bounds = BoundingRectangle;
- }
- }
- }
- }
- }
- // Save the new bounds bottom.
- previousBottom = bounds.Bottom;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement