public void Update(GameTime gameTime, PlayableSurface playableSurface) { if (!GameEngine.loadingMap) { GetInput(); } PhysicsAndCollision(gameTime, playableSurface); if (isInvincible) { invincibilityTime += gameTime.ElapsedGameTime.Milliseconds; if (invincibilityTime >= invincibilityTimeLimit) { isInvincible = false; playerColor.A = 255; } } movement.X = 0; movement.Y = 0; base.Update(gameTime); }
public void PhysicsAndCollision(GameTime gameTime, PlayableSurface playableSurface) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; playerCenter = position.X + (frameSize.X / 2); currentScreen = playerCenter / 1280; screenIndex = (int)currentScreen; nextSection = (int)Math.Round(currentScreen); if (nextSection >= playableSurface.playableSectors.Count()) { nextSection--; } //stop the player from moving too fast velocity.X = MathHelper.Clamp(velocity.X + movement.X * moveSpeed * elapsed, -maxVelocity.X, maxVelocity.X); velocity.Y = MathHelper.Clamp(velocity.Y + gravity * elapsed, -maxVelocity.Y, maxVelocity.Y); if (velocity.X > 800 || velocity.X < -800) { millisecondsPerFrame = 30; } else if (velocity.X > 400 || velocity.X < -400) { millisecondsPerFrame = 40; } else { millisecondsPerFrame = 80; } Jump(gameTime); if (isOnGround) { velocity.X *= GroundDrag; } else { velocity.X *= AirDrag; } if (velocity.X > 0) { isFacingRight = true; } if (velocity.X < 0) { isFacingRight = false; } if (velocity.Y > 0) { isFalling = true; } else { isFalling = false; } if (movement.X < 0.1 && movement.X > -0.1) { isStanding = true; } else { isStanding = false; isDucking = false; } if (isDucking) { collisionOffsetTop = 100; collisionOffsetBottom = 105; } else { collisionOffsetTop = 15; collisionOffsetBottom = 20; } //update the players position position += velocity * elapsed; position = new Vector2((float)Math.Round(position.X), (float)Math.Round(position.Y)); if (isFacingRight) { heldPosition.X = collisionRect.Right; heldPosition.Y = collisionRect.Y + 50; } else { heldPosition.X = collisionRect.Left; heldPosition.Y = collisionRect.Y + 50; } isOnGround = false; Collision(playableSurface.playableSectors[screenIndex].collisionBoxes); Collision(playableSurface.playableSectors[nextSection].collisionBoxes); previousBottom = collisionRect.Bottom; }