@margoengine/tilemap 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +387 -2
- package/dist/autotile.d.ts +105 -0
- package/dist/autotile.js +116 -0
- package/dist/chunks.d.ts +45 -0
- package/dist/fov.d.ts +73 -0
- package/dist/fov.js +107 -0
- package/dist/grid-search-B0VbYKpe.js +98 -0
- package/dist/grid-search.d.ts +33 -0
- package/dist/hex.d.ts +21 -0
- package/dist/hex.js +101 -0
- package/dist/hpa.d.ts +87 -0
- package/dist/hpa.js +239 -0
- package/dist/import-DsL40A38.js +33 -0
- package/dist/import.d.ts +29 -0
- package/dist/index.d.ts +240 -0
- package/dist/index.js +1 -0
- package/dist/iso-C2Hwh5Gr.js +300 -0
- package/dist/iso.d.ts +114 -0
- package/dist/iso.js +3 -0
- package/dist/layout-CLx3KdGC.js +124 -0
- package/dist/layout.d.ts +37 -0
- package/dist/ldtk.d.ts +72 -0
- package/dist/ldtk.js +187 -0
- package/dist/object-quads.d.ts +31 -0
- package/dist/objects-BK2mewd1.js +444 -0
- package/dist/objects.d.ts +88 -0
- package/dist/objects.js +1 -0
- package/dist/path.d.ts +82 -0
- package/dist/path.js +150 -0
- package/dist/plane.d.ts +37 -0
- package/dist/plane.js +76 -0
- package/dist/renderer.d.ts +177 -0
- package/dist/replan.d.ts +58 -0
- package/dist/replan.js +114 -0
- package/dist/src-CJvCfCkX.js +602 -0
- package/dist/tiled-xml-Dp4Ez81m.js +89 -0
- package/dist/tiled-xml.d.ts +3 -0
- package/dist/tiled.d.ts +86 -0
- package/dist/tiled.js +541 -0
- package/package.json +96 -4
package/dist/path.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { a as e } from "./src-CJvCfCkX.js";
|
|
2
|
+
import { a as t, i as n, n as r, o as i, r as a, s as o, t as s } from "./grid-search-B0VbYKpe.js";
|
|
3
|
+
//#region src/path.ts
|
|
4
|
+
function c(t, n = {}) {
|
|
5
|
+
let { property: r = "cost", solid: i = "solid", fallback: a = 1 } = n, o = n.empty ?? a;
|
|
6
|
+
if (!(a > 0)) throw RangeError(`fallback must be above 0, got ${a}.`);
|
|
7
|
+
let s = t.width, c = t.height, l = new Float64Array(s * c).fill(NaN), u = /* @__PURE__ */ new Map(), d = (e) => {
|
|
8
|
+
let n = u.get(e);
|
|
9
|
+
if (n !== void 0) return n;
|
|
10
|
+
let o;
|
|
11
|
+
for (let n of t.tilesets) e >= n.firstGid && e < n.firstGid + n.tileCount && (o = n.tiles?.find((t) => t.id === e - n.firstGid)?.properties);
|
|
12
|
+
let s = o?.[r];
|
|
13
|
+
return n = o?.[i] === !0 ? Infinity : typeof s == "number" && s > 0 ? s : a, u.set(e, n), n;
|
|
14
|
+
}, f = t.layers.filter((e) => e.type === "tile" && (!n.layers || n.layers.includes(e.name)));
|
|
15
|
+
for (let t of f) {
|
|
16
|
+
if (t.width !== s || t.height !== c) throw RangeError(`layer "${t.name}" is ${t.width} x ${t.height}, the map ${s} x ${c}; every tile layer must match the map size.`);
|
|
17
|
+
for (let n = 0; n < l.length; n++) {
|
|
18
|
+
let r = t.data[n] & e;
|
|
19
|
+
if (!r) continue;
|
|
20
|
+
let i = d(r);
|
|
21
|
+
l[n] = Number.isNaN(l[n]) ? i : Math.max(l[n], i);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
for (let e = 0; e < l.length; e++) Number.isNaN(l[e]) && (l[e] = o);
|
|
25
|
+
return {
|
|
26
|
+
width: s,
|
|
27
|
+
height: c,
|
|
28
|
+
cost: l,
|
|
29
|
+
layout: t
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function l(e, r, c, l = {}) {
|
|
33
|
+
let { diagonal: d, max: f, square: p } = a(e, l), m = t(e, r, "start"), h = t(e, c, "goal"), { width: g, cost: _ } = e;
|
|
34
|
+
if (!i(_[h]) || !i(_[m])) return null;
|
|
35
|
+
let v = Infinity, y = 0;
|
|
36
|
+
for (let e = 0; e < _.length; e++) i(_[e]) && (_[e] < v && (v = _[e]), _[e] > y && (y = _[e]));
|
|
37
|
+
if (l.jumpPoints && p && d === "no-corners" && v === y) return u(e, m, h, v, f);
|
|
38
|
+
let b = n(e, p, d, v), x = (e) => b(e, h), S = new Float64Array(_.length).fill(Infinity), C = new Int32Array(_.length).fill(-1), w = new Uint8Array(_.length), T = new s();
|
|
39
|
+
S[m] = 0, T.push(m, x(m));
|
|
40
|
+
let E = 0;
|
|
41
|
+
for (; T.size;) {
|
|
42
|
+
let t = T.pop();
|
|
43
|
+
if (!w[t]) {
|
|
44
|
+
if (w[t] = 1, E++, t === h) break;
|
|
45
|
+
if (E > f) return null;
|
|
46
|
+
o(e, p, d, t, (e, n) => {
|
|
47
|
+
let r = S[t] + _[e] * n;
|
|
48
|
+
r < S[e] && (S[e] = r, C[e] = t, T.push(e, r + x(e)));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!w[h]) return null;
|
|
53
|
+
let D = [];
|
|
54
|
+
for (let e = h; e !== -1; e = C[e]) D.push([e % g, Math.floor(e / g)]);
|
|
55
|
+
return {
|
|
56
|
+
cells: D.reverse(),
|
|
57
|
+
cost: S[h],
|
|
58
|
+
searched: E,
|
|
59
|
+
scanned: 0
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function u(e, t, n, a, o) {
|
|
63
|
+
let { width: c, height: l, cost: u } = e, d = n % c, f = (n - d) / c, p = (e, t) => e >= 0 && t >= 0 && e < c && t < l && i(u[t * c + e]), m = (e, t) => {
|
|
64
|
+
let n = Math.abs(e), i = Math.abs(t);
|
|
65
|
+
return Math.max(n, i) + (r - 1) * Math.min(n, i);
|
|
66
|
+
}, h = new Float64Array(u.length).fill(Infinity), g = new Int32Array(u.length).fill(-1), _ = new Uint8Array(u.length), v = new s(), y = 0, b = (e, t, n, r) => {
|
|
67
|
+
for (;;) {
|
|
68
|
+
if (e += n, t += r, y++, !p(e, t)) return -1;
|
|
69
|
+
if (e === d && t === f) return t * c + e;
|
|
70
|
+
if (n) {
|
|
71
|
+
if (p(e, t - 1) && !p(e - n, t - 1) || p(e, t + 1) && !p(e - n, t + 1)) return t * c + e;
|
|
72
|
+
} else if (p(e - 1, t) && !p(e - 1, t - r) || p(e + 1, t) && !p(e + 1, t - r)) return t * c + e;
|
|
73
|
+
}
|
|
74
|
+
}, x = (e, t, n, r) => {
|
|
75
|
+
for (;;) {
|
|
76
|
+
if (!(p(e + n, t + r) && p(e + n, t) && p(e, t + r))) return -1;
|
|
77
|
+
if (e += n, t += r, y++, e === d && t === f || b(e, t, n, 0) !== -1 || b(e, t, 0, r) !== -1) return t * c + e;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
h[t] = 0, v.push(t, a * m(d - t % c, f - Math.floor(t / c)));
|
|
81
|
+
let S = 0;
|
|
82
|
+
for (; v.size;) {
|
|
83
|
+
let e = v.pop();
|
|
84
|
+
if (_[e]) continue;
|
|
85
|
+
if (_[e] = 1, S++, e === n) break;
|
|
86
|
+
if (S + y > o) return null;
|
|
87
|
+
let t = e % c, r = (e - t) / c, i = g[e], s = [];
|
|
88
|
+
if (i < 0) for (let e = -1; e <= 1; e++) for (let t = -1; t <= 1; t++) (t || e) && s.push([t, e]);
|
|
89
|
+
else {
|
|
90
|
+
let e = i % c, n = Math.sign(t - e), a = Math.sign(r - (i - e) / c);
|
|
91
|
+
if (n && a) s.push([n, 0], [0, a], [n, a]);
|
|
92
|
+
else if (n) {
|
|
93
|
+
s.push([n, 0]);
|
|
94
|
+
for (let e of [-1, 1]) p(t, r + e) && !p(t - n, r + e) && s.push([0, e], [n, e]);
|
|
95
|
+
} else {
|
|
96
|
+
s.push([0, a]);
|
|
97
|
+
for (let e of [-1, 1]) p(t + e, r) && !p(t + e, r - a) && s.push([e, 0], [e, a]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (let [n, i] of s) {
|
|
101
|
+
let o = n && i ? x(t, r, n, i) : b(t, r, n, i);
|
|
102
|
+
if (o < 0 || _[o]) continue;
|
|
103
|
+
let s = o % c, l = (o - s) / c, u = h[e] + a * m(s - t, l - r);
|
|
104
|
+
u < h[o] && (h[o] = u, g[o] = e, v.push(o, u + a * m(d - s, f - l)));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (!_[n]) return null;
|
|
108
|
+
let C = [];
|
|
109
|
+
for (let e = n; e !== -1; e = g[e]) C.push(e);
|
|
110
|
+
C.reverse();
|
|
111
|
+
let w = [[t % c, Math.floor(t / c)]];
|
|
112
|
+
for (let e = 1; e < C.length; e++) {
|
|
113
|
+
let [t, n] = w[w.length - 1], r = C[e] % c, i = Math.floor(C[e] / c), a = Math.sign(r - t), o = Math.sign(i - n);
|
|
114
|
+
for (; t !== r || n !== i;) t += t === r ? 0 : a, n += n === i ? 0 : o, w.push([t, n]);
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
cells: w,
|
|
118
|
+
cost: h[n],
|
|
119
|
+
searched: S,
|
|
120
|
+
scanned: y
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function d(e, n, r = {}) {
|
|
124
|
+
let { diagonal: c, max: l, square: u } = a(e, r), { width: d, height: f, cost: p } = e, m = new Float64Array(p.length).fill(Infinity), h = new Int32Array(p.length).fill(-1), g = new Uint8Array(p.length), _ = new s();
|
|
125
|
+
for (let r of n) {
|
|
126
|
+
let n = t(e, r, "goal");
|
|
127
|
+
i(p[n]) && m[n] !== 0 && (m[n] = 0, _.push(n, 0));
|
|
128
|
+
}
|
|
129
|
+
let v = 0;
|
|
130
|
+
for (; _.size && v < l;) {
|
|
131
|
+
let t = _.pop();
|
|
132
|
+
g[t] || (g[t] = 1, v++, o(e, u, c, t, (e, n) => {
|
|
133
|
+
let r = m[t] + p[t] * n;
|
|
134
|
+
r < m[e] && (m[e] = r, h[e] = t, _.push(e, r));
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
width: d,
|
|
139
|
+
height: f,
|
|
140
|
+
distance: m,
|
|
141
|
+
next: h
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function f(e, t, n) {
|
|
145
|
+
let r = [[t, n]];
|
|
146
|
+
for (let i = e.next[n * e.width + t], a = 0; i !== -1 && a < e.next.length; i = e.next[i], a++) r.push([i % e.width, Math.floor(i / e.width)]);
|
|
147
|
+
return r;
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
|
150
|
+
export { l as findPath, d as flowField, f as followFlow, c as tileCosts };
|
package/dist/plane.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type PassContext } from '@margoengine/core/extension';
|
|
4
|
+
import { ObjectTileMap, type ObjectTileMapOptions } from './objects.js';
|
|
5
|
+
type Context = Extract<PassContext, {
|
|
6
|
+
backend: 'webgpu';
|
|
7
|
+
}>;
|
|
8
|
+
/** An ordered map embedded in a 3D scene. The active core camera projects the plane. */
|
|
9
|
+
export interface PlaneTileMapOptions extends Omit<ObjectTileMapOptions, 'target' | 'clear'> {
|
|
10
|
+
/** Column-major affine map-pixels-to-world transform. Default XY plane, 16 pixels/metre, map y points down. */
|
|
11
|
+
transform?: ArrayLike<number>;
|
|
12
|
+
/** Write scene depth for nonzero-alpha texels. Default true; false for blended transparent planes. */
|
|
13
|
+
depthWrite?: boolean;
|
|
14
|
+
/** Test against scene depth. Default true; false draws over scene geometry. */
|
|
15
|
+
depthTest?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Draw all map layers on one transformed plane with perspective-correct texels and shared scene depth. */
|
|
18
|
+
export declare class PlaneTileMap extends ObjectTileMap {
|
|
19
|
+
/** Column-major affine map-pixels-to-world transform, read each frame. Mutable elements. */
|
|
20
|
+
readonly transform: Float32Array<ArrayBuffer>;
|
|
21
|
+
/** Read each frame. Default true. */
|
|
22
|
+
depthWrite: boolean;
|
|
23
|
+
/** Read each frame. Default true. */
|
|
24
|
+
depthTest: boolean;
|
|
25
|
+
private readonly projected;
|
|
26
|
+
constructor(options: PlaneTileMapOptions);
|
|
27
|
+
/** Create and attach a plane map to the engine. */
|
|
28
|
+
static attach(instance: EngineKernel, options: PlaneTileMapOptions): PlaneTileMap;
|
|
29
|
+
protected projection(c: Context, _width: number, _height: number): Float32Array<ArrayBuffer>;
|
|
30
|
+
protected bounds(): undefined;
|
|
31
|
+
protected depth(c: Context): {
|
|
32
|
+
attachment: GPURenderPassDepthStencilAttachment;
|
|
33
|
+
state: GPUDepthStencilState;
|
|
34
|
+
};
|
|
35
|
+
private checkTransform;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
package/dist/plane.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const n=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { k as e } from "./src-CJvCfCkX.js";
|
|
5
|
+
import { t } from "./objects-BK2mewd1.js";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
//#region src/plane.ts
|
|
8
|
+
var r = class r extends t {
|
|
9
|
+
constructor(t) {
|
|
10
|
+
super({
|
|
11
|
+
...t,
|
|
12
|
+
snap: t.snap ?? !1,
|
|
13
|
+
parallax: t.parallax ?? !1
|
|
14
|
+
}), this.projected = /* @__PURE__ */ new Float32Array(16);
|
|
15
|
+
let n = t.transform ?? [
|
|
16
|
+
1 / 16,
|
|
17
|
+
0,
|
|
18
|
+
0,
|
|
19
|
+
0,
|
|
20
|
+
0,
|
|
21
|
+
-1 / 16,
|
|
22
|
+
0,
|
|
23
|
+
0,
|
|
24
|
+
0,
|
|
25
|
+
0,
|
|
26
|
+
1,
|
|
27
|
+
0,
|
|
28
|
+
0,
|
|
29
|
+
0,
|
|
30
|
+
0,
|
|
31
|
+
1
|
|
32
|
+
];
|
|
33
|
+
if (n.length !== 16) throw e("Plane transform needs sixteen affine values", "tilemap-options");
|
|
34
|
+
this.transform = Float32Array.from(n), this.depthWrite = t.depthWrite ?? !0, this.depthTest = t.depthTest ?? !0, this.checkTransform();
|
|
35
|
+
}
|
|
36
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
37
|
+
let i = new r(t);
|
|
38
|
+
return n(e).use(i), i;
|
|
39
|
+
}
|
|
40
|
+
projection(e, t, n) {
|
|
41
|
+
this.checkTransform();
|
|
42
|
+
let r = e.camera.matrix, i = this.transform, a = this.projected;
|
|
43
|
+
for (let e = 0; e < 4; e++) for (let t = 0; t < 4; t++) {
|
|
44
|
+
let n = e * 4;
|
|
45
|
+
a[n + t] = r[t] * i[n] + r[4 + t] * i[n + 1] + r[8 + t] * i[n + 2] + r[12 + t] * i[n + 3], e < 2 && (a[n + t] *= this.camera.zoom);
|
|
46
|
+
}
|
|
47
|
+
return a;
|
|
48
|
+
}
|
|
49
|
+
bounds() {}
|
|
50
|
+
depth(t) {
|
|
51
|
+
let n = t.descriptor.depthStencil?.format;
|
|
52
|
+
if (!n) throw e("PlaneTileMap needs a scene depth attachment", "tilemap-options");
|
|
53
|
+
return {
|
|
54
|
+
attachment: {
|
|
55
|
+
view: t.depth,
|
|
56
|
+
depthLoadOp: "load",
|
|
57
|
+
depthStoreOp: "store",
|
|
58
|
+
...n.includes("stencil") ? {
|
|
59
|
+
stencilLoadOp: "load",
|
|
60
|
+
stencilStoreOp: "store"
|
|
61
|
+
} : {}
|
|
62
|
+
},
|
|
63
|
+
state: {
|
|
64
|
+
format: n,
|
|
65
|
+
depthCompare: this.depthTest ? "greater-equal" : "always",
|
|
66
|
+
depthWriteEnabled: this.depthWrite
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
checkTransform() {
|
|
71
|
+
let t = this.transform;
|
|
72
|
+
if (!t.every(Number.isFinite) || t[3] !== 0 || t[7] !== 0 || t[11] !== 0 || t[15] !== 1 || !(Math.hypot(t[1] * t[6] - t[2] * t[5], t[2] * t[4] - t[0] * t[6], t[0] * t[5] - t[1] * t[4]) > 0)) throw e("Invalid plane transform; use finite affine values and independent map axes", "tilemap-options");
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
//#endregion
|
|
76
|
+
export { r as PlaneTileMap };
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import type { TileLayer, TileMapData } from './index.js';
|
|
5
|
+
/** Straight-alpha RGBA8 texels, row by row from the top: `ImageData` fits. */
|
|
6
|
+
export interface TilePixels {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
data: Uint8Array | Uint8ClampedArray;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Where a tileset's texels come from. Pixels and `ImageBitmap`s are uploaded to an `rgba8unorm` texture the map owns and
|
|
13
|
+
* uploads again after a device loss. A function is called with the device, also after a device loss. A plain
|
|
14
|
+
* `GPUTexture` stays the caller's: held at a device restore it is reported once through `engine.onError` and its tiles
|
|
15
|
+
* are skipped until {@link TileMap.setAtlas} gives a new one (docs/conventions.md, "Borrowed GPU objects").
|
|
16
|
+
*/
|
|
17
|
+
export type TileAtlas = TilePixels | ImageBitmap | GPUTexture | ((device: GPUDevice) => GPUTexture);
|
|
18
|
+
/**
|
|
19
|
+
* A render target, or a function read each frame for one that changes. A pixel layer's `target` fits. A plain texture
|
|
20
|
+
* held at a device restore is reported once through `engine.onError` and the map draws nothing until `setTarget`.
|
|
21
|
+
*/
|
|
22
|
+
export type TileTarget = GPUTexture | (() => GPUTexture);
|
|
23
|
+
/** Map pixels to target pixels: the map point at the top left and target pixels per map pixel. A pixel layer's `camera` fits. */
|
|
24
|
+
export interface TileCamera {
|
|
25
|
+
readonly x: number;
|
|
26
|
+
readonly y: number;
|
|
27
|
+
readonly zoom: number;
|
|
28
|
+
}
|
|
29
|
+
/** Settings of {@link TileMap}, fixed when it is created except where a member says otherwise. */
|
|
30
|
+
export interface TileMapOptions {
|
|
31
|
+
/** The map to draw. Required. */
|
|
32
|
+
map: TileMapData;
|
|
33
|
+
/** The texels of each tileset by its name; a tileset no drawn tile uses can be left out. Required. */
|
|
34
|
+
atlases: {
|
|
35
|
+
readonly [tileset: string]: TileAtlas;
|
|
36
|
+
};
|
|
37
|
+
/** Read each frame. Default `{x: 0, y: 0, zoom: 1}`: map pixels are target pixels. */
|
|
38
|
+
camera?: TileCamera;
|
|
39
|
+
/** Rounds each layer's top left to a whole target pixel, read each frame. Default true. */
|
|
40
|
+
snap?: boolean;
|
|
41
|
+
/** Names of the tile layers to draw, in map order. Default every tile layer. */
|
|
42
|
+
mapLayers?: readonly string[];
|
|
43
|
+
/**
|
|
44
|
+
* Scrolls each map layer by its own `parallax`, read each frame. Set it to false when the camera already carries it, as
|
|
45
|
+
* a pixel layer's `camera` does. Default true.
|
|
46
|
+
*/
|
|
47
|
+
parallax?: boolean;
|
|
48
|
+
/** Draw here instead of the canvas or the view's target. Default the frame's colour target. */
|
|
49
|
+
target?: TileTarget;
|
|
50
|
+
/**
|
|
51
|
+
* With `target`: the colour `[r, g, b, a]` (0 to 1) to clear it to first, or false to draw over it. Default the map's
|
|
52
|
+
* `backgroundColor`, or `[0, 0, 0, 0]` without one.
|
|
53
|
+
*/
|
|
54
|
+
clear?: readonly [number, number, number, number] | false;
|
|
55
|
+
/** Frame-graph order. Runs after the scene (0); 40 draws after a Tonemap. Default 10. */
|
|
56
|
+
order?: number;
|
|
57
|
+
/** Render-layer bits; a view whose `layers` shares none leaves this map out. Default 1. */
|
|
58
|
+
layers?: number;
|
|
59
|
+
/** Plugin name, unique per scene. Default `tilemap-1`, `tilemap-2`, ... */
|
|
60
|
+
name?: string;
|
|
61
|
+
}
|
|
62
|
+
/** What the last frame cost, for a budget check or a test. */
|
|
63
|
+
export interface TileMapStats {
|
|
64
|
+
/** Draw calls in the last frame: one per visible chunk per tileset it uses. */
|
|
65
|
+
draws: number;
|
|
66
|
+
/** Bytes of tile values uploaded in the last frame: 16,384 per changed chunk. */
|
|
67
|
+
uploadedBytes: number;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Draws a {@link TileMapData}'s tile layers, one instanced draw per visible 64 x 64 chunk per tileset, with exact pixels
|
|
71
|
+
* when the target has one sample per pixel. Tile values live in a storage buffer and only changed chunks go up again.
|
|
72
|
+
* Animated tiles change a small lookup in the shader, not the cells. Orthogonal maps only; object layers are not drawn.
|
|
73
|
+
* WebGPU only.
|
|
74
|
+
* @throws TypeError / RangeError on invalid options or, in a frame, a tileset with no atlas or larger than its atlas.
|
|
75
|
+
* @throws MargoError `'unsupported'` on a WebGL engine or for a drawn image-collection tileset, `'disposed'` after
|
|
76
|
+
* `dispose()`. A plain `GPUTexture` atlas or target held at a device restore is reported once through `engine.onError`
|
|
77
|
+
* (`'state'`, help `borrowed-lost`) and the frame goes on without it until {@link TileMap.setAtlas} or
|
|
78
|
+
* {@link TileMap.setTarget} gives a new one.
|
|
79
|
+
*/
|
|
80
|
+
export declare class TileMap implements EnginePlugin {
|
|
81
|
+
readonly name: string;
|
|
82
|
+
/** Map pixels to target pixels, read each frame. */
|
|
83
|
+
camera: TileCamera;
|
|
84
|
+
/** Rounds each layer's top left to a whole target pixel, read each frame. */
|
|
85
|
+
snap: boolean;
|
|
86
|
+
/** Scrolls each map layer by its own `parallax`, read each frame. */
|
|
87
|
+
parallax: boolean;
|
|
88
|
+
/** The animation clock in milliseconds. Each frame adds its delta; set it to jump or to hold a frame. */
|
|
89
|
+
time: number;
|
|
90
|
+
/** False draws nothing and costs no draw calls, uploads or target clears; true draws as before. Default true. */
|
|
91
|
+
visible: boolean;
|
|
92
|
+
/** What the last frame cost. */
|
|
93
|
+
readonly stats: TileMapStats;
|
|
94
|
+
private readonly order;
|
|
95
|
+
private readonly layerMask;
|
|
96
|
+
private target?;
|
|
97
|
+
private targetSlot;
|
|
98
|
+
private readonly clear?;
|
|
99
|
+
private readonly names?;
|
|
100
|
+
private atlases;
|
|
101
|
+
private readonly slots;
|
|
102
|
+
private data;
|
|
103
|
+
private sets;
|
|
104
|
+
private runs;
|
|
105
|
+
private reach;
|
|
106
|
+
private engine?;
|
|
107
|
+
private device?;
|
|
108
|
+
private module?;
|
|
109
|
+
private layerLayout?;
|
|
110
|
+
private cellsLayout?;
|
|
111
|
+
private setLayout?;
|
|
112
|
+
private pipelineLayout?;
|
|
113
|
+
private readonly pipelines;
|
|
114
|
+
private readonly chunk;
|
|
115
|
+
private readonly range;
|
|
116
|
+
private readonly offsets;
|
|
117
|
+
private readonly next;
|
|
118
|
+
private readonly words;
|
|
119
|
+
private animatedAt;
|
|
120
|
+
private disposed;
|
|
121
|
+
private readonly states;
|
|
122
|
+
private readonly attachments;
|
|
123
|
+
private readonly pass;
|
|
124
|
+
private targetTexture?;
|
|
125
|
+
private targetView?;
|
|
126
|
+
constructor(options: TileMapOptions);
|
|
127
|
+
/** Creates a map renderer and attaches it to `instance`. */
|
|
128
|
+
static attach(instance: EngineKernel, options: TileMapOptions): TileMap;
|
|
129
|
+
/** The map being drawn. */
|
|
130
|
+
get map(): TileMapData;
|
|
131
|
+
/** Draws another map from the next frame: everything goes up again. */
|
|
132
|
+
setMap(map: TileMapData): void;
|
|
133
|
+
/**
|
|
134
|
+
* Replaces the map's own target: a texture, a function read each frame, or undefined for the frame's colour target.
|
|
135
|
+
* After a device restore, hand in a texture made on `engine.gpu` here.
|
|
136
|
+
*/
|
|
137
|
+
setTarget(target: TileTarget | undefined): void;
|
|
138
|
+
/** Replaces a tileset's atlas from the next frame. */
|
|
139
|
+
setAtlas(tileset: string, atlas: TileAtlas): void;
|
|
140
|
+
/**
|
|
141
|
+
* Sets one cell of a tile layer. Only that cell's chunk goes up, on the next frame. Writes straight into `layer.data`
|
|
142
|
+
* need {@link invalidate} instead.
|
|
143
|
+
* @throws RangeError for a cell outside the layer or a layer of another map.
|
|
144
|
+
*/
|
|
145
|
+
setTile(layer: TileLayer | string, x: number, y: number, value: number): void;
|
|
146
|
+
/** Sends a layer's cells up again on the next frame, after writing into `layer.data` yourself. Default every layer. */
|
|
147
|
+
invalidate(layer?: TileLayer | string): void;
|
|
148
|
+
setup(engine: Engine): void;
|
|
149
|
+
/**
|
|
150
|
+
* Moves the tile animation clock (`time`, in milliseconds) by `delta` seconds without drawing (a delta of 0 or less
|
|
151
|
+
* moves nothing). Each frame update runs it, so `seek` from `@margoengine/cutscene/seek` replays it exactly.
|
|
152
|
+
*/
|
|
153
|
+
advanceTime(delta: number): void;
|
|
154
|
+
onPreUpdate(delta: number): void;
|
|
155
|
+
onDeviceRestored(): void;
|
|
156
|
+
dispose(): void;
|
|
157
|
+
[Symbol.dispose]: () => void;
|
|
158
|
+
private runFor;
|
|
159
|
+
private releaseMap;
|
|
160
|
+
private releaseDevice;
|
|
161
|
+
private prepare;
|
|
162
|
+
private pipeline;
|
|
163
|
+
/** Sends changed chunks up and recounts their tilesets. Several views in a frame upload once. */
|
|
164
|
+
private flush;
|
|
165
|
+
/** The atlas slot of a tileset, made on first use from the atlas given for it. */
|
|
166
|
+
private slot;
|
|
167
|
+
/**
|
|
168
|
+
* The bind group of a tileset some cell uses; made on first use, so an unused tileset needs no atlas. Undefined while
|
|
169
|
+
* a caller's atlas is lost with the device: its tiles are skipped.
|
|
170
|
+
*/
|
|
171
|
+
private setGroup;
|
|
172
|
+
/** Writes the frames that changed since the last time this clock value was drawn. */
|
|
173
|
+
private animate;
|
|
174
|
+
/** The layer's top left in target pixels. */
|
|
175
|
+
private origin;
|
|
176
|
+
private draw;
|
|
177
|
+
}
|
package/dist/replan.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { CostGrid, GridPath, PathOptions } from './path.js';
|
|
2
|
+
/** Settings of {@link PathReplanner}. */
|
|
3
|
+
export interface ReplanOptions {
|
|
4
|
+
/** Corner steps, as `findPath` takes them. Default `'no-corners'`. */
|
|
5
|
+
diagonal?: PathOptions['diagonal'];
|
|
6
|
+
/** Stops one `findPath` call after this many expanded cells; the work done is kept. Default 1,000,000. */
|
|
7
|
+
maxCells?: number;
|
|
8
|
+
/**
|
|
9
|
+
* The lowest cost an open cell will ever have, which the distance estimate is multiplied by. A changed cell below it
|
|
10
|
+
* starts the search afresh, so set it when costs will drop below today's lowest. Default the lowest cost on the grid.
|
|
11
|
+
*/
|
|
12
|
+
minCost?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Cheapest paths to one goal over a grid that changes, for a unit that moves along its path and learns of new walls on
|
|
16
|
+
* the way. Change `grid.cost`, tell {@link PathReplanner.update} which cells changed, and ask for the path again: the
|
|
17
|
+
* cost is the one a fresh A* search would find, usually for a fraction of the work.
|
|
18
|
+
*/
|
|
19
|
+
export declare class PathReplanner {
|
|
20
|
+
/** The grid searched; change its costs in place, then call {@link update}. */
|
|
21
|
+
readonly grid: CostGrid;
|
|
22
|
+
readonly goal: readonly [number, number];
|
|
23
|
+
private readonly diagonal;
|
|
24
|
+
private readonly square;
|
|
25
|
+
private readonly max;
|
|
26
|
+
private readonly to;
|
|
27
|
+
private low;
|
|
28
|
+
private estimate;
|
|
29
|
+
/** The start of the last search, -1 before the first. */
|
|
30
|
+
private last;
|
|
31
|
+
private km;
|
|
32
|
+
private readonly g;
|
|
33
|
+
private readonly rhs;
|
|
34
|
+
private readonly heap;
|
|
35
|
+
private readonly slot;
|
|
36
|
+
private readonly key1;
|
|
37
|
+
private readonly key2;
|
|
38
|
+
constructor(grid: CostGrid, goal: readonly [number, number], options?: ReplanOptions);
|
|
39
|
+
/**
|
|
40
|
+
* Tells the replanner which cells' costs changed since the last call. Every changed cell must be listed, or later
|
|
41
|
+
* paths may use the old costs.
|
|
42
|
+
*/
|
|
43
|
+
update(cells: Iterable<readonly [number, number]>): this;
|
|
44
|
+
/**
|
|
45
|
+
* A cheapest path from `start` to the goal under the costs as last told, or null when there is no way (or `maxCells`
|
|
46
|
+
* ran out; calling again continues from there). `searched` counts the cells this call expanded.
|
|
47
|
+
*/
|
|
48
|
+
findPath(start: readonly [number, number]): GridPath | null;
|
|
49
|
+
/** Sets a cell's look-ahead from its steps: 0 at an open goal, Infinity on a blocked cell. */
|
|
50
|
+
private look;
|
|
51
|
+
/** Expands cells until the start's distance is settled; the cells expanded, or -1 when `maxCells` ran out. */
|
|
52
|
+
private search;
|
|
53
|
+
/** Queues `u` under today's key when its distance and look-ahead disagree, and takes it out when they agree. */
|
|
54
|
+
private place;
|
|
55
|
+
private remove;
|
|
56
|
+
/** Moves the cell in place `i` up or down to where its key belongs. */
|
|
57
|
+
private sift;
|
|
58
|
+
}
|
package/dist/replan.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { a as e, i as t, o as n, r, s as i } from "./grid-search-B0VbYKpe.js";
|
|
2
|
+
import { MargoError as a } from "@margoengine/math/errors";
|
|
3
|
+
//#region src/replan.ts
|
|
4
|
+
var o = class {
|
|
5
|
+
constructor(i, a, o = {}) {
|
|
6
|
+
this.last = -1, this.km = 0, this.heap = [];
|
|
7
|
+
let { diagonal: s, max: c, square: l } = r(i, o);
|
|
8
|
+
this.grid = i, this.diagonal = s, this.square = l, this.max = c, this.to = e(i, a, "goal"), this.goal = [a[0], a[1]];
|
|
9
|
+
let u = o.minCost;
|
|
10
|
+
if (u === void 0) {
|
|
11
|
+
u = Infinity;
|
|
12
|
+
for (let e = 0; e < i.cost.length; e++) n(i.cost[e]) && i.cost[e] < u && (u = i.cost[e]);
|
|
13
|
+
u === Infinity && (u = 1);
|
|
14
|
+
} else if (!n(u)) throw RangeError(`minCost must be a finite number above 0, got ${u}.`);
|
|
15
|
+
this.low = u, this.estimate = t(i, l, s, u);
|
|
16
|
+
let d = i.cost.length;
|
|
17
|
+
this.g = new Float64Array(d), this.rhs = new Float64Array(d), this.slot = new Int32Array(d), this.key1 = new Float64Array(d), this.key2 = new Float64Array(d);
|
|
18
|
+
}
|
|
19
|
+
update(r) {
|
|
20
|
+
let { grid: i } = this, a = /* @__PURE__ */ new Set(), o = this.low;
|
|
21
|
+
for (let t of r) {
|
|
22
|
+
let r = e(i, t, "cell"), [s, c] = t;
|
|
23
|
+
n(i.cost[r]) && i.cost[r] < o && (o = i.cost[r]), a.add(r);
|
|
24
|
+
for (let e = -1; e <= 1; e++) for (let t = -1; t <= 1; t++) s + t >= 0 && c + e >= 0 && s + t < i.width && c + e < i.height && a.add(r + e * i.width + t);
|
|
25
|
+
}
|
|
26
|
+
if (o < this.low) this.low = o, this.estimate = t(i, this.square, this.diagonal, o), this.last = -1;
|
|
27
|
+
else if (this.last >= 0) for (let e of a) this.look(e), this.place(e);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
findPath(t) {
|
|
31
|
+
let { grid: r, g: o, rhs: s, to: c } = this, { cost: l, width: u } = r, d = e(r, t, "start");
|
|
32
|
+
if (this.last < 0 ? (o.fill(Infinity), s.fill(Infinity), this.slot.fill(-1), this.heap.length = 0, this.km = 0, this.last = d, this.look(c), this.place(c)) : d !== this.last && (this.km += this.estimate(this.last, d), this.last = d), !n(l[d]) || !n(l[c])) return null;
|
|
33
|
+
let f = this.search(d);
|
|
34
|
+
if (f < 0 || s[d] === Infinity) return null;
|
|
35
|
+
let p = [[t[0], t[1]]], m = 0;
|
|
36
|
+
for (let e = d; e !== c;) {
|
|
37
|
+
let t = Infinity, n = -1, s = 0;
|
|
38
|
+
if (i(r, this.square, this.diagonal, e, (e, r) => {
|
|
39
|
+
let i = l[e] * r + o[e];
|
|
40
|
+
i < t && (t = i, n = e, s = l[e] * r);
|
|
41
|
+
}), n < 0 || p.length > l.length) throw new a("state", "The grid changed without update(); pass every changed cell to update() before findPath().");
|
|
42
|
+
e = n, m += s, p.push([e % u, Math.floor(e / u)]);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
cells: p,
|
|
46
|
+
cost: m,
|
|
47
|
+
searched: f,
|
|
48
|
+
scanned: 0
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
look(e) {
|
|
52
|
+
let { grid: t, g: r } = this, { cost: a } = t, o = Infinity;
|
|
53
|
+
n(a[e]) && (e === this.to ? o = 0 : i(t, this.square, this.diagonal, e, (e, t) => {
|
|
54
|
+
let n = a[e] * t + r[e];
|
|
55
|
+
n < o && (o = n);
|
|
56
|
+
})), this.rhs[e] = o;
|
|
57
|
+
}
|
|
58
|
+
search(e) {
|
|
59
|
+
let { grid: t, g: r, rhs: a, heap: o, key1: s, key2: c, to: l } = this, { cost: u } = t, d = 0;
|
|
60
|
+
for (; o.length;) {
|
|
61
|
+
let f = o[0], p = Math.min(r[e], a[e]) + this.km;
|
|
62
|
+
if (!(s[f] <= p + p * 1e-9 || a[e] > r[e])) break;
|
|
63
|
+
let m = Math.min(r[f], a[f]), h = m + this.estimate(this.last, f) + this.km;
|
|
64
|
+
if (s[f] < h || s[f] === h && c[f] < m) this.place(f);
|
|
65
|
+
else {
|
|
66
|
+
if (++d > this.max) return -1;
|
|
67
|
+
if (r[f] > a[f]) {
|
|
68
|
+
if (r[f] = a[f], this.remove(f), !n(u[f])) continue;
|
|
69
|
+
i(t, this.square, this.diagonal, f, (e, t) => {
|
|
70
|
+
e !== l && (a[e] = Math.min(a[e], u[f] * t + r[f])), this.place(e);
|
|
71
|
+
});
|
|
72
|
+
} else {
|
|
73
|
+
let e = r[f];
|
|
74
|
+
r[f] = Infinity, i(t, this.square, this.diagonal, f, (t, n) => {
|
|
75
|
+
t !== l && a[t] === u[f] * n + e && this.look(t), this.place(t);
|
|
76
|
+
}), this.place(f);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return d;
|
|
81
|
+
}
|
|
82
|
+
place(e) {
|
|
83
|
+
let { g: t, rhs: n, slot: r, key1: i, key2: a, heap: o } = this;
|
|
84
|
+
if (t[e] === n[e]) {
|
|
85
|
+
this.remove(e);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
let s = Math.min(t[e], n[e]);
|
|
89
|
+
i[e] = s + this.estimate(this.last, e) + this.km, a[e] = s, r[e] < 0 && (r[e] = o.length, o.push(e)), this.sift(r[e]);
|
|
90
|
+
}
|
|
91
|
+
remove(e) {
|
|
92
|
+
let { slot: t, heap: n } = this, r = t[e];
|
|
93
|
+
if (r < 0) return;
|
|
94
|
+
t[e] = -1;
|
|
95
|
+
let i = n.pop();
|
|
96
|
+
i !== e && (n[r] = i, t[i] = r, this.sift(r));
|
|
97
|
+
}
|
|
98
|
+
sift(e) {
|
|
99
|
+
let { heap: t, slot: n, key1: r, key2: i } = this, a = t[e], o = (e, t) => r[e] < r[t] || r[e] === r[t] && i[e] < i[t];
|
|
100
|
+
for (; e > 0;) {
|
|
101
|
+
let r = e - 1 >> 1;
|
|
102
|
+
if (!o(a, t[r])) break;
|
|
103
|
+
t[e] = t[r], n[t[e]] = e, e = r;
|
|
104
|
+
}
|
|
105
|
+
for (;;) {
|
|
106
|
+
let r = e * 2 + 1;
|
|
107
|
+
if (r >= t.length || (r + 1 < t.length && o(t[r + 1], t[r]) && r++, !o(t[r], a))) break;
|
|
108
|
+
t[e] = t[r], n[t[e]] = e, e = r;
|
|
109
|
+
}
|
|
110
|
+
t[e] = a, n[a] = e;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
//#endregion
|
|
114
|
+
export { o as PathReplanner };
|