Created
June 19, 2020 00:36
-
-
Save mreinstein/734498ff92e1446f2a3a7e01e207c079 to your computer and use it in GitHub Desktop.
resize the tileset used by a tiled map, preserving the col, row locations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let level = require('./level.js') | |
// given a tiled level that references a tileset of certain dimensions, update to a tileset of different dimensions | |
let idx = -1 | |
const locations = [ ] | |
do { | |
idx = level.indexOf('<data encoding="csv">', idx+1) | |
if (idx > 0) | |
locations.push(idx) | |
} while(idx > 0) | |
const originalTilesetColumns = 15 | |
const newTilesetColumns = 120 | |
while (locations.length) { | |
const startIdx = locations.pop() + 21 | |
const endIdx = level.indexOf('</data>', startIdx) | |
// everything in the level up to the data set | |
const pre = level.substring(0, startIdx+1) | |
// the data set | |
const rawData = level.substring(startIdx, endIdx) | |
const data = rawData | |
.trim() | |
.split(',') | |
.map((raw) => parseInt(raw.trim(), 10)) | |
.map((cell) => { | |
// 0 means no tile set | |
if (cell === 0) | |
return 0 | |
// cells are not 0 based, they are 1 based | |
cell-- | |
const col = cell % originalTilesetColumns | |
const row = Math.floor(cell / originalTilesetColumns) | |
const newCell = (row * newTilesetColumns) + col | |
return newCell + 1 | |
}).join(',') | |
// everything after the data set | |
const post = level.substring(endIdx) | |
level = pre + data + post | |
} | |
console.log(level.trim()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment