silphius/resources/sprite.js
2024-09-17 01:25:39 -05:00

50 lines
1008 B
JavaScript
Executable File

#!/usr/bin/env node
import {writeFileSync} from 'node:fs';
import {basename, dirname, extname, join} from 'node:path';
import imageSize from 'image-size';
const tileset = process.argv[2];
let w = parseInt(process.argv[3] || '0');
let h = parseInt(process.argv[4] || '0');
const {width, height} = imageSize(tileset);
if (0 === w) {
w = width;
}
if (0 === h) {
h = height;
}
const total = (width / w) * (height / h);
const json = {
frames: {},
meta: {
format: 'RGBA8888',
image: ['.', basename(tileset)].join('/'),
scale: 1,
size: {w: width, h: height},
},
};
const extlessPath = join(dirname(tileset), basename(tileset, extname(tileset)));
let i = 0;
for (let y = 0; y < height; y += h) {
for (let x = 0; x < width; x += w) {
json.frames[1 === total ? '' : join(extlessPath, `${i++}`)] = {
frame: {x, y, w, h},
spriteSourceSize: {x: 0, y: 0, w, h},
sourceSize: {w, h},
};
}
}
writeFileSync(
`${extlessPath}.json`,
JSON.stringify(json),
);