Skip to content

Instantly share code, notes, and snippets.

@quou
Created September 10, 2021 06:45
Show Gist options
  • Save quou/374a246b95c53886af60248e1861dd67 to your computer and use it in GitHub Desktop.
Save quou/374a246b95c53886af60248e1861dd67 to your computer and use it in GitHub Desktop.
Binary tiled exporter
var dat_format = {
name: "Binary",
extension: "dat",
write:function(map, filename) {
var file = new BinaryFile(filename, BinaryFile.WriteOnly);
var tilesets = map.usedTilesets();
/* Write the amount of tilesets. */
var tileset_count_buf = new ArrayBuffer(4);
var tileset_count_view = new Uint32Array(tileset_count_buf);
tileset_count_view[0] = tilesets.length;
file.write(tileset_count_buf);
/* Write the tilesets */
for (var i = 0; i < tilesets.length; i++) {
var tileset = tilesets[i];
/* Write the name length, followed by the name string. */
var name_len_buf = new ArrayBuffer(4);
var name_len_view = new Uint32Array(name_len_buf);
name_len_view[0] = tileset.name.length;
file.write(name_len_buf);
var name_buf = new ArrayBuffer(tileset.name.length);
var name_view = new Uint8Array(name_buf);
for (var ii = 0; ii < tileset.name.length; ii++) {
name_view[ii] = tileset.name.charCodeAt(ii);
}
file.write(name_buf);
var image_path = tileset.image.substring(
tileset.image.lastIndexOf("res"),
tileset.image.length);
/* Write the image path length, followed by the image path string. */
var image_path_len_buf = new ArrayBuffer(4);
var image_path_len_view = new Uint32Array(image_path_len_buf);
image_path_len_view[0] = image_path.length;
file.write(image_path_len_buf);
var image_path_buf = new ArrayBuffer(image_path.length);
var image_path_view = new Uint8Array(image_path_buf);
for (var ii = 0; ii < image_path.length; ii++) {
image_path_view[ii] = image_path.charCodeAt(ii);
}
file.write(image_path_buf);
/* Write tile size */
var size_buf = new ArrayBuffer(8);
var size_view = new Uint32Array(size_buf);
size_view[0] = tileset.tileSize.width;
size_view[1] = tileset.tileSize.height;
file.write(size_buf);
}
/* Write the layer count */
var layer_count_buf = new ArrayBuffer(4);
var layer_count_view = new Uint32Array(layer_count_buf);
layer_count_view[0] = map.layerCount;
file.write(layer_count_buf);
/* Write the layers */
for (var i = 0; i < map.layerCount; i++) {
var layer = map.layerAt(i);
/* Write the layer name length, followed by the bytes of the layer name string. */
var name_len_buf = new ArrayBuffer(4);
var name_len_view = new Uint32Array(name_len_buf);
name_len_view[0] = layer.name.length;
file.write(name_len_buf);
var name_buf = new ArrayBuffer(layer.name.length);
var name_view = new Uint8Array(name_buf);
for (var ii = 0; ii < layer.name.length; ii++) {
name_view[ii] = layer.name.charCodeAt(ii);
}
file.write(name_buf);
if (layer.isTileLayer) {
/* Write the layer type ID */
var type_buf = new ArrayBuffer(4);
var type_view = new Int32Array(type_buf);
type_view[0] = 0;
file.write(type_buf);
/* Write the width and height */
var size_buf = new ArrayBuffer(8);
var size_view = new Uint32Array(size_buf);
size_view[0] = layer.width;
size_view[1] = layer.height;
file.write(size_buf);
for (var y = 0; y < layer.height; y++) {
for (var x = 0; x < layer.width; x++) {
var tile_id = -1;
var tile = layer.tileAt(x, y);
if (tile) {
tile_id = tile.id;
}
if (tile_id >= 0) {
/* Figure out the index of the tileset that this
* tile uses, and use that to add on the tile count,
* to get a sort of "global" tile ID. */
var tileset_idx = 0;
for (var ii = 0; ii < tilesets.length; ii++) {
if (tile.tileset == tilesets[ii]) {
tileset_idx = ii;
}
}
tile_id += tileset_idx * tile.tileset.tileCount;
}
/* Write the ID */
var id_buf = new ArrayBuffer(4);
var id_view = new Int32Array(id_buf);
id_view[0] = tile_id;
file.write(id_buf);
}
}
} else if (layer.isObjectLayer) {
/* Write the layer type ID */
var type_buf = new ArrayBuffer(4);
var type_view = new Int32Array(type_buf);
type_view[0] = 1;
file.write(type_buf);
/* Write the object count */
var obj_count_buf = new ArrayBuffer(4);
var obj_count_view = new Uint32Array(obj_count_buf);
obj_count_view[0] = layer.objectCount;
file.write(obj_count_buf);
for (var ii = 0; ii < layer.objectCount; ii++) {
var obj = layer.objects[ii];
var rect_buf = new ArrayBuffer(4*4);
var rect_view = new Int32Array(rect_buf);
rect_view[0] = obj.x;
rect_view[1] = obj.y;
rect_view[2] = obj.size.width;
rect_view[3] = obj.size.height;
file.write(rect_buf);
}
} else {
/* Layer type ID is -1 because it's an unknown type */
var type_buf = new ArrayBuffer(4);
var type_view = new Int32Array(type_buf);
type_view[0] = -1;
file.write(type_buf);
}
}
file.commit();
}
}
tiled.registerMapFormat("dat", dat_format);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment