dsh-game-material-master 0.9.2 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ /**
2
+ * 45° 地图编辑器(模块五 · iso)的**导出**。
3
+ *
4
+ * 产物与模块六同形:`map.png`(宿主光栅化,与界面同一份计划)+ `map.json`
5
+ * (`dsh-gem-iso-map@1`)+ 可选 `tilesets/**` + 多产物时一个 zip + `manifest.json`。
6
+ *
7
+ * 三条口径:
8
+ * 1. `map.json` 写**语义序号**与物件/标记源数据,不写解析后的贴图;
9
+ * 2. PNG 与界面执行同一份 `z` 排序计划;
10
+ * 3. 每次导出先清空 `export/<安全名>/`,zip 时间戳写死(同文档两次导出逐字节一致)。
11
+ */
12
+ import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
13
+ import { dirname, join } from "node:path";
14
+ import { decodeFile } from "./bitmap.js";
15
+ import { buildIsoPlan, isoBitmapToPng, passCellsOf, planBounds, rasterizeIso } from "./isorender.js";
16
+ import { safeFileName } from "./mapexport.js";
17
+ import { zipEntries } from "./zip.js";
18
+ export const OWN_ISO_JSON_FORMAT = "dsh-gem-iso-map@1";
19
+ /** `dsh-gem-iso-map@1` 的内容。 */
20
+ export function buildIsoJson(context, doc, project) {
21
+ const legend = buildIsoPlan(context, doc, { includeLegend: true }).legend;
22
+ return {
23
+ format: OWN_ISO_JSON_FORMAT,
24
+ projection: "isometric-2:1",
25
+ cell: { width: context.cell.width, height: context.cell.height },
26
+ grid: { stepX: context.layout.stepX, stepY: context.layout.stepY, wrap: context.layout.wrap },
27
+ cols: doc.cols,
28
+ rows: doc.rows,
29
+ project: { id: project.id, name: project.name },
30
+ tilesets: context.tilesets.map((tileset) => ({
31
+ id: tileset.id,
32
+ name: tileset.name,
33
+ image: tileset.file,
34
+ kind: tileset.kind,
35
+ cell: tileset.cell,
36
+ frames: tileset.frames,
37
+ footprint: tileset.footprint,
38
+ anchor: tileset.anchor,
39
+ objectScale: tileset.kind === "object" ? tileset.objectScale : undefined,
40
+ pass: tileset.kind === "object" ? tileset.pass : undefined,
41
+ loop: tileset.loop,
42
+ fps: tileset.fps,
43
+ tileCount: tileset.tiles.length
44
+ })),
45
+ legend: legend.map((entry) => ({ index: entry.index, tilesetId: entry.tilesetId, kind: entry.kind, rect: entry.rect })),
46
+ layers: doc.layers.map((layer) => ({ id: layer.id, name: layer.name, kind: layer.kind })),
47
+ ground: doc.layers
48
+ .filter((layer) => layer.kind === "ground")
49
+ .map((layer) => ({ layerId: layer.id, data: doc.data[layer.id] ?? [] })),
50
+ objects: doc.objects.map((object) => ({ id: object.id, layerId: object.layerId, tilesetId: object.tilesetId, r: object.r, c: object.c })),
51
+ markers: doc.markers.map((marker, index) => ({ n: index + 1, name: marker.name, r: marker.r, c: marker.c })),
52
+ pass: passCellsOf(context, doc)
53
+ };
54
+ }
55
+ async function decodeTilesets(input) {
56
+ const out = new Map();
57
+ for (const tileset of input.context.tilesets) {
58
+ if (tileset.file === "")
59
+ continue;
60
+ const bitmap = await decodeFile(input.tilesetPath(tileset.file)).catch(() => undefined);
61
+ if (bitmap !== undefined)
62
+ out.set(tileset.id, bitmap);
63
+ }
64
+ return out;
65
+ }
66
+ function tilesetExportPath(file) {
67
+ const name = file.split(/[\\/]/u).pop() ?? "";
68
+ return `tilesets/${name}`;
69
+ }
70
+ export async function exportIsoMap(input) {
71
+ const { dir, context, doc } = input;
72
+ const formats = input.formats ?? {};
73
+ const wantPng = formats.png !== false;
74
+ const wantJson = formats.json !== false;
75
+ const wantTilesets = formats.tilesets === true;
76
+ const scale = Math.max(1, Math.round(formats.scale ?? 1));
77
+ const files = [];
78
+ const warnings = [];
79
+ const manifest = { files, warnings, bounds: planBounds(context, doc) };
80
+ await rm(dir, { recursive: true, force: true });
81
+ await mkdir(dir, { recursive: true });
82
+ const write = async (path, content, kind) => {
83
+ const file = join(dir, path);
84
+ await mkdir(dirname(file), { recursive: true });
85
+ const data = typeof content === "string" ? Buffer.from(content, "utf8") : content;
86
+ await writeFile(file, data);
87
+ files.push({ path, bytes: data.length, kind });
88
+ };
89
+ const writeJson = (path, value) => write(path, `${JSON.stringify(value, null, 2)}\n`, "json");
90
+ const content = [];
91
+ if (wantPng) {
92
+ const lookup = input.lookup ?? (await decodeTilesets(input));
93
+ const raster = rasterizeIso(context, doc, lookup, { scale });
94
+ manifest.bounds = raster.bounds;
95
+ warnings.push(...raster.warnings);
96
+ if (raster.drawn === 0)
97
+ warnings.push("这张地图所有图层都是空的,导出的 PNG 是全透明的。");
98
+ await write("map.png", isoBitmapToPng(raster.bitmap), "image");
99
+ manifest.mapPng = "map.png";
100
+ content.push("map.png");
101
+ }
102
+ if (wantTilesets) {
103
+ for (const tileset of context.tilesets) {
104
+ if (tileset.file === "")
105
+ continue;
106
+ const relative = tilesetExportPath(tileset.file);
107
+ try {
108
+ await write(relative, await readFile(input.tilesetPath(tileset.file)), "image");
109
+ content.push(relative);
110
+ }
111
+ catch (error) {
112
+ warnings.push(`素材「${tileset.name}」的原图没能放进包里(${error instanceof Error ? error.message : String(error)})。`);
113
+ }
114
+ }
115
+ }
116
+ if (wantJson) {
117
+ await writeJson("map.json", buildIsoJson(context, doc, input.project));
118
+ manifest.mapJson = "map.json";
119
+ content.push("map.json");
120
+ }
121
+ const wantZip = formats.zip === true || (formats.zip !== false && content.length > 1);
122
+ if (wantZip && content.length > 0) {
123
+ const entries = [];
124
+ for (const path of content)
125
+ entries.push({ path, data: await readFile(join(dir, path)) });
126
+ const zipName = `${safeFileName(doc.name, doc.id)}.zip`;
127
+ await write(zipName, zipEntries(entries), "archive");
128
+ manifest.mapZip = zipName;
129
+ }
130
+ await writeJson("manifest.json", { ...manifest, files: [...manifest.files, { path: "manifest.json", bytes: 0, kind: "json" }] });
131
+ return manifest;
132
+ }
133
+ /** 兼容旧调用:只算导出用的包围盒。 */
134
+ export function isoExportBounds(context, doc) {
135
+ return planBounds(context, doc);
136
+ }
137
+ /** 导出时给界面 / 工具看的素材说明(当前未用,保留一个明确入口)。 */
138
+ export function tilesetFileName(tileset) {
139
+ return tilesetExportPath(tileset.file);
140
+ }