silphius/resources/sprite.js

50 lines
1008 B
JavaScript
Raw Normal View History

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