matrix-workadventure

Experimental workadventure map for matrix.org using an experimental support of ldtk
git clone git://archive.git.mtrnord.blog/MTRNord/matrix-workadventure.git
Log | Files | Refs | README

postprocess_map_export.ts (2801B)


      1 import fs from "node:fs";
      2 import path from "node:path";
      3 
      4 
      5 const files = await fs.promises.opendir(".");
      6 const ldtk_file = process.argv[2]
      7 
      8 const teleport_areas_raw = await fs.promises.readFile("./teleport_areas.json", { encoding: "utf-8" })
      9 const teleport_areas = JSON.parse(teleport_areas_raw)
     10 
     11 for await (const file of files) {
     12     if (file.isFile() && file.name.endsWith(".tmj")) {
     13         const tiled_file = path.join(file.path, file.name)
     14         const level_name = file.name.replace(".tmj", "").replace(/^[0-9]{4}/, "")
     15         const tiled_clean_file = path.join(file.path, `${level_name}.tmj`)
     16         // Parse the map
     17         const ldtk_map_raw = await fs.promises.readFile(ldtk_file, { encoding: "utf-8" })
     18         const ldtk_map = JSON.parse(ldtk_map_raw);
     19         const tiled_map_raw = await fs.promises.readFile(tiled_file, { encoding: "utf-8" })
     20         const tiled_map = JSON.parse(tiled_map_raw);
     21 
     22         const ldtk_layer = ldtk_map.levels.find((x: any) => x.identifier === level_name)
     23 
     24         for (const layer of tiled_map.layers) {
     25             if (layer.type == "objectgroup") {
     26                 for (const object of layer.objects) {
     27                     if (!object.ldtk_fixed) {
     28                         // Yes this really has to be backwards ^^
     29                         const entity_id = object.properties.find((x: any) => x.name === "entity_id").value
     30                         // Find the same entity over in the ldtk mapfile
     31                         const entities = ldtk_layer.layerInstances.find((x: any) => x.__type === "Entities")
     32 
     33                         for (const entity of entities.entityInstances) {
     34                             if (entity.fieldInstances.find((y: any) => y.__identifier === "entity_id").__value === entity_id) {
     35                                 object.width = entity.width
     36                                 object.height = entity.height
     37                                 object.ldtk_fixed = true
     38                             }
     39                         }
     40                     }
     41                 }
     42             }
     43             if (layer.type == "tilelayer" && !layer.ldtk_fixed) {
     44                 const teleport_area = teleport_areas.areas.find((x: any) => x.layer_name === layer.name)
     45                 if (teleport_area) {
     46                     if (!layer.properties) {
     47                         layer.properties = []
     48                     }
     49                     layer.properties.push({
     50                         "name": "exitUrl",
     51                         "type": "string",
     52                         "value": teleport_area.exitUrl
     53                     })
     54                     layer.ldtk_fixed = true
     55                 }
     56             }
     57         }
     58 
     59         // Write the file back
     60         await fs.promises.writeFile(tiled_clean_file, JSON.stringify(tiled_map), { encoding: "utf-8" })
     61     }
     62 }