Enum Class Direction

java.lang.Object
java.lang.Enum<Direction>
com.github.yellowstonegames.grid.Direction
All Implemented Interfaces:
Serializable, Comparable<Direction>, Constable

public enum Direction extends Enum<Direction>
Represents the eight grid directions and the deltaX, deltaY values associated with those directions.
The grid referenced has x positive to the right and y positive upwards on screen.
  • Enum Constant Details

    • UP

      public static final Direction UP
    • DOWN

      public static final Direction DOWN
    • LEFT

      public static final Direction LEFT
    • UP_LEFT

      public static final Direction UP_LEFT
    • UP_RIGHT

      public static final Direction UP_RIGHT
    • DOWN_LEFT

      public static final Direction DOWN_LEFT
    • DOWN_RIGHT

      public static final Direction DOWN_RIGHT
    • NONE

      public static final Direction NONE
  • Field Details

    • CARDINALS

      public static final Direction[] CARDINALS
      An array which holds only the four cardinal directions.
    • CARDINALS_CLOCKWISE

      public static final Direction[] CARDINALS_CLOCKWISE
      An array which holds only the four cardinal directions in clockwise order.
    • CARDINALS_COUNTERCLOCKWISE

      public static final Direction[] CARDINALS_COUNTERCLOCKWISE
      An array which holds only the four cardinal directions in counter-clockwise order.
    • DIAGONALS

      public static final Direction[] DIAGONALS
      An array which holds only the four diagonal directions.
    • OUTWARDS

      public static final Direction[] OUTWARDS
      An array which holds all eight OUTWARDS directions.
    • CLOCKWISE

      public static final Direction[] CLOCKWISE
      An array which holds all eight OUTWARDS directions in clockwise order.
    • COUNTERCLOCKWISE

      public static final Direction[] COUNTERCLOCKWISE
      An array which holds all eight OUTWARDS directions in counter-clockwise order.
    • ALL

      public static final Direction[] ALL
      The cached result of values(), so you can avoid repeatedly allocating Direction[] objects. DO NOT MODIFY THIS ARRAY.
    • deltaX

      public final int deltaX
      The x coordinate difference for this direction.
    • deltaY

      public final int deltaY
      The y coordinate difference for this direction.
    • coord

      public final Coord coord
  • Method Details

    • values

      public static Direction[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static Direction valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • getDirection

      public static Direction getDirection(int x, int y)
      Returns the direction that most closely matches the input. This can be any direction, including cardinal and diagonal directions as well as NONE in the case that x and y are both 0.
      This can be used to get the primary intercardinal direction from an origin point to an event point, such as a mouse click on a grid. If the point given is exactly on a boundary between directions then the direction clockwise is returned.
      Parameters:
      x - the x position relative to the origin (0,0)
      y - the y position relative to the origin (0,0)
      Returns:
      the closest matching Direction enum, which may be NONE
    • getRoughDirection

      public static Direction getRoughDirection(int x, int y)
      Gets an estimate at the correct direction that a position lies in given the distance towards it on the x and y axes. If x and y are both between -1 and 1 inclusive, this will always be accurate, and should be faster than getDirection(int, int) by avoiding trigonometry or any other math on floats. If at least one of x or y is 0, then this will also be accurate and will produce either a cardinal direction or NONE if both are 0. If x and y are both non-zero, this will always produce a diagonal, even if a cardinal direction should be more accurate; this behavior may sometimes be desirable to detect when some position is even slightly off from a true cardinal direction.
      Parameters:
      x - the x position relative to the origin (0,0)
      y - the y position relative to the origin (0,0)
      Returns:
      the Direction that x,y lies in, roughly; will always be accurate for arguments between -1 and 1 inclusive
    • getCardinalDirection

      public static Direction getCardinalDirection(int x, int y)
      Returns the direction that most closely matches the input. This can be any of the four cardinal directions as well as NONE in the case that x and y are both 0.
      This can be used to get the primary cardinal direction from an origin point to an event point, such as a mouse click on a grid. If the point given is directly diagonal then the direction clockwise is returned.
      Parameters:
      x - the x position relative to the origin (0,0)
      y - the y position relative to the origin (0,0)
      Returns:
      the closest matching cardinal Direction enum, which may also be NONE
    • toGoTo

      public static Direction toGoTo(Coord from, Coord to)
      Parameters:
      from - The starting point.
      to - The desired point to reach.
      Returns:
      The direction to follow to go from from to to. It can be cardinal or diagonal.
    • clockwise

      public Direction clockwise()
      Returns the Direction one step clockwise, including diagonals. If considering only Cardinal directions, calling this twice will get the next clockwise cardinal direction.
      Returns:
      the Direction one step clockwise, including diagonals
    • counterClockwise

      public Direction counterClockwise()
      Returns the Direction one step counterclockwise, including diagonals. If considering only Cardinal directions, calling this twice will get the next counterclockwise cardinal direction.
      Returns:
      the Direction one step counterclockwise, including diagonals
    • opposite

      public Direction opposite()
      Returns the direction directly opposite to this one.
      Returns:
      the direction directly opposite to this one
    • isDiagonal

      public boolean isDiagonal()
      Returns:
      Whether this is a diagonal move.
    • isCardinal

      public boolean isCardinal()
      Returns:
      Whether this is a cardinal-direction move.
    • hasUp

      public boolean hasUp()
      Returns:
      true if this has an upward component.
    • hasDown

      public boolean hasDown()
      Returns:
      true if this has a downward component.
    • hasLeft

      public boolean hasLeft()
      Returns:
      true if this has a left component.
    • hasRight

      public boolean hasRight()
      Returns:
      true if this has a right component.