Advertisement
Guest User

Player class

a guest
Feb 10th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.63 KB | None | 0 0
  1. private float DoJump(float velocityY, GameTime gameTime)
  2.         {
  3.            
  4.             // If the player wants to jump
  5.             if (isJumping)
  6.             {
  7.                 // Begin or continue a jump
  8.                 if ((!wasJumping && IsOnGround) || jumpTime > 0.0f)
  9.                 {
  10.                     if (jumpTime == 0.0f)
  11.                         jumpSound.Play();
  12.  
  13.                     jumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
  14.                     sprite.PlayAnimation(jumpAnimation);
  15.                 }
  16.                 if (isJumping && isOnLeft && numberOfJumps < 1)
  17.                 {
  18.                     numberOfJumps++;
  19.                 }
  20.  
  21.  
  22.                 // If we are in the ascent of the jump
  23.                 if (0.0f < jumpTime && jumpTime <= MaxJumpTime)
  24.                 {
  25.                     // Fully override the vertical velocity with a power curve that gives players more control over the top of the jump
  26.                     velocityY = JumpLaunchVelocity * (1.0f - (float)Math.Pow(jumpTime / MaxJumpTime, JumpControlPower));
  27.                 }
  28.                 else
  29.                 {
  30.                     // Reached the apex of the jump and has double jumps
  31.                     if (velocityY > -MaxFallSpeed * 0.5f && !wasJumping)
  32.                     {
  33.                         velocityY =
  34.                             JumpLaunchVelocity * (0.5f - (float)Math.Pow(jumpTime / MaxJumpTime, JumpControlPower));
  35.                         jumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
  36.                        
  37.                     }
  38.                     else
  39.                     {
  40.                         jumpTime = 0.0f;
  41.                     }
  42.                    
  43.                 }
  44.  
  45.  
  46.             }
  47.             else
  48.             {
  49.                 // Continues not jumping or cancels a jump in progress
  50.                 jumpTime = 0.0f;
  51.             }
  52.             wasJumping = isJumping;
  53.  
  54.             return velocityY;
  55.         }
  56.  
  57.        
  58.  
  59.         /// <summary>
  60.         /// Detects and resolves all collisions between the player and his neighboring
  61.         /// tiles. When a collision is detected, the player is pushed away along one
  62.         /// axis to prevent overlapping. There is some special logic for the Y axis to
  63.         /// handle platforms which behave differently depending on direction of movement.
  64.         /// </summary>
  65.         private void HandleCollisions()
  66.         {
  67.             // Get the player's bounding rectangle and find neighboring tiles.
  68.             Rectangle bounds = BoundingRectangle;
  69.             int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width);
  70.             int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
  71.             int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height);
  72.             int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;
  73.  
  74.             // Reset flag to search for ground collision.
  75.             isOnGround = false;
  76.             isOnLeft = true;
  77.  
  78.             // For each potentially colliding tile,
  79.             for (int y = topTile; y <= bottomTile; ++y)
  80.             {
  81.                 for (int x = leftTile; x <= rightTile; ++x)
  82.                 {
  83.                     // If this tile is collidable,
  84.                     TileCollision collision = Level.GetCollision(x, y);
  85.                     if (collision != TileCollision.Passable)
  86.                     {
  87.                         // Determine collision depth (with direction) and magnitude.
  88.                         Rectangle tileBounds = Level.GetBounds(x, y);
  89.                         Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
  90.                         if (depth != Vector2.Zero)
  91.                         {
  92.                             float absDepthX = Math.Abs(depth.X);
  93.                             float absDepthY = Math.Abs(depth.Y);
  94.                            
  95.  
  96.                             // Resolve the collision along the shallow axis.
  97.                             if (absDepthY < absDepthX || collision == TileCollision.Platform)
  98.                             {
  99.                                 // If we crossed the top of a tile, we are on the ground.
  100.                                 if (previousBottom <= tileBounds.Top)
  101.                                     isOnGround = true;
  102.  
  103.                                 // Ignore platforms, unless we are on the ground.
  104.                                 if (collision == TileCollision.Impassable || IsOnGround)
  105.                                 {
  106.                                     // Resolve the collision along the Y axis.
  107.                                     Position = new Vector2(Position.X, Position.Y + depth.Y);
  108.                                     isOnLeft = true;
  109.  
  110.                                     // Perform further collisions with the new bounds.
  111.                                     bounds = BoundingRectangle;
  112.                                 }
  113.                             }
  114.                             else if (collision == TileCollision.Impassable) // Ignore platforms.
  115.                             {
  116.                                 // Resolve the collision along the X axis.
  117.                                 Position = new Vector2(Position.X + depth.X, Position.Y);
  118.                                
  119.  
  120.                                 // Perform further collisions with the new bounds.
  121.                                 bounds = BoundingRectangle;
  122.                             }
  123.                         }
  124.                     }
  125.                 }
  126.             }
  127.  
  128.             // Save the new bounds bottom.
  129.             previousBottom = bounds.Bottom;
  130.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement