Class Player

java.lang.Object
com.skloch.game.Player

public class Player extends Object
A class handling everything needed to control and draw a player, including animation, movement and collision
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private com.badlogic.gdx.math.Rectangle
     
    float
     
    float
     
    private GameObject
     
    com.badlogic.gdx.utils.Array<GameObject>
     
    private com.badlogic.gdx.graphics.g2d.TextureRegion
     
    int
     
    com.badlogic.gdx.math.Rectangle
     
    com.badlogic.gdx.math.Rectangle
     
    boolean
     
    private final com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.Animation<com.badlogic.gdx.graphics.g2d.TextureRegion>>
     
    boolean
     
    int
     
    float
     
    com.badlogic.gdx.math.Rectangle
     
    private float
     
    private final com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.Animation<com.badlogic.gdx.graphics.g2d.TextureRegion>>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Player(String avatar)
    A player character, contains methods to move the player and update animations, also includes collision handling and can be used to trigger events of objects near the player.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a GameObeject to the player's list of collidable objects
    private float
    Returns the euclidian distance from a GameObject to the centre of the player
    float
     
    float
     
    Returns the object that is closest to the player, calculated during move()
    com.badlogic.gdx.graphics.g2d.TextureRegion
    Returns the current frame the player's animation is on
    com.badlogic.gdx.math.Vector3
     
    float
     
    float
     
    boolean
     
    boolean
    Returns if the player is moving or not
    void
    move(float delta)
    Handles all the logic involved in moving the player given keyboard inputs If the player encounters an object, they will not be alowed to move into the space, but will attempt to 'slide' off of it.
    boolean
    Returns whether the player's eventHitbox overlaps an object Call getClosestObject to get the nearest
    private void
    Recalculates the centre of the player, useful after moving the player
    void
    setBounds(com.badlogic.gdx.math.Rectangle bounds)
    Set a large rectangle that the player should be kept inside, set to null to set no bounds
    void
    setCollidables(com.badlogic.gdx.utils.Array<GameObject> collidables)
    Sets the objects the player cannot move into as an Array of GameObjects
    void
    setFrozen(boolean freeze)
    Sets the player to frozen, a frozen player can be set to ignore keyboard inputs in render
    void
    setMoving(boolean moving)
    Sets the player's state to moving or not moving, a not moving character will just display an idle animation
    void
    setPos(float x, float y)
     
    void
    setX(float x)
    Sets the x coordinate of the player, updating all 3 hitboxes at once as opposed to just the sprite rectangle
    void
    setY(float y)
    Sets the Y coordinate of the player, updating all 3 hitboxes at once as opposed to just the sprite rectangle
    void
    Advances the current animation based on the time since the last render The animation frame of the player can be grabbed with getCurrentFrame

    Methods inherited from class java.lang.Object

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

    • sprite

      public com.badlogic.gdx.math.Rectangle sprite
    • feet

      public com.badlogic.gdx.math.Rectangle feet
    • eventHitbox

      public com.badlogic.gdx.math.Rectangle eventHitbox
    • centreX

      public float centreX
    • centreY

      public float centreY
    • direction

      public int direction
    • currentFrame

      private com.badlogic.gdx.graphics.g2d.TextureRegion currentFrame
    • stateTime

      private float stateTime
    • walkingAnimation

      private final com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.Animation<com.badlogic.gdx.graphics.g2d.TextureRegion>> walkingAnimation
    • idleAnimation

      private final com.badlogic.gdx.utils.Array<com.badlogic.gdx.graphics.g2d.Animation<com.badlogic.gdx.graphics.g2d.TextureRegion>> idleAnimation
    • speed

      public float speed
    • collidables

      public com.badlogic.gdx.utils.Array<GameObject> collidables
    • scale

      public int scale
    • bounds

      private com.badlogic.gdx.math.Rectangle bounds
    • closestObject

      private GameObject closestObject
    • frozen

      public boolean frozen
    • moving

      public boolean moving
  • Constructor Details

    • Player

      public Player(String avatar)
      A player character, contains methods to move the player and update animations, also includes collision handling and can be used to trigger events of objects near the player. Includes a feet hitbox for collision and an event hitbox for triggering objects. Call move() then draw the result of getCurrentAnimation() to use
      Parameters:
      avatar - "avatar1" for the more masculine character, "avatar2" for the more feminine character, player animations are packed in the player_sprites atlas
  • Method Details

    • move

      public void move(float delta)
      Handles all the logic involved in moving the player given keyboard inputs If the player encounters an object, they will not be alowed to move into the space, but will attempt to 'slide' off of it. Also updates the player's animation

      Also locates the nearest object after moving, which can be used to trigger events
      Parameters:
      delta - The time passed since the previous render
    • updateAnimation

      public void updateAnimation()
      Advances the current animation based on the time since the last render The animation frame of the player can be grabbed with getCurrentFrame
    • nearObject

      public boolean nearObject()
      Returns whether the player's eventHitbox overlaps an object Call getClosestObject to get the nearest
      Returns:
      true if a player is near enough an object to interact with it
    • getClosestObject

      public GameObject getClosestObject()
      Returns the object that is closest to the player, calculated during move()
      Returns:
      A GameObject that is closest
    • isMoving

      public boolean isMoving()
      Returns if the player is moving or not
      Returns:
      true if the player is moving
    • setMoving

      public void setMoving(boolean moving)
      Sets the player's state to moving or not moving, a not moving character will just display an idle animation
      Parameters:
      moving - The boolean to set moving to
    • getCurrentFrame

      public com.badlogic.gdx.graphics.g2d.TextureRegion getCurrentFrame()
      Returns the current frame the player's animation is on
      Returns:
      TextureRegion the frame of the player's animation
    • setCollidables

      public void setCollidables(com.badlogic.gdx.utils.Array<GameObject> collidables)
      Sets the objects the player cannot move into as an Array of GameObjects
      Parameters:
      collidables - An array of GameObjects that the player should collide with
    • addCollidable

      public void addCollidable(GameObject object)
      Adds a GameObeject to the player's list of collidable objects
      Parameters:
      object - a GameObject for the player to collide with
    • getX

      public float getX()
      Returns:
      The X coordinate of the player
    • getY

      public float getY()
      Returns:
      The Y coordinate of the player
    • getCentreX

      public float getCentreX()
      Returns:
      The X coordinate of the centre point of the player's sprite rectangle
    • getCentreY

      public float getCentreY()
      Returns:
      The Y coordinate of the centre point of the player's sprite rectangle
    • getPosAsVec3

      public com.badlogic.gdx.math.Vector3 getPosAsVec3()
      Returns:
      The Vector3 representation of the bottom left corner of the player's sprite hitbox
    • setX

      public void setX(float x)
      Sets the x coordinate of the player, updating all 3 hitboxes at once as opposed to just the sprite rectangle
    • setY

      public void setY(float y)
      Sets the Y coordinate of the player, updating all 3 hitboxes at once as opposed to just the sprite rectangle
    • setPos

      public void setPos(float x, float y)
      Parameters:
      x - The X coordinate to set the player to
      y - The Y coordinate to set the player to
    • setBounds

      public void setBounds(com.badlogic.gdx.math.Rectangle bounds)
      Set a large rectangle that the player should be kept inside, set to null to set no bounds
      Parameters:
      bounds - The bounds of the playable map
    • distanceFrom

      private float distanceFrom(GameObject object)
      Returns the euclidian distance from a GameObject to the centre of the player
      Parameters:
      object - The object to get the distance from
      Returns:
      The distance from the object
    • recalcCentre

      private void recalcCentre()
      Recalculates the centre of the player, useful after moving the player
    • setFrozen

      public void setFrozen(boolean freeze)
      Sets the player to frozen, a frozen player can be set to ignore keyboard inputs in render
      Parameters:
      freeze - The value to set frozen to
    • isFrozen

      public boolean isFrozen()
      Returns:
      true if the player is frozen