Enum Direction

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<Direction>, Named

    public enum Direction
    extends java.lang.Enum<Direction>
    implements Named
    The directions a Unit can move to. Includes deltas for moving to adjacent squares, which are required due to the isometric map. Starting north and going clockwise.
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      E  
      N  
      NE  
      NW  
      S  
      SE  
      SW  
      W  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Direction​(int oddDX, int oddDY, int evenDX, int evenDY)
      Create a new direction with the given increments.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Direction angleToDirection​(double angle)
      Convert an angle (radians) to a direction.
      Direction[] getClosestDirections​(java.lang.String logMe, java.util.logging.Logger logger, java.util.Random random)
      Creates an array of the directions in an order that favours a supplied one.
      Direction getEWMirroredDirection()
      Gets the direction with east-west part mirrored.
      java.lang.String getKey()
      Get a message key for this direction.
      java.lang.String getNameKey()
      Gets the name of this named object.
      Direction getNextDirection()
      Get the next direction after this one (clockwise).
      Direction getPreviousDirection()
      Get the previous direction after this one (anticlockwise).
      static Direction getRandomDirection​(java.lang.String logMe, java.util.logging.Logger logger, java.util.Random random)
      Gets a random Direction.
      static Direction[] getRandomDirections​(java.lang.String logMe, java.util.logging.Logger logger, java.util.Random random)
      Creates an array of the eight directions in a random order.
      Direction getReverseDirection()
      Gets the reverse direction of this one.
      Direction rotate​(int n)
      Gets this direction rotated by n places.
      Map.Position step​(int x, int y)
      Step the x and y coordinates in this direction.
      static Direction valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static Direction[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • NUMBER_OF_DIRECTIONS

        public static final int NUMBER_OF_DIRECTIONS
      • allDirections

        public static final java.util.List<Direction> allDirections
      • longSides

        public static final java.util.List<Direction> longSides
      • corners

        public static final java.util.List<Direction> corners
      • oddDX

        private final int oddDX
        The direction increments.
      • oddDY

        private final int oddDY
      • evenDX

        private final int evenDX
      • evenDY

        private final int evenDY
    • Constructor Detail

      • Direction

        private Direction​(int oddDX,
                          int oddDY,
                          int evenDX,
                          int evenDY)
        Create a new direction with the given increments.
        Parameters:
        oddDX - Delta X/odd.
        oddDY - Delta y/odd.
        evenDX - Delta X/even.
        evenDY - Delta y/even.
    • Method Detail

      • values

        public static Direction[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (Direction c : Direction.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static Direction valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (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:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • step

        public Map.Position step​(int x,
                                 int y)
        Step the x and y coordinates in this direction.
        Parameters:
        x - The x coordinate.
        y - The y coordinate.
        Returns:
        The map position after the step.
      • rotate

        public Direction rotate​(int n)
        Gets this direction rotated by n places.
        Parameters:
        n - The number of places to rotate (-#directions ≤ n ≤ #directions).
        Returns:
        The rotated direction.
      • getNextDirection

        public Direction getNextDirection()
        Get the next direction after this one (clockwise).
        Returns:
        The next Direction.
      • getPreviousDirection

        public Direction getPreviousDirection()
        Get the previous direction after this one (anticlockwise).
        Returns:
        The previous Direction.
      • getReverseDirection

        public Direction getReverseDirection()
        Gets the reverse direction of this one.
        Returns:
        The reverse Direction.
      • getEWMirroredDirection

        public Direction getEWMirroredDirection()
        Gets the direction with east-west part mirrored.
        Returns:
        The mirrored Direction.
      • getRandomDirection

        public static Direction getRandomDirection​(java.lang.String logMe,
                                                   java.util.logging.Logger logger,
                                                   java.util.Random random)
        Gets a random Direction.
        Parameters:
        logMe - A string to log with the random results.
        logger - A Logger to log to.
        random - A Random number source.
        Returns:
        A random Direction value.
      • getRandomDirections

        public static Direction[] getRandomDirections​(java.lang.String logMe,
                                                      java.util.logging.Logger logger,
                                                      java.util.Random random)
        Creates an array of the eight directions in a random order.
        Parameters:
        logMe - A string to log with the random results.
        logger - A Logger to log to.
        random - A Random number source.
        Returns:
        An array of the Directions in a random order.
      • getClosestDirections

        public Direction[] getClosestDirections​(java.lang.String logMe,
                                                java.util.logging.Logger logger,
                                                java.util.Random random)
        Creates an array of the directions in an order that favours a supplied one. Entry 0 will be the supplied direction, entry 1+2 will be those immediately to the left and right of it (chosen randomly), and so on until the last entry will be the complete reverse of the supplied direction. Useful if we want to travel in a particular direction, but if this fails to be able to try the closest other directions to the original one in order.
        Parameters:
        logMe - A string to log with the random results.
        logger - A Logger to log to.
        random - A Random number source.
        Returns:
        An array of the Directions favouring this one.
      • angleToDirection

        public static Direction angleToDirection​(double angle)
        Convert an angle (radians) to a direction.
        Parameters:
        angle - The angle to convert.
        Returns:
        An equivalent Direction.
      • getKey

        public java.lang.String getKey()
        Get a message key for this direction.
        Returns:
        A suitable message key.
      • getNameKey

        public java.lang.String getNameKey()
        Gets the name of this named object. Try to avoid using this directly except in i18n-related routines or to implement itself in more complext objects.
        Specified by:
        getNameKey in interface Named
        Returns:
        The name of the Named.