Class MapUtilities


public final class MapUtilities
Static utility methods for working with TMX-style maps, map objects, tiles and tile coordinates. Most overloads default to the map of the currently loaded environment.
  • Method Details

    • getBounds

      public static Rectangle2D getBounds(IMapObject... objects)
      Computes the combined axis-aligned bounding box of the supplied map objects.
      Parameters:
      objects - the map objects whose bounding boxes are merged
      Returns:
      the union bounding box
    • getMaxMapId

      public static int getMaxMapId(IMap map)
      Returns the maximum id of any map object on the given map. Returns 0 if the map is null or contains no map object layers.
      Parameters:
      map - the map to inspect
      Returns:
      the maximum map object id
    • getTileBoundingBox

      public static Rectangle2D getTileBoundingBox(IMap map, Rectangle2D box)
      Snaps the given pixel-space rectangle to the bounding box of the tiles it covers on the given map.
      Parameters:
      map - the map providing tile size and orientation
      box - the pixel-space rectangle to snap
      Returns:
      the bounding box that covers all enclosed tiles
    • getTile

      public static Point getTile(Point2D mapLocation)
      Get the corresponding tile for a given pixel map location. This is an overload taking the Map from the current environment to calculate a tile location.
      Parameters:
      mapLocation - the pixel map location.
      Returns:
      The x / y tile coordinate for the given location.
      See Also:
    • getTile

      public static Point getTile(IMap map, Point2D mapLocation)
      Get the corresponding tile for a given pixel map location.
      Parameters:
      map - The map on which to calculate the tile location.
      mapLocation - the pixel map location.
      Returns:
      The x / y tile coordinate for the given mapLocation.
    • isStaggeredRowOrColumn

      public static boolean isStaggeredRowOrColumn(StaggerIndex staggerIndex, int index)
      Check if the row or column with the given index is staggered.
      Parameters:
      staggerIndex - the staggerIndex property of the map. Every second row (or column, depending on the StaggerAxis of the map is staggered half a tile.
      index - the index of the current row or column for which we want to determine if it's staggered or not.
      Returns:
      a boolean representing if the row or column with the given index is staggered.
    • getMapLocation

      public static Point2D getMapLocation(IMap map, Point tileLocation)
      Converts a tile coordinate to its pixel-space top-left location on the given map.
      Parameters:
      map - the map providing tile size
      tileLocation - the tile coordinate
      Returns:
      the pixel-space location of the tile's top-left corner
    • getTileImageLocation

      public static Point2D getTileImageLocation(IMap map, ITile tile)
      Gets the top-left map location at which the image of a tile is rendered.
      Parameters:
      map - the map containing the tile
      tile - the tile
      Returns:
      the rendered image location, or null if the tile has no image or coordinate
    • getTilesByPixelLocation

      public static List<ITile> getTilesByPixelLocation(IMap map, Point2D location)
      Returns all tiles from any tile layer on the given map that contain the supplied pixel-space location.
      Parameters:
      map - the map to inspect
      location - the pixel-space location
      Returns:
      the matching tiles, ordered by layer (bottom to top)
    • getTopMostTile

      public static ITile getTopMostTile(Point2D location)
      Returns the top-most non-empty tile at the given pixel-space location on the current environment's map.
      Parameters:
      location - the pixel-space location
      Returns:
      the top-most tile, or null if no environment or tile is found
    • getTopMostTile

      public static ITile getTopMostTile(IMap map, Point2D location)
      Returns the top-most non-empty tile at the given pixel-space location on the supplied map.
      Parameters:
      map - the map to inspect
      location - the pixel-space location
      Returns:
      the top-most tile, or null if no tile is found
    • getTopMostTile

      public static ITile getTopMostTile(Point point)
      Returns the top-most non-empty tile at the given tile coordinate on the current environment's map.
      Parameters:
      point - the tile coordinate
      Returns:
      the top-most tile, or null if no environment or tile is found
    • getTopMostTile

      public static ITile getTopMostTile(IMap map, Point point)
      Returns the top-most non-empty tile at the given tile coordinate on the supplied map.
      Parameters:
      map - the map to inspect
      point - the tile coordinate
      Returns:
      the top-most tile, or null if no tile is found
    • findTileSet

      public static ITileset findTileSet(IMap map, ITile tile)
      Searches for the tile set that contains the specified tile, identified by the grid id.
      Parameters:
      map - the map
      tile - the tile
      Returns:
      the tileset
    • convertPolyshapeToPath

      public static Path2D convertPolyshapeToPath(IMapObject mapObject)
      Converts a polyline or polygon map object to a Path2D in absolute (map-space) coordinates.
      Parameters:
      mapObject - the polyline or polygon map object
      Returns:
      the resulting path, or null if the object is not a polyline/polygon or contains no points
    • getAbsolutePolyshapePoints

      public static List<Point2D> getAbsolutePolyshapePoints(IMapObject mapObject)
      Returns the points of a polyline or polygon map object translated to absolute (map-space) coordinates.
      Parameters:
      mapObject - the polyline or polygon map object
      Returns:
      the absolute points, or an empty list if the object is not a polyline/polygon
    • findMapObject

      public static IMapObject findMapObject(IMap map, int id)
      Searches the supplied map for a map object with the given id.
      Parameters:
      map - the map to search
      id - the id to look up
      Returns:
      the matching map object, or null if none exists
    • getTileBoundingBox

      public static Rectangle2D getTileBoundingBox(Point2D mapLocation)
      Returns the bounding box of the tile that contains the given pixel-space location on the current environment's map.
      Parameters:
      mapLocation - the pixel-space location
      Returns:
      the enclosing tile's bounding box
    • getTileBoundingBox

      public static Rectangle2D getTileBoundingBox(IMap map, Point2D mapLocation)
      Returns the bounding box of the tile that contains the given pixel-space location on the supplied map.
      Parameters:
      map - the map providing tile size and orientation
      mapLocation - the pixel-space location
      Returns:
      the enclosing tile's bounding box, or an empty rectangle if map is null
    • getTileBoundingBox

      public static Rectangle2D getTileBoundingBox(int x, int y)
      Returns the bounding box of the tile at the given tile coordinate on the current environment's map.
      Parameters:
      x - the tile x coordinate
      y - the tile y coordinate
      Returns:
      the tile's bounding box
    • getTileBoundingBox

      public static Rectangle2D getTileBoundingBox(IMap map, int x, int y)
      Returns the bounding box of the tile at the given tile coordinate on the supplied map.
      Parameters:
      map - the map providing tile size and orientation
      x - the tile x coordinate
      y - the tile y coordinate
      Returns:
      the tile's bounding box
    • getTileBoundingBox

      public static Rectangle2D getTileBoundingBox(Point tile)
      Returns the bounding box of the tile at the given tile coordinate on the current environment's map.
      Parameters:
      tile - the tile coordinate
      Returns:
      the tile's bounding box
    • getTileBoundingBox

      public static Rectangle2D getTileBoundingBox(IMap map, Point tile)
      Returns the bounding box of the tile at the given tile coordinate on the supplied map.
      Parameters:
      map - the map providing tile size and orientation
      tile - the tile coordinate
      Returns:
      the tile's bounding box, or an empty rectangle if map is null