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 std::path::PathBuf;
use tiled_json_rs as tiled;
let map = tiled::Map::load_from_file(&PathBuf::from("tests/data/csv.json"))
.expect("Failed to load map");
# use std::path::PathBuf;
# use tiled_json_rs as tiled;
# let map = tiled::Map::load_from_file(&PathBuf::from("tests/data/csv.json")).unwrap();
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 std::path::PathBuf;
# use tiled_json_rs as tiled;
# let map = tiled::Map::load_from_file(&PathBuf::from("tests/data/csv.json")).unwrap();
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 toTileSet
- The tileset must be included in the JSON (this is temporary until parsing the path is done)
- Doc comments are only provided where clarification may be useful. In general things should be named well enough that intention is self-describing.