Class Map.Position

  • Enclosing class:
    Map

    public static final class Map.Position
    extends java.lang.Object
    A position on the Map.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      int x
      The coordinates of the position.
      int y
      The coordinates of the position.
    • Constructor Summary

      Constructors 
      Constructor Description
      Position​(int posX, int posY)
      Creates a new Position object with the given coordinates.
      Position​(Map.Position start, Direction direction)
      Creates a new Position from an existing one with an optional step in a given direction.
      Position​(Tile tile)
      Creates a new Position object with the coordinates of a supplied tile.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object o)
      Direction getDirection​(Map.Position other)
      Get the direction from this position to an adjacent position.
      int getDistance​(Map.Position position)
      Gets the distance in tiles between two map positions.
      int getX()
      Gets the x-coordinate of this Position.
      static int getXYDistance​(int ax, int ay, int bx, int by)
      Gets the distance in tiles between two map positions.
      int getY()
      Gets the y-coordinate of this Position.
      int hashCode()
      boolean isValid​(int width, int height)
      Checks whether a position is valid within a given map size.
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

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

      • x

        public final int x
        The coordinates of the position.
      • y

        public final int y
        The coordinates of the position.
    • Constructor Detail

      • Position

        public Position​(int posX,
                        int posY)
        Creates a new Position object with the given coordinates.
        Parameters:
        posX - The x-coordinate for this position.
        posY - The y-coordinate for this position.
      • Position

        public Position​(Tile tile)
        Creates a new Position object with the coordinates of a supplied tile.
        Parameters:
        tile - The Tile to extract coordinates from.
      • Position

        public Position​(Map.Position start,
                        Direction direction)
        Creates a new Position from an existing one with an optional step in a given direction.
        Parameters:
        start - The starting Position.
        direction - An optional Direction to step.
    • Method Detail

      • getX

        public int getX()
        Gets the x-coordinate of this Position.
        Returns:
        The x-coordinate of this Position.
      • getY

        public int getY()
        Gets the y-coordinate of this Position.
        Returns:
        The y-coordinate of this Position.
      • isValid

        public boolean isValid​(int width,
                               int height)
        Checks whether a position is valid within a given map size.
        Parameters:
        width - The width of the map.
        height - The height of the map.
        Returns:
        True if the given position is within the bounds of the map.
      • getXYDistance

        public static int getXYDistance​(int ax,
                                        int ay,
                                        int bx,
                                        int by)
        Gets the distance in tiles between two map positions. With an isometric map this is a non-trivial task. The formula below has been developed largely through trial and error. It should cover all cases, but I wouldn't bet my life on it.
        Parameters:
        ax - The x-coordinate of the first position.
        ay - The y-coordinate of the first position.
        bx - The x-coordinate of the second position.
        by - The y-coordinate of the second position.
        Returns:
        The distance in tiles between the positions.
      • getDistance

        public int getDistance​(Map.Position position)
        Gets the distance in tiles between two map positions. With an isometric map this is a non-trivial task. The formula below has been developed largely through trial and error. It should cover all cases, but I wouldn't bet my life on it.
        Parameters:
        position - The other Position to compare.
        Returns:
        The distance in tiles to the other position.
      • getDirection

        public Direction getDirection​(Map.Position other)
        Get the direction from this position to an adjacent position.
        Parameters:
        other - The adjacent Position.
        Returns:
        The Direction, or null if not adjacent.
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object