/// <summary> /// Resets the player to life. /// </summary> /// <param name="position">The position to come to life at.</param> public void Reset(Vector2 position) { Position = position; Velocity = Vector2.Zero; isAlive = true; sprite.PlayAnimation(idleAnimation); player_move_type = enum_move_type.move_type_idle; }
private void GetInput(KeyboardState keyboardState) { // Get analog horizontal movement. shouldLog = false; if (keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)) { movement = -1.0f; if (enum_move_type.move_type_left != player_move_type) { player_idxX = 0; deltaX = GetVelocityX(player_idxX); player_move_type = enum_move_type.move_type_left; } else if (enum_move_type.move_type_left == player_move_type) { player_idxX++; if (player_idxX > MAX_X - 1) { player_idxX = MAX_X - 1; } deltaX = GetVelocityX(player_idxX); } player_move_type = enum_move_type.move_type_left; } else if (keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D)) { movement = 1.0f; if (enum_move_type.move_type_rght != player_move_type) { player_idxX = 0; deltaX = GetVelocityX(player_idxX); player_move_type = enum_move_type.move_type_rght; } else if (enum_move_type.move_type_rght == player_move_type) { player_idxX++; if (player_idxX > MAX_X - 1) { player_idxX = MAX_X - 1; } deltaX = GetVelocityX(player_idxX); } } shouldLog = keyboardState.IsKeyDown(Keys.Enter) && prevKeyboardState.IsKeyUp(Keys.Enter); // Check if the player wants to jump. isJumping = keyboardState.IsKeyDown(Keys.Space); prevKeyboardState = keyboardState; }
/// <summary> /// Updates the player's velocity and position based on input, gravity, etc. /// </summary> public void ApplyPhysics(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; Vector2 previousPosition = Position; int prevPosY = (int)previousPosition.Y; int prevPosX = (int)previousPosition.X; // Base velocity is a combination of horizontal movement control and // acceleration downward due to gravity. if (0 == movement) { player_move_type = enum_move_type.move_type_idle; velocity.X = 0; } if (enum_move_type.move_type_idle != player_move_type) { velocity.X = (int)(player_move_type - 1) * deltaX * 2; // IMPORTANT must multiply by 2 as pre-calc's for 16px } //velocity.X += movement * MoveAcceleration * elapsed; // TODO stevepro - this is the problem line: // once hit the apex of the jump the DoJump() method will reset velocityY to 0 so as not harshly fall. if (!isOnGround) { player_grav++; if (player_grav > COUNT - 1) { player_grav = COUNT - 1; } } else { player_grav = 0; } deltaY = gravityZ[player_grav]; velocity.Y = deltaY * 2; // IMPORTANT must multiply by 2 as pre-calc's for 16px //velocity.Y = MathHelper.Clamp(velocity.Y + GravityAcceleration * elapsed, -MaxFallSpeed, MaxFallSpeed); velocity.Y = DoJump(velocity.Y, gameTime); // Apply pseudo-drag horizontally. //if (IsOnGround) // velocity.X *= GroundDragFactor; //else // velocity.X *= AirDragFactor; //velocity.X *= GroundDragFactor; //velocity.X *= AirDragFactor; // Prevent the player from running faster than his top speed. velocity.X = MathHelper.Clamp(velocity.X, -MaxMoveSpeed, MaxMoveSpeed); // Apply velocity. //var bob = velocity * elapsed; var bobX = velocity.X; // * elapsed; // IMPORTANT pre-calc'd so don't multiply by game tile delta elapsed var bobY = velocity.Y; // * elapsed; // IMPORTANT pre-calc'd so don't multiply by game tile delta elapsed //velocity.X *= elapsed; //velocity.Y *= elapsed; //Position += velocity * elapsed; //Position += velocity; //Position += bob; var bobPos = Position; bobPos.X += bobX; bobPos.Y += bobY; Position = bobPos; Position = new Vector2((float)Math.Round(Position.X), (float)Math.Round(Position.Y)); int currPosY = (int)Position.Y; int currPosX = (int)Position.X; if (0.0 != movement) { int velX = (int)(velocity.X); int delta = currPosX - prevPosX; string msg = String.Format("{0}\t\t{1}\t\t{2}\t\t{3}", velX, currPosX, prevPosX, delta); //Logger.Info(msg); } // If the player is now colliding with the level, separate them. HandleCollisions(); // If the collision stopped us from moving, reset the velocity to zero. if (Position.X == previousPosition.X) { velocity.X = 0; } if (Position.Y == previousPosition.Y) { velocity.Y = 0; player_grav = 0; jumpFrame = 0; } }
/// <summary> /// Gets player horizontal movement and jump commands from input. /// </summary> private void GetInput(KeyboardState keyboardState) { // Get analog horizontal movement. //movement = gamePadState.ThumbSticks.Left.X * MoveStickScale; // Ignore small movements to prevent running in place. //if (Math.Abs(movement) < 0.5f) // movement = 0.0f; // Move the player with accelerometer //if (Math.Abs(accelState.Acceleration.Y) > 0.10f) //{ // // set our movement speed // movement = MathHelper.Clamp(-accelState.Acceleration.Y * AccelerometerScale, -1f, 1f); // // if we're in the LandscapeLeft orientation, we must reverse our movement // if (orientation == DisplayOrientation.LandscapeRight) // movement = -movement; //} shouldLog = false; if (keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)) { movement = -1.0f; if (enum_move_type.move_type_left != player_move_type) { player_idxX = 0; //deltaX = velocityX[player_idxX]; deltaX = GetVelocityX(player_idxX); player_move_type = enum_move_type.move_type_left; } else if (enum_move_type.move_type_left == player_move_type) { player_idxX++; if (player_idxX > MAX_X - 1) { player_idxX = MAX_X - 1; } //deltaX = velocityX[player_idxX]; deltaX = GetVelocityX(player_idxX); //player_move_type = enum_move_type.move_type_left; } player_move_type = enum_move_type.move_type_left; } else if (keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D)) { movement = 1.0f; if (enum_move_type.move_type_rght != player_move_type) { player_idxX = 0; //deltaX = velocityX[player_idxX]; deltaX = GetVelocityX(player_idxX); player_move_type = enum_move_type.move_type_rght; } else if (enum_move_type.move_type_rght == player_move_type) { player_idxX++; if (player_idxX > MAX_X - 1) { player_idxX = MAX_X - 1; } //deltaX = velocityX[player_idxX]; deltaX = GetVelocityX(player_idxX); //player_move_type = move_type_rght; } } shouldLog = keyboardState.IsKeyDown(Keys.Enter) && prevKeyboardState.IsKeyUp(Keys.Enter); // Check if the player wants to jump. isJumping = //gamePadState.IsButtonDown(JumpButton) || keyboardState.IsKeyDown(Keys.Space); // || //keyboardState.IsKeyDown(Keys.Up) || //keyboardState.IsKeyDown(Keys.W);// || //touchState.AnyTouch(); prevKeyboardState = keyboardState; }
/// <summary> /// Updates the player's velocity and position based on input, gravity, etc. /// </summary> public void ApplyPhysics(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; Vector2 previousPosition = Position; int prevPosY = (int)previousPosition.Y; int prevPosX = (int)previousPosition.X; // Base velocity is a combination of horizontal movement control and // acceleration downward due to gravity. if (0 == movement) { player_move_type = enum_move_type.move_type_idle; velocity.X = 0; } if (enum_move_type.move_type_idle != player_move_type) { velocity.X = (int)(player_move_type - 1) * deltaX * 2; } //velocity.X += movement * MoveAcceleration * elapsed; velocity.Y = MathHelper.Clamp(velocity.Y + GravityAcceleration * elapsed, -MaxFallSpeed, MaxFallSpeed); velocity.Y = DoJump(velocity.Y, gameTime); // Apply pseudo-drag horizontally. //if (IsOnGround) // velocity.X *= GroundDragFactor; //else // velocity.X *= AirDragFactor; //velocity.X *= GroundDragFactor; //velocity.X *= AirDragFactor; // Prevent the player from running faster than his top speed. velocity.X = MathHelper.Clamp(velocity.X, -MaxMoveSpeed, MaxMoveSpeed); // Apply velocity. //var bob = velocity * elapsed; var bobX = velocity.X; // * elapsed; // IMPORTANT pre-calc'd so don't multiply by game tile delta elapsed var bobY = velocity.Y * elapsed; //velocity.X *= elapsed; //velocity.Y *= elapsed; //Position += velocity * elapsed; //Position += velocity; //Position += bob; var bobPos = Position; bobPos.X += bobX; bobPos.Y += bobY; Position = bobPos; Position = new Vector2((float)Math.Round(Position.X), (float)Math.Round(Position.Y)); int currPosY = (int)Position.Y; int currPosX = (int)Position.X; if (0.0 != movement) { int velX = (int)(velocity.X); int delta = currPosX - prevPosX; string msg = String.Format("{0}\t\t{1}\t\t{2}\t\t{3}", velX, currPosX, prevPosX, delta); //Logger.Info(msg); } // If the player is now colliding with the level, separate them. HandleCollisions(); // If the collision stopped us from moving, reset the velocity to zero. if (Position.X == previousPosition.X) { velocity.X = 0; } if (Position.Y == previousPosition.Y) { velocity.Y = 0; } if (velocity.Y != 0) { float check = Position.Y - previousPosition.Y; //Logger.Info(velocity.Y.ToString() + " " + check.ToString()); } }