Class Hitbox


  • public class Hitbox
    extends java.lang.Object
    Represents an Entities Hitbox.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int height
      The height of the hit box.
      private int width
      The width of the hit box
      private float x
      The x position of the bottom left corner.
      private float y
      The y position of the bottom left corner.
    • Constructor Summary

      Constructors 
      Constructor Description
      Hitbox​(float x, float y, int width, int height)
      Create a new hit box at a specified position with a width and height.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean collidesWith​(Hitbox box)
      Check if the hit box is colliding with another hit box.
      int getHeight()
      Get the height of the hit box.
      int getWidth()
      Get the width of the hit box.
      float getX()
      Get the x coordinate of the hit box.
      float getY()
      Get the y coordinate of the hit box.
      boolean leaves​(Hitbox box)
      Checks if the another hit box has left the area of this hit box.
      void move​(float x, float y)
      Move the hit box's position to the one specified, not additive.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • width

        private final int width
        The width of the hit box
      • height

        private final int height
        The height of the hit box.
      • x

        private float x
        The x position of the bottom left corner.
      • y

        private float y
        The y position of the bottom left corner.
    • Constructor Detail

      • Hitbox

        public Hitbox​(float x,
                      float y,
                      int width,
                      int height)
        Create a new hit box at a specified position with a width and height.
        Parameters:
        x - The x position of the bottom left corner.
        y - The y position of the bottom left corner.
        width - The width of the hit box.
        height - The height of the hit box
    • Method Detail

      • move

        public void move​(float x,
                         float y)
        Move the hit box's position to the one specified, not additive.

        Used to make sure the hit boxes position is the same as the entity it represents.

        Parameters:
        x - The new x position.
        y - The new y position.
      • collidesWith

        public boolean collidesWith​(Hitbox box)
        Check if the hit box is colliding with another hit box.
        Parameters:
        box - The hit box that the collision is being checked against.
        Returns:
        A boolean of if the two hit boxes are intersecting.
      • leaves

        public boolean leaves​(Hitbox box)
        Checks if the another hit box has left the area of this hit box.
        Parameters:
        box - The other hit box.
        Returns:
        A boolean of if the other hit box is partially outside of this hit box
      • getX

        public float getX()
        Get the x coordinate of the hit box.
        Returns:
        A float of the x position of the bottom left corner.
      • getY

        public float getY()
        Get the y coordinate of the hit box.
        Returns:
        A float of the y position of the bottom left corner.
      • getWidth

        public int getWidth()
        Get the width of the hit box.
        Returns:
        An int representing how wide the hit box is.
      • getHeight

        public int getHeight()
        Get the height of the hit box.
        Returns:
        An int representing how tall the hit box is.