Class Player
This class handles the player's movement, jumping, falling, collision detection, and rendering. It manages the player's position in the world and responds to keyboard input to control the character.
- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionbooleanWhether the player has requested a jump.booleanWhether the player is colliding downward.booleanWhether the player is colliding to the left.booleanWhether the player is colliding to the right.booleanWhether the player is colliding upward.booleanWhether the player is currently falling.intThe speed of the player's fall in pixels per frame.booleanWhether the player is moving left.booleanWhether the player is moving right.intThe height of the player's jump in pixels.booleanWhether the player is currently jumping.intThe speed of the player's jump in pixels per frame.static final intThe maximum number of ticks for momentum to last.intThe maximum height the player can reach during a jump.intCounter for leftward momentum ticks.intCounter for rightward momentum ticks.booleanWhether the player has leftward momentum after releasing the left key.booleanWhether the player has rightward momentum after releasing the right key.intThe player's facing direction.final intThe player's X position on screen, used for camera offset.booleanWhether the player is currently sneaking.intThe walking speed of the player in pixels per frame.intThe player's X position in the world.intThe player's Y position in the world. -
Constructor Summary
ConstructorsConstructorDescriptionPlayer(ParkourMain parkourMain) Constructs a newPlayerwith a reference to the main game panel and initializes the player's collision area and screen position. -
Method Summary
Modifier and TypeMethodDescriptionvoidDetects collision below the player.voidDetects collision to the left of the player.voidDetects collision to the right of the player.voidDetects collision above the player.voiddrawPlayer(Graphics2D graphics2D) Draws the player sprite on the screen.voidfall()Handles the player's falling logic.voidLoads the player's sprite images from the resources.voidjump()Handles the player's jump logic.voidUpdates the player's state each frame.
-
Field Details
-
askJump
public boolean askJumpWhether the player has requested a jump.- Since:
- 1.0
-
jumping
public boolean jumpingWhether the player is currently jumping.- Since:
- 1.0
-
falling
public boolean fallingWhether the player is currently falling.- Since:
- 1.0
-
goRight
public boolean goRightWhether the player is moving right.- Since:
- 1.0
-
goLeft
public boolean goLeftWhether the player is moving left.- Since:
- 1.0
-
sneaking
public boolean sneakingWhether the player is currently sneaking.- Since:
- 1.2
-
collideUp
public boolean collideUpWhether the player is colliding upward.- Since:
- 1.0
-
collideDown
public boolean collideDownWhether the player is colliding downward.- Since:
- 1.0
-
collideLeft
public boolean collideLeftWhether the player is colliding to the left.- Since:
- 1.0
-
collideRight
public boolean collideRightWhether the player is colliding to the right.- Since:
- 1.0
-
worldX
public int worldXThe player's X position in the world.- Since:
- 1.0
-
worldY
public int worldYThe player's Y position in the world.- Since:
- 1.0
-
maxJumpHeight
public int maxJumpHeightThe maximum height the player can reach during a jump.- Since:
- 1.0
-
jumpHeight
public int jumpHeightThe height of the player's jump in pixels.- Since:
- 1.0
-
jumpSpeed
public int jumpSpeedThe speed of the player's jump in pixels per frame.- Since:
- 1.0
-
fallSpeed
public int fallSpeedThe speed of the player's fall in pixels per frame.- Since:
- 1.0
-
walkSpeed
public int walkSpeedThe walking speed of the player in pixels per frame.- Since:
- 1.0
-
screenX
public final int screenXThe player's X position on screen, used for camera offset.- Since:
- 1.0
-
momentumLeft
public boolean momentumLeftWhether the player has leftward momentum after releasing the left key.- Since:
- 1.3
-
momentumRight
public boolean momentumRightWhether the player has rightward momentum after releasing the right key.- Since:
- 1.3
-
momentumCountLeft
public int momentumCountLeftCounter for leftward momentum ticks.- Since:
- 1.3
-
momentumCountRight
public int momentumCountRightCounter for rightward momentum ticks.- Since:
- 1.3
-
MAX_MOMENTUM_TICKS
public static final int MAX_MOMENTUM_TICKSThe maximum number of ticks for momentum to last.- Since:
- 1.3
- See Also:
-
playerDirection
public int playerDirectionThe player's facing direction.1 = left, 2 = right.
- Since:
- 1.0
-
-
Constructor Details
-
Player
Constructs a newPlayerwith a reference to the main game panel and initializes the player's collision area and screen position.- Parameters:
parkourMain- the main game panel instance- Since:
- 1.0
-
-
Method Details
-
updatePlayer
public void updatePlayer()Updates the player's state each frame.This method handles movement, collision detection, jumping, falling, momentum, animation cycling, and map completion checks.
- Since:
- 1.0
-
drawPlayer
Draws the player sprite on the screen.- Parameters:
graphics2D- theGraphics2Dcontext to draw on- Since:
- 1.0
-
jump
public void jump()Handles the player's jump logic.Moves the player upward if a jump has been requested and the player has not reached the maximum jump height or collided with a tile above.
- Since:
- 1.0
-
fall
public void fall()Handles the player's falling logic.Moves the player downward when not jumping and not colliding with a tile below.
- Since:
- 1.0
-
getPlayer
public void getPlayer()Loads the player's sprite images from the resources.- Since:
- 1.0
-
detectCollisionUp
public void detectCollisionUp()Detects collision above the player.Checks whether the tiles directly above the player are solid, and updates the
collideDownflag accordingly.- Since:
- 1.0
-
detectCollisionDown
public void detectCollisionDown()Detects collision below the player.Checks whether the tiles directly below the player are solid, and updates the
collideDownflag accordingly.- Since:
- 1.0
-
detectCollisionLeft
public void detectCollisionLeft()Detects collision to the left of the player.Checks whether the tiles to the left of the player are solid, and updates the
collideLeftflag accordingly.- Since:
- 1.0
-
detectCollisionRight
public void detectCollisionRight()Detects collision to the right of the player.Checks whether the tiles to the right of the player are solid, and updates the
collideRightflag accordingly.- Since:
- 1.0
-