mirror of
https://github.com/LucasDower/ObjToSchematic.git
synced 2025-12-11 20:15:30 +01:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { BlockMesh } from '../block_mesh';
|
|
import { IExporter, TStructureExport } from './base_exporter';
|
|
|
|
export class IndexedJSONExporter extends IExporter {
|
|
public override getFormatFilter() {
|
|
return {
|
|
name: 'Indexed JSON',
|
|
extension: 'json',
|
|
};
|
|
}
|
|
|
|
public override export(blockMesh: BlockMesh): TStructureExport {
|
|
const blocks = blockMesh.getBlocks();
|
|
|
|
const blocksUsed = blockMesh.getBlockPalette();
|
|
const blockToIndex = new Map<string, number>();
|
|
const indexToBlock = new Map<number, string>();
|
|
for (let i = 0; i < blocksUsed.length; ++i) {
|
|
blockToIndex.set(blocksUsed[i], i);
|
|
indexToBlock.set(i, blocksUsed[i]);
|
|
}
|
|
|
|
const blockArray = new Array<Array<number>>();
|
|
|
|
// Serialise all block except for the last one.
|
|
for (let i = 0; i < blocks.length; ++i) {
|
|
const block = blocks[i];
|
|
const pos = block.voxel.position;
|
|
blockArray.push([pos.x, pos.y, pos.z, blockToIndex.get(block.blockInfo.name)!]);
|
|
}
|
|
|
|
const json = JSON.stringify({
|
|
blocks: Object.fromEntries(indexToBlock),
|
|
xyzi: blockArray,
|
|
});
|
|
|
|
return { type: 'single', extension: '.json', content: Buffer.from(json) };
|
|
}
|
|
}
|