Crate tiled_json_rs

Source
Expand description

A handy crate for parsing the Tiled JSON data in to a usable structure.

The crate includes a few small helper functions on Map, TileSet, and TileLayer. These functions are for common tasks such as generating a cloumn/row location (tiles are stored in a 1D array), a located box on an image for helping with tile-to-tilesheet image picking, and loading files (or strings).

§Examples

use tiled_json_rs as tiled;
let map = tiled::Map::load_from_file(&PathBuf::from("tests/data/csv.json"))
           .expect("Failed to load map");
for tileset in &map.tile_sets {
    let name = tileset.name.clone();
    let mut path = PathBuf::from("assets");
    path.push(tileset.image.clone());
    // Do stuff
}
use tiled::Layer;

fn render_layers(layers: &Vec<Layer>) {
    for layer in layers {
        match &layer.layer_type {
            tiled::LayerType::TileLayer(tiles) => {
                //do_something(tiles);
            }
            tiled::LayerType::Group { layers } => {
                &mut render_layers(layers);
            }
            tiled::LayerType::ImageLayer(image) => {
                //do_something_else(image);
            }
            tiled::LayerType::ObjectGroup(objects) => {
                //and_another_thing(objects);
            }
        }
    }
}

render_layers(&map.layers);

§Info

Tiled can export maps as JSON files. To do so, simply select “File > Export As” and select the JSON file type. You can export json from the command line with the --export-map option.

Notes:

  • GID for tiles starts at 1 with 0 reserved for empty tile
  • Local Id starts at 0 for TileSet, and only applies to TileSet
  • Doc comments are only provided where clarification may be useful. In general things should be named well enough that intention is self-describing.

Structs§

Chunk
Chunks are used to store the tile layer data for infinite maps
Color
An RGBA representation of colours
Frame
Data for an individual frame of animation
Group
Used to group layers if required
ImageLayer
Contains a file path to an image plus a mix colour
Layer
A map can contain any number of layers.
Map
The base structure which contains all data - as in the root of a tree
Object
ObjectGroup
Terrain
Text
Tile
Contains all possible data for a tile including an optional ObjectGroup
TileLayer
TileRect
Used to provide the location and dimensions of the required tile on the tiles tileset image.
TileSet
A tileset that associates information with each tile.
Vec2
A simple representation of a 2d Vector to pass coords around
WangColor
WangSet
Data set for Wang tiles
WangTile

Enums§

DrawOrder
Can be TopDown (default) or Index. Applies to ObjectGroup only.
LayerType
Contains the data for this variant of layer
ObjectType
Contains data for the object sub-types
Orientation
The orientation of the Map
RenderOrder
Rendering direction. Applies only to orthogonal maps
StaggerAxis
Applies only to staggered or hexagonal maps
StaggerIndex
Applies only to staggered or hexagonal maps
TiledValue
A TiledValue is similar to JSON values.