Class Player

java.lang.Object
net.vincent.parkourwarrior.Player

public class Player extends Object
The player class for Parkour Warrior.

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

    Fields
    Modifier and Type
    Field
    Description
    boolean
    Whether the player has requested a jump.
    boolean
    Whether the player is colliding downward.
    boolean
    Whether the player is colliding to the left.
    boolean
    Whether the player is colliding to the right.
    boolean
    Whether the player is colliding upward.
    boolean
    Whether the player is currently falling.
    int
    The speed of the player's fall in pixels per frame.
    boolean
    Whether the player is moving left.
    boolean
    Whether the player is moving right.
    int
    The height of the player's jump in pixels.
    boolean
    Whether the player is currently jumping.
    int
    The speed of the player's jump in pixels per frame.
    static final int
    The maximum number of ticks for momentum to last.
    int
    The maximum height the player can reach during a jump.
    int
    Counter for leftward momentum ticks.
    int
    Counter for rightward momentum ticks.
    boolean
    Whether the player has leftward momentum after releasing the left key.
    boolean
    Whether the player has rightward momentum after releasing the right key.
    int
    The player's facing direction.
    final int
    The player's X position on screen, used for camera offset.
    boolean
    Whether the player is currently sneaking.
    int
    The walking speed of the player in pixels per frame.
    int
    The player's X position in the world.
    int
    The player's Y position in the world.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Player(ParkourMain parkourMain)
    Constructs a new Player with a reference to the main game panel and initializes the player's collision area and screen position.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Detects collision below the player.
    void
    Detects collision to the left of the player.
    void
    Detects collision to the right of the player.
    void
    Detects collision above the player.
    void
    drawPlayer(Graphics2D graphics2D)
    Draws the player sprite on the screen.
    void
    Handles the player's falling logic.
    void
    Loads the player's sprite images from the resources.
    void
    Handles the player's jump logic.
    void
    Updates the player's state each frame.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • askJump

      public boolean askJump
      Whether the player has requested a jump.
      Since:
      1.0
    • jumping

      public boolean jumping
      Whether the player is currently jumping.
      Since:
      1.0
    • falling

      public boolean falling
      Whether the player is currently falling.
      Since:
      1.0
    • goRight

      public boolean goRight
      Whether the player is moving right.
      Since:
      1.0
    • goLeft

      public boolean goLeft
      Whether the player is moving left.
      Since:
      1.0
    • sneaking

      public boolean sneaking
      Whether the player is currently sneaking.
      Since:
      1.2
    • collideUp

      public boolean collideUp
      Whether the player is colliding upward.
      Since:
      1.0
    • collideDown

      public boolean collideDown
      Whether the player is colliding downward.
      Since:
      1.0
    • collideLeft

      public boolean collideLeft
      Whether the player is colliding to the left.
      Since:
      1.0
    • collideRight

      public boolean collideRight
      Whether the player is colliding to the right.
      Since:
      1.0
    • worldX

      public int worldX
      The player's X position in the world.
      Since:
      1.0
    • worldY

      public int worldY
      The player's Y position in the world.
      Since:
      1.0
    • maxJumpHeight

      public int maxJumpHeight
      The maximum height the player can reach during a jump.
      Since:
      1.0
    • jumpHeight

      public int jumpHeight
      The height of the player's jump in pixels.
      Since:
      1.0
    • jumpSpeed

      public int jumpSpeed
      The speed of the player's jump in pixels per frame.
      Since:
      1.0
    • fallSpeed

      public int fallSpeed
      The speed of the player's fall in pixels per frame.
      Since:
      1.0
    • walkSpeed

      public int walkSpeed
      The walking speed of the player in pixels per frame.
      Since:
      1.0
    • screenX

      public final int screenX
      The player's X position on screen, used for camera offset.
      Since:
      1.0
    • momentumLeft

      public boolean momentumLeft
      Whether the player has leftward momentum after releasing the left key.
      Since:
      1.3
    • momentumRight

      public boolean momentumRight
      Whether the player has rightward momentum after releasing the right key.
      Since:
      1.3
    • momentumCountLeft

      public int momentumCountLeft
      Counter for leftward momentum ticks.
      Since:
      1.3
    • momentumCountRight

      public int momentumCountRight
      Counter for rightward momentum ticks.
      Since:
      1.3
    • MAX_MOMENTUM_TICKS

      public static final int MAX_MOMENTUM_TICKS
      The maximum number of ticks for momentum to last.
      Since:
      1.3
      See Also:
    • playerDirection

      public int playerDirection
      The player's facing direction.

      1 = left, 2 = right.

      Since:
      1.0
  • Constructor Details

    • Player

      public Player(ParkourMain parkourMain)
      Constructs a new Player with 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

      public void drawPlayer(Graphics2D graphics2D)
      Draws the player sprite on the screen.
      Parameters:
      graphics2D - the Graphics2D context 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 collideDown flag 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 collideDown flag 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 collideLeft flag 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 collideRight flag accordingly.

      Since:
      1.0