Class TiledMap

java.lang.Object
org.openpatch.scratch.tiled.TiledMap

public class TiledMap extends Object
A map made with the Tiled editor (TMX or TMJ): its tile layers are shown in the stage's layers and drawn every frame, its object layers can spawn sprites, its tiles carry properties and collision shapes.

Example usage:

TiledMap map = new TiledMap("assets/map.tmx", stage);
map.showLayer("ground", Layer.BACKGROUND);
map.showLayer("treetops", Layer.FOREGROUND);

for (MapObject object : map.getObjectsFromLayer("objects")) {
  if (object.type.equals("player")) {
    Player p = new Player(object.x, object.y);
    stage.add(p);
  }
}

  • Constructor Details

    • TiledMap

      public TiledMap(String path)
      Loads map data without attaching it to a stage. Properties, tile IDs and collision shapes can be read without a window. To stamp or spawn, use the constructor that takes a stage.
      Parameters:
      path - a TMX file
    • TiledMap

      public TiledMap(String path, Stage stage)
      Constructs a new TiledMap object.
      Parameters:
      path - the file path to the Tiled map XML file
      stage - the stage to which this Tiled map belongs
  • Method Details

    • getWidth

      public int getWidth()
      Returns:
      the number of map columns
    • getHeight

      public int getHeight()
      Returns:
      the number of map rows
    • getTileWidth

      public int getTileWidth()
      Returns:
      the width of a map cell in pixels
    • getTileHeight

      public int getTileHeight()
      Returns:
      the height of a map cell in pixels
    • getProperty

      public String getProperty(String name)
      Reads a custom property of the map itself, such as its biome or tutorial.
      Parameters:
      name - property name
      Returns:
      its string value, including multiline strings
      Throws:
      NoSuchElementException - if the property does not exist
    • getProperty

      public String getProperty(String name, String defaultValue)
      Reads a map property, with a default for maps that omit it.
      Parameters:
      name - property name
      defaultValue - value when absent
      Returns:
      the stored value or the default
    • getPropertyInt

      public int getPropertyInt(String name)
      Parameters:
      name - property name
      Returns:
      its integer value
    • getPropertyFloat

      public float getPropertyFloat(String name)
      Parameters:
      name - property name
      Returns:
      its floating-point value
    • getPropertyBoolean

      public boolean getPropertyBoolean(String name)
      Parameters:
      name - property name
      Returns:
      its boolean value
    • getTileId

      public int getTileId(String layerName, int column, int row)
      Reads a global tile ID, without flip bits. Columns and rows are local to the named layer, starting at its top-left cell. Zero means an empty cell, including cells outside the layer. An unknown layer is an error.
      Parameters:
      layerName - tile layer name, also within a group
      column - column index
      row - row index
      Returns:
      global ID, or zero for an empty/outside cell
    • getTileCollisionShapes

      public Shape[] getTileCollisionShapes(int tileId)
      Returns collision outlines defined in a tile's tileset object group. Coordinates start at the tile's top-left; x points right and y points up. Shapes are independent copies and do not modify the map when transformed.
      Parameters:
      tileId - global tile ID (zero and unknown IDs have no shapes)
      Returns:
      rectangles, ellipses or polygons for this tile
    • getTileCollisionShapes

      public Shape[] getTileCollisionShapes(String layerName, int column, int row)
      Returns a cell's collision outlines in map/stage coordinates: the map's top-left is (0,0), x points right and y points up, matching map stamping. Tiled's diagonal, horizontal and vertical flips are applied in that order.
      Parameters:
      layerName - tile layer name
      column - column index, starting at zero
      row - row index, starting at zero
      Returns:
      positioned outlines, or an empty array outside/without collision
    • spawn

      @SafeVarargs public final List<Sprite> spawn(String layerName, Class<? extends Sprite>... types)
      Creates a sprite for each object of a layer whose type is one of the given sprite classes, and adds it to the stage. In Tiled, give an object the type (called "class" in newer versions of Tiled) Player, and map.spawn("Objects", Player.class) makes a new Player() where the object is.

      The sprite's class needs one of these constructors, tried in this order: one that takes the MapObject (to read its name and properties), one that takes (double x, double y), or one without parameters. Either way the sprite is put where the object is. An object drawn as an area (a rectangle, an ellipse or a polygon) also gives the sprite that area as its hitbox, which is how walls and water work without a costume.

      Objects whose type is none of the classes, or that have no type, are left alone, so a layer can hold markers the program reads itself. The classes are named in the program, not looked up by the names in the map: a game exported as a web page only contains the classes its code names.

      TiledMap map = new TiledMap("level.tmx", this);
      map.spawn("Objects", Player.class, Wall.class);
      
      Parameters:
      layerName - the object layer
      types - the sprite classes to make, matched to the objects' types by their names
      Returns:
      the sprites that were made, in the order of the layer
      Throws:
      IllegalArgumentException - if no class is given, or the map has no such object layer
    • getObjectsFromLayer

      public MapObject[] getObjectsFromLayer(String name)
      Retrieves the objects from a specified layer in the tiled map.
      Parameters:
      name - the name of the layer from which to retrieve objects
      Returns:
      an array of MapObject from the specified layer, or null if the layer does not exist
    • showLayer

      public void showLayer(String name, Layer layer)
      Shows a tile layer of the map in a layer of the stage, drawn every frame from now on (only the tiles the camera shows). The map's top left corner is at (0, 0) of the world; the map extends right and down.
      map.showLayer("ground", Layer.BACKGROUND);
      map.showLayer("trees", Layer.FOREGROUND);
      
      Parameters:
      name - the tile layer's name in Tiled
      layer - the stage's layer to draw it in
    • hideLayer

      public void hideLayer(String name)
      Stops drawing a tile layer shown with showLayer(String, Layer).
      Parameters:
      name - the tile layer's name in Tiled
    • toString

      public String toString()
      Overrides:
      toString in class Object