Advertisement
smc_gamer

SML - Player Jump Methods

Feb 24th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. /* constants */
  2.         private const float accelerationX = 800f;
  3.         private const float maxSpeedX = 5000f;
  4.         private const float runToWalkRatio = 2f;
  5.  
  6.         private const float groundFriction = 0.68f;
  7.         private const float airFriction = 0.78f;
  8.  
  9.         private const float maxJumpTime = 1500f;
  10.         private const float jumpLaunchSpeed = 500f;
  11.         private const float gravity = 300f;
  12.  
  13.         private const float maxSpeedY = 3000f;
  14.         private const float maxFallSpeed = 300f;
  15.         private const float maxFloatSpeed = 300f;
  16.  
  17.         private const float jumpControlPower = 0.75f;
  18.  
  19.         public override void Update(GameTime gameTime)
  20.         {
  21.             float delta = gameTime.GetElapsedSeconds();
  22.  
  23.             var current = InputComponent.Input.Current;
  24.  
  25.             if (InputComponent.Input.IsNewKeyPress(Keys.B))
  26.                 mode = !mode;
  27.  
  28.             if (mode)
  29.             {
  30.                 //This makes the player follow the mouse.
  31.  
  32.                 Vector2 mouse = new Vector2(current.MouseState.X, current.MouseState.Y) + Owner.Camera.Position;
  33.  
  34.                 Velocity = mouse - Position;
  35.                 Velocity.Normalize();
  36.                 Velocity *= 5;
  37.  
  38.                 base.Update(gameTime);
  39.             }
  40.             else
  41.             {
  42.  
  43.                 float acceleration = 0f;
  44.  
  45.                 if (current.IsKeyMapDown(InputSettings.Left))
  46.                 {
  47.                     AnimationComponent.SpriteEffects = SpriteEffects.FlipHorizontally;
  48.                     acceleration = -accelerationX;
  49.                 }
  50.                 else if (current.IsKeyMapDown(InputSettings.Right))
  51.                 {
  52.                     AnimationComponent.SpriteEffects = SpriteEffects.None;
  53.                     acceleration = accelerationX;
  54.                 }
  55.  
  56.                 if (current.IsKeyMapDown(InputSettings.Run))
  57.                 {
  58.                     acceleration *= runToWalkRatio;
  59.                 }
  60.  
  61.                 isJumping = current.IsKeyMapDown(InputSettings.Jump);
  62.  
  63.                 Acceleration = DoJump(delta, acceleration);
  64.  
  65.                 Velocity = new Vector2(Velocity.X * (CollisionComponent.IsOnGround ? groundFriction : airFriction), Velocity.Y);
  66.  
  67.                 base.Update(gameTime);
  68.  
  69.                 //Apply gravity
  70.                 if (!CollisionComponent.IsOnGround)
  71.                     Acceleration = new Vector2(Acceleration.X, Acceleration.Y + gravity);
  72.                 else { }
  73.  
  74.                 wasJumping = isJumping;
  75.  
  76.                 Clamp();
  77.  
  78.                 if (Bounds.Bottom > Owner.Height)
  79.                     Die();
  80.             }
  81.         }
  82.  
  83.  
  84.         private Vector2 DoJump(float delta, float accelerationX)
  85.         {
  86.             //We invert accelerationY at the end, so invert current Acceleration here.
  87.             float accelerationY = -Acceleration.Y;
  88.  
  89.             if (isJumping)
  90.             {
  91.                 //Begin or continue jump
  92.                 if ((!wasJumping && CollisionComponent.IsOnGround) || jumpTime > 0f)
  93.                 {
  94.                     if (jumpTime == 0f)
  95.                     {
  96.                         //Started a jump
  97.                         //TODO: play sound or animation etc.
  98.                     }
  99.  
  100.                     jumpTime += delta;
  101.                 }
  102.  
  103.                 //If the player is ascending
  104.                 if (jumpTime > 0f && jumpTime <= maxJumpTime)
  105.                 {
  106.                     accelerationY = jumpLaunchSpeed * (1f - (float)Math.Pow(jumpTime / maxJumpTime, jumpControlPower));
  107.                 }
  108.                 else
  109.                 {
  110.                     //The player has reached the apex
  111.                     jumpTime = 0f;
  112.                 }
  113.             }
  114.             else
  115.             {
  116.                 //Cancel the jump
  117.                 jumpTime = 0f;
  118.             }
  119.  
  120.             return new Vector2(accelerationX, -accelerationY);
  121.         }
  122.  
  123.         private void Clamp()
  124.         {
  125.             Velocity = new Vector2(MathHelper.Clamp(Velocity.X, -maxSpeedX, maxSpeedX),
  126.                                    MathHelper.Clamp(Velocity.Y, -maxSpeedY, maxSpeedY));
  127.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement