Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* constants */
- private const float accelerationX = 800f;
- private const float maxSpeedX = 5000f;
- private const float runToWalkRatio = 2f;
- private const float groundFriction = 0.68f;
- private const float airFriction = 0.78f;
- private const float maxJumpTime = 1500f;
- private const float jumpLaunchSpeed = 500f;
- private const float gravity = 300f;
- private const float maxSpeedY = 3000f;
- private const float maxFallSpeed = 300f;
- private const float maxFloatSpeed = 300f;
- private const float jumpControlPower = 0.75f;
- public override void Update(GameTime gameTime)
- {
- float delta = gameTime.GetElapsedSeconds();
- var current = InputComponent.Input.Current;
- if (InputComponent.Input.IsNewKeyPress(Keys.B))
- mode = !mode;
- if (mode)
- {
- //This makes the player follow the mouse.
- Vector2 mouse = new Vector2(current.MouseState.X, current.MouseState.Y) + Owner.Camera.Position;
- Velocity = mouse - Position;
- Velocity.Normalize();
- Velocity *= 5;
- base.Update(gameTime);
- }
- else
- {
- float acceleration = 0f;
- if (current.IsKeyMapDown(InputSettings.Left))
- {
- AnimationComponent.SpriteEffects = SpriteEffects.FlipHorizontally;
- acceleration = -accelerationX;
- }
- else if (current.IsKeyMapDown(InputSettings.Right))
- {
- AnimationComponent.SpriteEffects = SpriteEffects.None;
- acceleration = accelerationX;
- }
- if (current.IsKeyMapDown(InputSettings.Run))
- {
- acceleration *= runToWalkRatio;
- }
- isJumping = current.IsKeyMapDown(InputSettings.Jump);
- Acceleration = DoJump(delta, acceleration);
- Velocity = new Vector2(Velocity.X * (CollisionComponent.IsOnGround ? groundFriction : airFriction), Velocity.Y);
- base.Update(gameTime);
- //Apply gravity
- if (!CollisionComponent.IsOnGround)
- Acceleration = new Vector2(Acceleration.X, Acceleration.Y + gravity);
- else { }
- wasJumping = isJumping;
- Clamp();
- if (Bounds.Bottom > Owner.Height)
- Die();
- }
- }
- private Vector2 DoJump(float delta, float accelerationX)
- {
- //We invert accelerationY at the end, so invert current Acceleration here.
- float accelerationY = -Acceleration.Y;
- if (isJumping)
- {
- //Begin or continue jump
- if ((!wasJumping && CollisionComponent.IsOnGround) || jumpTime > 0f)
- {
- if (jumpTime == 0f)
- {
- //Started a jump
- //TODO: play sound or animation etc.
- }
- jumpTime += delta;
- }
- //If the player is ascending
- if (jumpTime > 0f && jumpTime <= maxJumpTime)
- {
- accelerationY = jumpLaunchSpeed * (1f - (float)Math.Pow(jumpTime / maxJumpTime, jumpControlPower));
- }
- else
- {
- //The player has reached the apex
- jumpTime = 0f;
- }
- }
- else
- {
- //Cancel the jump
- jumpTime = 0f;
- }
- return new Vector2(accelerationX, -accelerationY);
- }
- private void Clamp()
- {
- Velocity = new Vector2(MathHelper.Clamp(Velocity.X, -maxSpeedX, maxSpeedX),
- MathHelper.Clamp(Velocity.Y, -maxSpeedY, maxSpeedY));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement