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.
package/lib/isogeom.js ADDED
@@ -0,0 +1,359 @@
1
+ /**
2
+ * 45° 地图编辑器(模块五 · iso)的**几何内核**:纯函数、不碰磁盘、不碰网络。
3
+ *
4
+ * 只有一种格网:**2:1 正交等距**(宽是高的两倍)。这是本模块可靠性的前提,
5
+ * 也是旧模块五(已删除的 AI 地块生成)留下的研究结论。这里不做可切换格网:
6
+ * 正方格俯视由模块六(map)负责,两套口径混在一个文件里迟早会互相污染。
7
+ *
8
+ * ## 坐标口径
9
+ *
10
+ * 世界坐标以格子 (0,0) 的**菱形中心**为原点:
11
+ *
12
+ * cellCenter(r,c) = { x: (c - r) * stepX, y: (c + r) * stepY }
13
+ *
14
+ * 其中 `stepX = cell.width / 2`、`stepY = cell.width / 4` ——
15
+ * **stepY 来自菱形高(= cellWidth/2)的一半,不是 cellHeight/4**。
16
+ * 写成 cellHeight/4 会得到 24(正确 16),整张图就裂了。
17
+ *
18
+ * 因此计划里的 `x/y` 可以是负数;导出 / 相机用 `bounds.originX/originY` 平移。
19
+ *
20
+ * ## 命中测试(容易写成 floor 的地方)
21
+ *
22
+ * 菱形在 (r,c) 坐标里其实是一个**正方形**:对某点做线性反解得到实数 r/c 后,
23
+ * 最近格 = `floor(v + 0.5)`。用 `floor(rReal)` 会在上半格整片偏一格 ——
24
+ * 而且只在菱形的斜边上显形,看起来像「偶尔点不中」。
25
+ *
26
+ * ## 环绕
27
+ *
28
+ * 与模块六同款环面:格坐标允许为负、允许越界,读格取模;像素位置照线性公式算。
29
+ * 视口某一维超过整张地图时收在**地图自己那一轮**,不按视口起点起算。
30
+ */
31
+ /** 默认单元格:64×96。菱形高 32、上下各留 32 给高出地面的物件。 */
32
+ export const DEFAULT_ISO_CELL = { width: 64, height: 96 };
33
+ /** 单边最大格数(与模块六同一档;真正防 OOM 的是导出像素上限)。 */
34
+ export const MAX_ISO_DIM = 512;
35
+ export const MAX_ISO_CELLS = MAX_ISO_DIM * MAX_ISO_DIM;
36
+ /** 计划分块边长(格)。与模块六相同,只影响缓存粒度,不影响落盘格式。 */
37
+ export const ISO_CHUNK_SIZE = 8;
38
+ /** 一次计划最多回多少块(按面积折算:块数 × 块边长² = 一张最大地图)。 */
39
+ export const MAX_ISO_PLAN_CHUNKS = Math.ceil((MAX_ISO_DIM * MAX_ISO_DIM) / (ISO_CHUNK_SIZE * ISO_CHUNK_SIZE));
40
+ /** 物件数量硬上限:防止一篇文章式的项目把计划撑爆。 */
41
+ export const MAX_ISO_OBJECTS = 20_000;
42
+ /** 把任意输入收敛成一份合法单元格;**形状不合法不修**,交给 `assertCell` 抛。 */
43
+ export function normalizeIsoCell(input) {
44
+ const raw = (input ?? {});
45
+ const int = (value, fallback) => {
46
+ const n = typeof value === "number" ? value : Number.parseFloat(String(value ?? ""));
47
+ return Number.isFinite(n) ? Math.round(n) : fallback;
48
+ };
49
+ return {
50
+ width: int(raw.width, DEFAULT_ISO_CELL.width),
51
+ height: int(raw.height, DEFAULT_ISO_CELL.height)
52
+ };
53
+ }
54
+ /** 单元格是否合法;不合法抛 `Error`(文案要能直接告诉用户改成什么)。 */
55
+ export function assertIsoCell(cell) {
56
+ if (!Number.isInteger(cell.width) || cell.width < 16 || cell.width > 512) {
57
+ throw new Error(`格子宽必须是 16~512 的整数,收到 ${String(cell.width)}`);
58
+ }
59
+ if (cell.width % 4 !== 0) {
60
+ throw new Error(`格子宽必须能被 4 整除(菱形半宽 / 半高要落在整数像素上),收到 ${cell.width}`);
61
+ }
62
+ if (!Number.isInteger(cell.height) || cell.height < 32 || cell.height > 768) {
63
+ throw new Error(`格子高必须是 32~768 的整数,收到 ${String(cell.height)}`);
64
+ }
65
+ if (cell.height <= cell.width / 2) {
66
+ throw new Error(`格子高(${cell.height})必须大于菱形高(${cell.width / 2}),否则地块会被裁掉`);
67
+ }
68
+ }
69
+ export function assertIsoMapSize(cols, rows) {
70
+ if (!Number.isInteger(cols) || !Number.isInteger(rows) || cols < 1 || rows < 1) {
71
+ throw new Error(`地图尺寸必须是正整数,收到 ${cols}×${rows}`);
72
+ }
73
+ if (cols > MAX_ISO_DIM || rows > MAX_ISO_DIM) {
74
+ throw new Error(`地图单边最多 ${MAX_ISO_DIM} 格,收到 ${cols}×${rows}。请把地图改小或把格子调大。`);
75
+ }
76
+ }
77
+ export function layoutOf(cell, cols, rows, wrap) {
78
+ assertIsoCell(cell);
79
+ assertIsoMapSize(cols, rows);
80
+ return {
81
+ cell: { width: cell.width, height: cell.height },
82
+ cols,
83
+ rows,
84
+ wrap,
85
+ stepX: cell.width / 2,
86
+ stepY: cell.width / 4,
87
+ diamondHeight: cell.width / 2
88
+ };
89
+ }
90
+ // ── 环绕 ─────────────────────────────────────────────────────────────────
91
+ function mod(value, size) {
92
+ const rest = value % size;
93
+ return rest < 0 ? rest + size : rest;
94
+ }
95
+ export function wrapRow(layout, r) {
96
+ return layout.wrap ? mod(r, layout.rows) : r;
97
+ }
98
+ export function wrapCol(layout, c) {
99
+ return layout.wrap ? mod(c, layout.cols) : c;
100
+ }
101
+ export function wrapCell(layout, r, c) {
102
+ return { r: wrapRow(layout, r), c: wrapCol(layout, c) };
103
+ }
104
+ // ── 格子 ↔ 像素 ──────────────────────────────────────────────────────────
105
+ /** 格子中心(= 菱形中心)。 */
106
+ export function cellCenter(layout, r, c) {
107
+ return { x: (c - r) * layout.stepX, y: (c + r) * layout.stepY };
108
+ }
109
+ /**
110
+ * 一张「整格贴图」贴在格子上时的左上角。
111
+ *
112
+ * 贴图内部菱形必须居中(内置素材与导入素材都按这个约定生成):
113
+ * 左上角 = 菱形中心 − 贴图尺寸的一半。
114
+ */
115
+ export function cellTopLeft(layout, r, c, width, height) {
116
+ const center = cellCenter(layout, r, c);
117
+ return { x: center.x - width / 2, y: center.y - height / 2 };
118
+ }
119
+ export function normalizeFootprint(input) {
120
+ const raw = (input ?? {});
121
+ const int = (value, fallback) => {
122
+ const n = typeof value === "number" ? value : Number.parseFloat(String(value ?? ""));
123
+ return Number.isFinite(n) ? Math.round(n) : fallback;
124
+ };
125
+ return { cols: Math.max(1, int(raw.cols, 1)), rows: Math.max(1, int(raw.rows, 1)) };
126
+ }
127
+ /**
128
+ * 物件**底面中心**:占格各格菱形中心的包围盒中点。
129
+ *
130
+ * 推导:占格 r..r+rows-1、c..c+cols-1,中心坐标是线性的,
131
+ * 所以平均下来 x 多出 `(cols−rows)/2` 个 stepX、y 多出 `(cols+rows−2)/2` 个 stepY。
132
+ * 1×1 时正好退回格子中心;2×2 时落在四格正中(不是锚点格中心)。
133
+ */
134
+ export function objectBase(layout, r, c, footprint) {
135
+ return {
136
+ x: (c - r + (footprint.cols - footprint.rows) / 2) * layout.stepX,
137
+ y: (c + r + (footprint.cols + footprint.rows - 2) / 2) * layout.stepY
138
+ };
139
+ }
140
+ /** 物件图片左上角:底面中心 − 图片尺寸 × 锚点。默认锚点 = 底边中点。 */
141
+ export function objectTopLeft(layout, r, c, footprint, imageWidth, imageHeight, anchor = { x: 0.5, y: 1 }) {
142
+ const base = objectBase(layout, r, c, footprint);
143
+ return { x: base.x - imageWidth * anchor.x, y: base.y - imageHeight * anchor.y };
144
+ }
145
+ /** 物件占到的每一格(**不取模**;调用方按 layout.wrap 决定是否 wrap)。 */
146
+ export function footprintCells(r, c, footprint) {
147
+ const out = [];
148
+ for (let dr = 0; dr < footprint.rows; dr++) {
149
+ for (let dc = 0; dc < footprint.cols; dc++)
150
+ out.push({ r: r + dr, c: c + dc });
151
+ }
152
+ return out;
153
+ }
154
+ /** 物件是否完全落在地图内(允许环绕时按取模判断,但占格必须能一一落回图内)。 */
155
+ export function footprintInside(layout, r, c, footprint) {
156
+ // 物件是一张完整图,不跨环绕接缝:占格必须在 0..rows / 0..cols 内。
157
+ // 允许 wrap 的话,贴边物件会被切成两半(一半在另一头),而图片只有一张。
158
+ for (const cell of footprintCells(r, c, footprint)) {
159
+ if (cell.r < 0 || cell.c < 0 || cell.r >= layout.rows || cell.c >= layout.cols)
160
+ return false;
161
+ }
162
+ return true;
163
+ }
164
+ /**
165
+ * 像素 → 格子,**不取模**。
166
+ *
167
+ * 反解公式见文件头。`floor(v + 0.5)` 对负数也对称(-0.5 落到 0),
168
+ * 与客户端 `ISO_pointToCell` 逐字一致。
169
+ */
170
+ export function pointToCellRaw(layout, x, y) {
171
+ const cReal = (x / layout.stepX + y / layout.stepY) / 2;
172
+ const rReal = (y / layout.stepY - x / layout.stepX) / 2;
173
+ const c = Math.floor(cReal + 0.5);
174
+ const r = Math.floor(rReal + 0.5);
175
+ return { r, c, inMap: r >= 0 && c >= 0 && r < layout.rows && c < layout.cols };
176
+ }
177
+ /** 像素 → 格子。环绕时取模且 `inMap` 恒 true。 */
178
+ export function pointToCell(layout, x, y) {
179
+ const raw = pointToCellRaw(layout, x, y);
180
+ if (!layout.wrap)
181
+ return raw;
182
+ return { r: wrapRow(layout, raw.r), c: wrapCol(layout, raw.c), inMap: true };
183
+ }
184
+ /** 网格脚印的像素包围盒(只按整格贴图算;物件由调用方另外扩)。 */
185
+ export function groundBounds(layout) {
186
+ const { cell } = layout;
187
+ const corners = [
188
+ [0, 0],
189
+ [0, layout.cols - 1],
190
+ [layout.rows - 1, 0],
191
+ [layout.rows - 1, layout.cols - 1]
192
+ ];
193
+ let minX = Infinity;
194
+ let minY = Infinity;
195
+ let maxX = -Infinity;
196
+ let maxY = -Infinity;
197
+ for (const [r, c] of corners) {
198
+ const topLeft = cellTopLeft(layout, r, c, cell.width, cell.height);
199
+ if (topLeft.x < minX)
200
+ minX = topLeft.x;
201
+ if (topLeft.y < minY)
202
+ minY = topLeft.y;
203
+ if (topLeft.x + cell.width > maxX)
204
+ maxX = topLeft.x + cell.width;
205
+ if (topLeft.y + cell.height > maxY)
206
+ maxY = topLeft.y + cell.height;
207
+ }
208
+ const originX = Math.floor(minX);
209
+ const originY = Math.floor(minY);
210
+ return { originX, originY, width: Math.ceil(maxX) - originX, height: Math.ceil(maxY) - originY };
211
+ }
212
+ export function expandBounds(bounds, rect) {
213
+ const left = rect.x;
214
+ const top = rect.y;
215
+ const right = rect.x + rect.width;
216
+ const bottom = rect.y + rect.height;
217
+ if (bounds === undefined || !(bounds.width > 0) || !(bounds.height > 0)) {
218
+ const originX = Math.floor(left);
219
+ const originY = Math.floor(top);
220
+ return { originX, originY, width: Math.max(1, Math.ceil(right) - originX), height: Math.max(1, Math.ceil(bottom) - originY) };
221
+ }
222
+ const minX = Math.min(bounds.originX, Math.floor(left));
223
+ const minY = Math.min(bounds.originY, Math.floor(top));
224
+ const maxX = Math.max(bounds.originX + bounds.width, Math.ceil(right));
225
+ const maxY = Math.max(bounds.originY + bounds.height, Math.ceil(bottom));
226
+ return { originX: minX, originY: minY, width: Math.max(1, maxX - minX), height: Math.max(1, maxY - minY) };
227
+ }
228
+ export function chunkOf(r, c) {
229
+ return { cx: Math.floor(c / ISO_CHUNK_SIZE), cy: Math.floor(r / ISO_CHUNK_SIZE) };
230
+ }
231
+ export function chunkKey(chunk) {
232
+ return `${chunk.cx},${chunk.cy}`;
233
+ }
234
+ export function parseChunkKey(key) {
235
+ const matched = /^(-?\d+),(-?\d+)$/.exec(key);
236
+ if (matched === null)
237
+ return undefined;
238
+ const cx = Number(matched[1]);
239
+ const cy = Number(matched[2]);
240
+ if (!Number.isSafeInteger(cx) || !Number.isSafeInteger(cy))
241
+ return undefined;
242
+ return { cx, cy };
243
+ }
244
+ export function rangeOfChunk(chunk) {
245
+ return {
246
+ r0: chunk.cy * ISO_CHUNK_SIZE,
247
+ c0: chunk.cx * ISO_CHUNK_SIZE,
248
+ r1: (chunk.cy + 1) * ISO_CHUNK_SIZE,
249
+ c1: (chunk.cx + 1) * ISO_CHUNK_SIZE
250
+ };
251
+ }
252
+ export function chunksInRange(range) {
253
+ const out = [];
254
+ if (range.r1 <= range.r0 || range.c1 <= range.c0)
255
+ return out;
256
+ const cx0 = Math.floor(range.c0 / ISO_CHUNK_SIZE);
257
+ const cx1 = Math.floor((range.c1 - 1) / ISO_CHUNK_SIZE);
258
+ const cy0 = Math.floor(range.r0 / ISO_CHUNK_SIZE);
259
+ const cy1 = Math.floor((range.r1 - 1) / ISO_CHUNK_SIZE);
260
+ for (let cy = cy0; cy <= cy1; cy++) {
261
+ for (let cx = cx0; cx <= cx1; cx++)
262
+ out.push({ cx, cy });
263
+ }
264
+ return out;
265
+ }
266
+ export function canonicalChunk(chunk, cols, rows) {
267
+ const nx = Math.max(1, Math.ceil(cols / ISO_CHUNK_SIZE));
268
+ const ny = Math.max(1, Math.ceil(rows / ISO_CHUNK_SIZE));
269
+ return { cx: mod(chunk.cx, nx), cy: mod(chunk.cy, ny) };
270
+ }
271
+ export function allChunks(cols, rows) {
272
+ const out = [];
273
+ const nx = Math.max(1, Math.ceil(cols / ISO_CHUNK_SIZE));
274
+ const ny = Math.max(1, Math.ceil(rows / ISO_CHUNK_SIZE));
275
+ for (let cy = 0; cy < ny; cy++)
276
+ for (let cx = 0; cx < nx; cx++)
277
+ out.push({ cx, cy });
278
+ return out;
279
+ }
280
+ /**
281
+ * 一批格子会弄脏哪些 chunk(规范 key)。
282
+ *
283
+ * 取 3×3 邻域:自动连接会改邻近格的外观,而邻居可能在隔壁 chunk。环绕时
284
+ * `r=0` 的上一行邻居是 `rows-1`,必须落到 `rows-1` 所在的 chunk。
285
+ */
286
+ export function dirtyChunksFor(cells, layout) {
287
+ const keys = new Set();
288
+ const push = (r, c) => {
289
+ const cell = layout.wrap ? wrapCell(layout, r, c) : { r, c };
290
+ if (cell.r < 0 || cell.c < 0 || cell.r >= layout.rows || cell.c >= layout.cols)
291
+ return;
292
+ keys.add(chunkKey(canonicalChunk(chunkOf(cell.r, cell.c), layout.cols, layout.rows)));
293
+ };
294
+ for (const cell of cells) {
295
+ for (let dr = -1; dr <= 1; dr++)
296
+ for (let dc = -1; dc <= 1; dc++)
297
+ push(cell.r + dr, cell.c + dc);
298
+ }
299
+ return [...keys].sort();
300
+ }
301
+ function clampInt(value, low, high) {
302
+ return Math.max(low, Math.min(high, value));
303
+ }
304
+ /**
305
+ * 视口矩形覆盖到的格子范围。
306
+ *
307
+ * 等距下视口矩形反解出来是一个菱形;用四角反解的包围盒即可(再多给 padding)。
308
+ * 环绕下每一维最多铺一整轮,且取**地图自己那一轮**(不能从视口左上角起算,
309
+ * 否则贴边时会漏掉最后一列 / 最后一行)。
310
+ */
311
+ export function viewCellRange(layout, view, padding = 2) {
312
+ const corners = [
313
+ { x: view.x, y: view.y },
314
+ { x: view.x + view.width, y: view.y },
315
+ { x: view.x, y: view.y + view.height },
316
+ { x: view.x + view.width, y: view.y + view.height }
317
+ ].map((point) => pointToCellRaw(layout, point.x, point.y));
318
+ const r0 = Math.min(...corners.map((hit) => hit.r)) - padding;
319
+ const c0 = Math.min(...corners.map((hit) => hit.c)) - padding;
320
+ const r1 = Math.max(...corners.map((hit) => hit.r)) + 1 + padding;
321
+ const c1 = Math.max(...corners.map((hit) => hit.c)) + 1 + padding;
322
+ if (!layout.wrap) {
323
+ return {
324
+ r0: clampInt(r0, 0, layout.rows),
325
+ c0: clampInt(c0, 0, layout.cols),
326
+ r1: clampInt(r1, 0, layout.rows),
327
+ c1: clampInt(c1, 0, layout.cols)
328
+ };
329
+ }
330
+ const whole = (from, to, span) => (to - from > span ? [0, span] : [from, to]);
331
+ const [rr0, rr1] = whole(r0, r1, layout.rows);
332
+ const [cc0, cc1] = whole(c0, c1, layout.cols);
333
+ return { r0: rr0, r1: rr1, c0: cc0, c1: cc1 };
334
+ }
335
+ export function clampRangeToMap(layout, range) {
336
+ return {
337
+ r0: clampInt(range.r0, 0, layout.rows),
338
+ c0: clampInt(range.c0, 0, layout.cols),
339
+ r1: clampInt(range.r1, 0, layout.rows),
340
+ c1: clampInt(range.c1, 0, layout.cols)
341
+ };
342
+ }
343
+ // ── 绘制顺序 ─────────────────────────────────────────────────────────────
344
+ /**
345
+ * 深度键:值越小越先画。
346
+ *
347
+ * 同一图层内按「最靠前的占格 (r+c)」排;`r` / `c` 只做稳定 tie-break。
348
+ * `layerIndex` 乘以一个大常数,保证层序永远优先于层内深度 ——
349
+ * 这正是「地面层先铺完、物件层再按前后盖上去」的语义。
350
+ */
351
+ export function isoDepthZ(layerIndex, r, c, footprint) {
352
+ const depth = r + c + (footprint === undefined ? 0 : footprint.cols + footprint.rows - 2);
353
+ return layerIndex * 1e12 + depth * 1e6 + r * 4096 + c;
354
+ }
355
+ /** 点在不在某个格子的菱形内(调试 / 自检用,命中测试不要用它做逐格扫描)。 */
356
+ export function insideCell(layout, r, c, x, y) {
357
+ const center = cellCenter(layout, r, c);
358
+ return Math.abs(x - center.x) / layout.stepX + Math.abs(y - center.y) / layout.stepY <= 1;
359
+ }