sci2-ts 0.0.0-stage → 0.2.2
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/CHANGELOG.md +78 -0
- package/LICENSE +21 -0
- package/README.md +113 -2
- package/apps/viewer/editor.html +43 -0
- package/apps/viewer/explore.html +27 -0
- package/apps/viewer/index.html +26 -0
- package/apps/viewer/package.json +27 -0
- package/apps/viewer/play.html +26 -0
- package/apps/viewer/plugin.d.ts +18 -0
- package/apps/viewer/plugin.js +75 -0
- package/apps/viewer/plugin.ts +84 -0
- package/apps/viewer/src/adlib.d.ts +20 -0
- package/apps/viewer/src/adlib.js +55 -0
- package/apps/viewer/src/adlib.ts +62 -0
- package/apps/viewer/src/app.d.ts +5 -0
- package/apps/viewer/src/app.js +5 -0
- package/apps/viewer/src/app.ts +5 -0
- package/apps/viewer/src/asset-index.d.ts +22 -0
- package/apps/viewer/src/asset-index.js +74 -0
- package/apps/viewer/src/asset-index.ts +90 -0
- package/apps/viewer/src/audio.d.ts +28 -0
- package/apps/viewer/src/audio.js +102 -0
- package/apps/viewer/src/audio.ts +110 -0
- package/apps/viewer/src/editor-handles.d.ts +63 -0
- package/apps/viewer/src/editor-handles.js +332 -0
- package/apps/viewer/src/editor-handles.ts +368 -0
- package/apps/viewer/src/editor-languages.d.ts +23 -0
- package/apps/viewer/src/editor-languages.js +157 -0
- package/apps/viewer/src/editor-languages.ts +135 -0
- package/apps/viewer/src/editor.css +213 -0
- package/apps/viewer/src/editor.d.ts +1 -0
- package/apps/viewer/src/editor.js +393 -0
- package/apps/viewer/src/editor.ts +412 -0
- package/apps/viewer/src/explore/classes.d.ts +7 -0
- package/apps/viewer/src/explore/classes.js +63 -0
- package/apps/viewer/src/explore/classes.ts +61 -0
- package/apps/viewer/src/explore/context.d.ts +33 -0
- package/apps/viewer/src/explore/context.js +96 -0
- package/apps/viewer/src/explore/context.ts +124 -0
- package/apps/viewer/src/explore/explore.css +93 -0
- package/apps/viewer/src/explore/explore.d.ts +1 -0
- package/apps/viewer/src/explore/explore.js +102 -0
- package/apps/viewer/src/explore/explore.ts +104 -0
- package/apps/viewer/src/explore/inspect.d.ts +5 -0
- package/apps/viewer/src/explore/inspect.js +208 -0
- package/apps/viewer/src/explore/inspect.ts +279 -0
- package/apps/viewer/src/explore/live.d.ts +22 -0
- package/apps/viewer/src/explore/live.js +318 -0
- package/apps/viewer/src/explore/live.ts +365 -0
- package/apps/viewer/src/explore/map.d.ts +11 -0
- package/apps/viewer/src/explore/map.js +182 -0
- package/apps/viewer/src/explore/map.ts +193 -0
- package/apps/viewer/src/files.d.ts +21 -0
- package/apps/viewer/src/files.js +69 -0
- package/apps/viewer/src/files.ts +72 -0
- package/apps/viewer/src/game-profile.d.ts +19 -0
- package/apps/viewer/src/game-profile.js +4 -0
- package/apps/viewer/src/game-profile.ts +26 -0
- package/apps/viewer/src/main.d.ts +1 -0
- package/apps/viewer/src/main.js +473 -0
- package/apps/viewer/src/main.ts +527 -0
- package/apps/viewer/src/music.d.ts +14 -0
- package/apps/viewer/src/music.js +32 -0
- package/apps/viewer/src/music.ts +34 -0
- package/apps/viewer/src/opl-worklet.d.ts +1 -0
- package/apps/viewer/src/opl-worklet.js +47 -0
- package/apps/viewer/src/opl-worklet.ts +55 -0
- package/apps/viewer/src/overlay.d.ts +31 -0
- package/apps/viewer/src/overlay.js +103 -0
- package/apps/viewer/src/overlay.ts +123 -0
- package/apps/viewer/src/play.css +14 -0
- package/apps/viewer/src/play.d.ts +1 -0
- package/apps/viewer/src/play.js +179 -0
- package/apps/viewer/src/play.ts +177 -0
- package/apps/viewer/src/saves.d.ts +10 -0
- package/apps/viewer/src/saves.js +50 -0
- package/apps/viewer/src/saves.ts +51 -0
- package/apps/viewer/src/session.d.ts +91 -0
- package/apps/viewer/src/session.js +301 -0
- package/apps/viewer/src/session.ts +321 -0
- package/apps/viewer/src/sound-preview.d.ts +7 -0
- package/apps/viewer/src/sound-preview.js +214 -0
- package/apps/viewer/src/sound-preview.ts +228 -0
- package/apps/viewer/src/style.css +64 -0
- package/apps/viewer/vite.config.ts +25 -0
- package/bin/sci-ts.mjs +9 -0
- package/lib/994.sc +49 -0
- package/lib/995.sc +53 -0
- package/lib/996.sc +299 -0
- package/lib/997.sc +198 -0
- package/lib/998.sc +338 -0
- package/lib/999.sc +189 -0
- package/lib/resources.d.ts +2 -0
- package/lib/resources.js +84 -0
- package/lib/resources.ts +88 -0
- package/lib/system.sh +62 -0
- package/package.json +75 -4
- package/packages/content/room.schema.json +267 -0
- package/packages/content/src/index.d.ts +3 -0
- package/packages/content/src/index.js +3 -0
- package/packages/content/src/index.ts +3 -0
- package/packages/content/src/room.d.ts +111 -0
- package/packages/content/src/room.js +886 -0
- package/packages/content/src/room.ts +1011 -0
- package/packages/content/src/target.d.ts +80 -0
- package/packages/content/src/target.js +1 -0
- package/packages/content/src/target.ts +60 -0
- package/packages/content/src/yarn.d.ts +90 -0
- package/packages/content/src/yarn.js +214 -0
- package/packages/content/src/yarn.ts +261 -0
- package/packages/sci/src/analysis/game.d.ts +90 -0
- package/packages/sci/src/analysis/game.js +221 -0
- package/packages/sci/src/analysis/game.ts +305 -0
- package/packages/sci/src/analysis/runtime.d.ts +58 -0
- package/packages/sci/src/analysis/runtime.js +133 -0
- package/packages/sci/src/analysis/runtime.ts +163 -0
- package/packages/sci/src/audio/adlib.d.ts +73 -0
- package/packages/sci/src/audio/adlib.js +280 -0
- package/packages/sci/src/audio/adlib.ts +329 -0
- package/packages/sci/src/audio/index.d.ts +43 -0
- package/packages/sci/src/audio/index.js +99 -0
- package/packages/sci/src/audio/index.ts +122 -0
- package/packages/sci/src/audio/midi.d.ts +92 -0
- package/packages/sci/src/audio/midi.js +242 -0
- package/packages/sci/src/audio/midi.ts +262 -0
- package/packages/sci/src/audio/opl2.d.ts +74 -0
- package/packages/sci/src/audio/opl2.js +283 -0
- package/packages/sci/src/audio/opl2.ts +283 -0
- package/packages/sci/src/audio/sol.d.ts +32 -0
- package/packages/sci/src/audio/sol.js +81 -0
- package/packages/sci/src/audio/sol.ts +100 -0
- package/packages/sci/src/audio/write.d.ts +35 -0
- package/packages/sci/src/audio/write.js +91 -0
- package/packages/sci/src/audio/write.ts +108 -0
- package/packages/sci/src/gfx/cel.d.ts +36 -0
- package/packages/sci/src/gfx/cel.js +82 -0
- package/packages/sci/src/gfx/cel.ts +110 -0
- package/packages/sci/src/gfx/palette.d.ts +24 -0
- package/packages/sci/src/gfx/palette.js +49 -0
- package/packages/sci/src/gfx/palette.ts +56 -0
- package/packages/sci/src/gfx/pic-file.d.ts +46 -0
- package/packages/sci/src/gfx/pic-file.js +114 -0
- package/packages/sci/src/gfx/pic-file.ts +155 -0
- package/packages/sci/src/gfx/pic.d.ts +27 -0
- package/packages/sci/src/gfx/pic.js +34 -0
- package/packages/sci/src/gfx/pic.ts +53 -0
- package/packages/sci/src/gfx/quantize.d.ts +42 -0
- package/packages/sci/src/gfx/quantize.js +199 -0
- package/packages/sci/src/gfx/quantize.ts +218 -0
- package/packages/sci/src/gfx/view-file.d.ts +62 -0
- package/packages/sci/src/gfx/view-file.js +182 -0
- package/packages/sci/src/gfx/view-file.ts +233 -0
- package/packages/sci/src/gfx/view.d.ts +28 -0
- package/packages/sci/src/gfx/view.js +45 -0
- package/packages/sci/src/gfx/view.ts +62 -0
- package/packages/sci/src/index.d.ts +38 -0
- package/packages/sci/src/index.js +38 -0
- package/packages/sci/src/index.ts +38 -0
- package/packages/sci/src/motion/pathfinding.d.ts +40 -0
- package/packages/sci/src/motion/pathfinding.js +140 -0
- package/packages/sci/src/motion/pathfinding.ts +147 -0
- package/packages/sci/src/resource/archive.d.ts +16 -0
- package/packages/sci/src/resource/archive.js +51 -0
- package/packages/sci/src/resource/archive.ts +56 -0
- package/packages/sci/src/resource/lzs.d.ts +12 -0
- package/packages/sci/src/resource/lzs.js +64 -0
- package/packages/sci/src/resource/lzs.ts +66 -0
- package/packages/sci/src/resource/manager.d.ts +45 -0
- package/packages/sci/src/resource/manager.js +158 -0
- package/packages/sci/src/resource/manager.ts +184 -0
- package/packages/sci/src/resource/map.d.ts +18 -0
- package/packages/sci/src/resource/map.js +34 -0
- package/packages/sci/src/resource/map.ts +44 -0
- package/packages/sci/src/resource/types.d.ts +37 -0
- package/packages/sci/src/resource/types.js +45 -0
- package/packages/sci/src/resource/types.ts +57 -0
- package/packages/sci/src/script/assembly.d.ts +67 -0
- package/packages/sci/src/script/assembly.js +748 -0
- package/packages/sci/src/script/assembly.ts +714 -0
- package/packages/sci/src/script/compiler.d.ts +75 -0
- package/packages/sci/src/script/compiler.js +993 -0
- package/packages/sci/src/script/compiler.ts +1040 -0
- package/packages/sci/src/script/disasm.d.ts +81 -0
- package/packages/sci/src/script/disasm.js +318 -0
- package/packages/sci/src/script/disasm.ts +323 -0
- package/packages/sci/src/script/kernel-names.d.ts +6 -0
- package/packages/sci/src/script/kernel-names.js +165 -0
- package/packages/sci/src/script/kernel-names.ts +165 -0
- package/packages/sci/src/script/opcodes.d.ts +22 -0
- package/packages/sci/src/script/opcodes.js +45 -0
- package/packages/sci/src/script/opcodes.ts +72 -0
- package/packages/sci/src/script/script.d.ts +56 -0
- package/packages/sci/src/script/script.js +63 -0
- package/packages/sci/src/script/script.ts +111 -0
- package/packages/sci/src/script/sexpr.d.ts +45 -0
- package/packages/sci/src/script/sexpr.js +151 -0
- package/packages/sci/src/script/sexpr.ts +139 -0
- package/packages/sci/src/script/vocab.d.ts +15 -0
- package/packages/sci/src/script/vocab.js +44 -0
- package/packages/sci/src/script/vocab.ts +48 -0
- package/packages/sci/src/script/world.d.ts +25 -0
- package/packages/sci/src/script/world.js +65 -0
- package/packages/sci/src/script/world.ts +70 -0
- package/packages/sci/src/text/font.d.ts +37 -0
- package/packages/sci/src/text/font.js +103 -0
- package/packages/sci/src/text/font.ts +125 -0
- package/packages/sci/src/text/message.d.ts +69 -0
- package/packages/sci/src/text/message.js +171 -0
- package/packages/sci/src/text/message.ts +205 -0
- package/packages/sci/src/vm/kernels/arrays.d.ts +19 -0
- package/packages/sci/src/vm/kernels/arrays.js +182 -0
- package/packages/sci/src/vm/kernels/arrays.ts +164 -0
- package/packages/sci/src/vm/kernels/audio.d.ts +70 -0
- package/packages/sci/src/vm/kernels/audio.js +347 -0
- package/packages/sci/src/vm/kernels/audio.ts +349 -0
- package/packages/sci/src/vm/kernels/core.d.ts +5 -0
- package/packages/sci/src/vm/kernels/core.js +87 -0
- package/packages/sci/src/vm/kernels/core.ts +91 -0
- package/packages/sci/src/vm/kernels/graphics.d.ts +141 -0
- package/packages/sci/src/vm/kernels/graphics.js +467 -0
- package/packages/sci/src/vm/kernels/graphics.ts +502 -0
- package/packages/sci/src/vm/kernels/index.d.ts +8 -0
- package/packages/sci/src/vm/kernels/index.js +29 -0
- package/packages/sci/src/vm/kernels/index.ts +32 -0
- package/packages/sci/src/vm/kernels/lists.d.ts +6 -0
- package/packages/sci/src/vm/kernels/lists.js +127 -0
- package/packages/sci/src/vm/kernels/lists.ts +111 -0
- package/packages/sci/src/vm/kernels/motion.d.ts +7 -0
- package/packages/sci/src/vm/kernels/motion.js +204 -0
- package/packages/sci/src/vm/kernels/motion.ts +194 -0
- package/packages/sci/src/vm/kernels/palette.d.ts +82 -0
- package/packages/sci/src/vm/kernels/palette.js +292 -0
- package/packages/sci/src/vm/kernels/palette.ts +303 -0
- package/packages/sci/src/vm/kernels/saves.d.ts +45 -0
- package/packages/sci/src/vm/kernels/saves.js +123 -0
- package/packages/sci/src/vm/kernels/saves.ts +149 -0
- package/packages/sci/src/vm/kernels/system.d.ts +62 -0
- package/packages/sci/src/vm/kernels/system.js +110 -0
- package/packages/sci/src/vm/kernels/system.ts +128 -0
- package/packages/sci/src/vm/kernels/text.d.ts +13 -0
- package/packages/sci/src/vm/kernels/text.js +338 -0
- package/packages/sci/src/vm/kernels/text.ts +328 -0
- package/packages/sci/src/vm/memory.d.ts +138 -0
- package/packages/sci/src/vm/memory.js +168 -0
- package/packages/sci/src/vm/memory.ts +266 -0
- package/packages/sci/src/vm/replace.d.ts +11 -0
- package/packages/sci/src/vm/replace.js +14 -0
- package/packages/sci/src/vm/replace.ts +16 -0
- package/packages/sci/src/vm/savegame.d.ts +89 -0
- package/packages/sci/src/vm/savegame.js +161 -0
- package/packages/sci/src/vm/savegame.ts +188 -0
- package/packages/sci/src/vm/value.d.ts +17 -0
- package/packages/sci/src/vm/value.js +12 -0
- package/packages/sci/src/vm/value.ts +25 -0
- package/packages/sci/src/vm/vm.d.ts +155 -0
- package/packages/sci/src/vm/vm.js +1053 -0
- package/packages/sci/src/vm/vm.ts +914 -0
- package/tools/adlib-render.d.ts +1 -0
- package/tools/adlib-render.js +49 -0
- package/tools/adlib-render.ts +42 -0
- package/tools/analyze.d.ts +1 -0
- package/tools/analyze.js +25 -0
- package/tools/analyze.ts +25 -0
- package/tools/art/build.d.ts +34 -0
- package/tools/art/build.js +175 -0
- package/tools/art/build.ts +153 -0
- package/tools/art.d.ts +1 -0
- package/tools/art.js +31 -0
- package/tools/art.ts +29 -0
- package/tools/asm.d.ts +1 -0
- package/tools/asm.js +61 -0
- package/tools/asm.ts +55 -0
- package/tools/cli.d.ts +1 -0
- package/tools/cli.js +99 -0
- package/tools/cli.ts +100 -0
- package/tools/editor-server.d.ts +29 -0
- package/tools/editor-server.js +162 -0
- package/tools/editor-server.ts +176 -0
- package/tools/explore.d.ts +1 -0
- package/tools/explore.js +305 -0
- package/tools/explore.ts +250 -0
- package/tools/game/audio.d.ts +19 -0
- package/tools/game/audio.js +143 -0
- package/tools/game/audio.ts +130 -0
- package/tools/game/build.d.ts +46 -0
- package/tools/game/build.js +259 -0
- package/tools/game/build.ts +283 -0
- package/tools/game/defaults.d.ts +26 -0
- package/tools/game/defaults.js +82 -0
- package/tools/game/defaults.ts +83 -0
- package/tools/game/font.d.ts +3 -0
- package/tools/game/font.js +124 -0
- package/tools/game/font.ts +126 -0
- package/tools/game/kit.d.ts +7 -0
- package/tools/game/kit.js +7 -0
- package/tools/game/kit.ts +10 -0
- package/tools/game/new.d.ts +8 -0
- package/tools/game/new.js +39 -0
- package/tools/game/new.ts +38 -0
- package/tools/game/target.d.ts +7 -0
- package/tools/game/target.js +41 -0
- package/tools/game/target.ts +42 -0
- package/tools/game/template/resources.ts.tmpl +43 -0
- package/tools/game/template/rooms/1.room.yaml +9 -0
- package/tools/game/template/rooms/1.yarn +17 -0
- package/tools/game/template/scripts/0.sc +24 -0
- package/tools/game.d.ts +1 -0
- package/tools/game.js +77 -0
- package/tools/game.ts +70 -0
- package/tools/mod-build.d.ts +28 -0
- package/tools/mod-build.js +173 -0
- package/tools/mod-build.ts +179 -0
- package/tools/mod-target.d.ts +16 -0
- package/tools/mod-target.js +26 -0
- package/tools/mod-target.ts +32 -0
- package/tools/mod.d.ts +1 -0
- package/tools/mod.js +47 -0
- package/tools/mod.ts +45 -0
- package/tools/msg.d.ts +1 -0
- package/tools/msg.js +40 -0
- package/tools/msg.ts +36 -0
- package/tools/node-files.d.ts +3 -0
- package/tools/node-files.js +34 -0
- package/tools/node-files.ts +33 -0
- package/tools/patches.d.ts +3 -0
- package/tools/patches.js +13 -0
- package/tools/patches.ts +13 -0
- package/tools/pic-build.d.ts +9 -0
- package/tools/pic-build.js +26 -0
- package/tools/pic-build.ts +27 -0
- package/tools/pic.d.ts +1 -0
- package/tools/pic.js +64 -0
- package/tools/pic.ts +61 -0
- package/tools/play.d.ts +1 -0
- package/tools/play.js +54 -0
- package/tools/play.ts +54 -0
- package/tools/png.d.ts +16 -0
- package/tools/png.js +124 -0
- package/tools/png.ts +123 -0
- package/tools/res.d.ts +1 -0
- package/tools/res.js +48 -0
- package/tools/res.ts +50 -0
- package/tools/scenario.d.ts +1 -0
- package/tools/scenario.js +74 -0
- package/tools/scenario.ts +80 -0
- package/tools/site/build.d.ts +8 -0
- package/tools/site/build.js +110 -0
- package/tools/site/build.ts +116 -0
- package/tools/site/markdown.d.ts +11 -0
- package/tools/site/markdown.js +88 -0
- package/tools/site/markdown.ts +84 -0
- package/tools/view-build.d.ts +34 -0
- package/tools/view-build.js +47 -0
- package/tools/view-build.ts +71 -0
- package/tools/view.d.ts +1 -0
- package/tools/view.js +54 -0
- package/tools/view.ts +52 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { isSeq, parseDocument } from "yaml";
|
|
2
|
+
// --- Editing YAML text in place ----------------------------------------------------------
|
|
3
|
+
const flow = (v) => (Array.isArray(v) ? `[${v.map(flow).join(", ")}]` : String(v));
|
|
4
|
+
/** `text` with the value at `path` replaced by `value`, keeping the rest as written. */
|
|
5
|
+
export function setYamlValue(text, path, value) {
|
|
6
|
+
const doc = parseDocument(text);
|
|
7
|
+
const node = doc.getIn(path, true);
|
|
8
|
+
if (node?.range) {
|
|
9
|
+
const start = node.range[0];
|
|
10
|
+
let end = node.range[1];
|
|
11
|
+
while (end > start && text[end - 1] === "\n")
|
|
12
|
+
end--;
|
|
13
|
+
let replacement;
|
|
14
|
+
if (isSeq(node) && !node.flow && Array.isArray(value[0])) {
|
|
15
|
+
// A block list of points stays one "- [x, y]" per line, at its indentation.
|
|
16
|
+
const indent = " ".repeat(start - (text.lastIndexOf("\n", start - 1) + 1));
|
|
17
|
+
replacement = value.map((p) => `- ${flow(p)}`).join(`\n${indent}`);
|
|
18
|
+
}
|
|
19
|
+
else
|
|
20
|
+
replacement = flow(value);
|
|
21
|
+
return text.slice(0, start) + replacement + text.slice(end);
|
|
22
|
+
}
|
|
23
|
+
// Not there yet: let the YAML library add it (it may tidy the layout).
|
|
24
|
+
doc.setIn(path, flowNode(doc, value));
|
|
25
|
+
return doc.toString();
|
|
26
|
+
}
|
|
27
|
+
/** `text` with `value` added at the end of the list at `path` (made if missing). */
|
|
28
|
+
export function appendYamlItem(text, path, value) {
|
|
29
|
+
const doc = parseDocument(text);
|
|
30
|
+
const node = doc.getIn(path, true);
|
|
31
|
+
if (node?.range && isSeq(node) && !node.flow) {
|
|
32
|
+
const start = node.range[0];
|
|
33
|
+
let end = node.range[1];
|
|
34
|
+
while (end > start && text[end - 1] === "\n")
|
|
35
|
+
end--;
|
|
36
|
+
const indent = " ".repeat(start - (text.lastIndexOf("\n", start - 1) + 1));
|
|
37
|
+
return `${text.slice(0, end)}\n${indent}- ${flow(value)}${text.slice(end)}`;
|
|
38
|
+
}
|
|
39
|
+
if (!node)
|
|
40
|
+
return `${text.replace(/\n*$/, "\n")}\n${path.join(".")}:\n - ${flow(value)}\n`;
|
|
41
|
+
doc.addIn(path, flowNode(doc, value));
|
|
42
|
+
return doc.toString();
|
|
43
|
+
}
|
|
44
|
+
function flowNode(doc, value) {
|
|
45
|
+
const node = doc.createNode(value);
|
|
46
|
+
if (isSeq(node)) {
|
|
47
|
+
node.flow = true;
|
|
48
|
+
for (const item of node.items)
|
|
49
|
+
if (isSeq(item))
|
|
50
|
+
item.flow = true;
|
|
51
|
+
}
|
|
52
|
+
return node;
|
|
53
|
+
}
|
|
54
|
+
const NS = "http://www.w3.org/2000/svg";
|
|
55
|
+
const el = (tag, attrs = {}, text) => {
|
|
56
|
+
const node = document.createElementNS(NS, tag);
|
|
57
|
+
for (const [k, v] of Object.entries(attrs))
|
|
58
|
+
node.setAttribute(k, String(v));
|
|
59
|
+
if (text)
|
|
60
|
+
node.textContent = text;
|
|
61
|
+
return node;
|
|
62
|
+
};
|
|
63
|
+
const COLORS = { feature: "#e8b04a", exit: "#5fd08c", prop: "#7fb3e8", hero: "#f08fd0", floor: "#5fa0e8", obstacle: "#e05555" };
|
|
64
|
+
const clampX = (x) => Math.max(0, Math.min(319, Math.round(x)));
|
|
65
|
+
const clampY = (y) => Math.max(0, Math.min(189, Math.round(y)));
|
|
66
|
+
/**
|
|
67
|
+
* An SVG layer over the game. In "move" mode, everything the YAML places has a handle;
|
|
68
|
+
* "floor" and "obstacle" are brushes that draw a polygon. In "play" it's out of the way.
|
|
69
|
+
*/
|
|
70
|
+
export class EditLayer {
|
|
71
|
+
host;
|
|
72
|
+
svg;
|
|
73
|
+
mode = "play";
|
|
74
|
+
spec = {};
|
|
75
|
+
specText = "";
|
|
76
|
+
drag;
|
|
77
|
+
stroke;
|
|
78
|
+
lastWrite = 0;
|
|
79
|
+
constructor(host) {
|
|
80
|
+
this.host = host;
|
|
81
|
+
this.svg = el("svg", { viewBox: "0 0 320 200", preserveAspectRatio: "none" });
|
|
82
|
+
this.svg.style.touchAction = "none";
|
|
83
|
+
this.svg.addEventListener("pointerdown", (e) => this.down(e));
|
|
84
|
+
this.svg.addEventListener("pointermove", (e) => this.move(e));
|
|
85
|
+
this.svg.addEventListener("pointerup", (e) => this.up(e));
|
|
86
|
+
this.svg.addEventListener("pointercancel", (e) => this.up(e));
|
|
87
|
+
this.setMode("play");
|
|
88
|
+
}
|
|
89
|
+
setMode(mode) {
|
|
90
|
+
this.mode = mode;
|
|
91
|
+
this.svg.style.pointerEvents = mode === "play" ? "none" : "auto";
|
|
92
|
+
this.svg.style.cursor = mode === "move" ? "default" : mode === "play" ? "" : "crosshair";
|
|
93
|
+
this.render();
|
|
94
|
+
}
|
|
95
|
+
/** Room coordinates under the pointer. */
|
|
96
|
+
point(e) {
|
|
97
|
+
const m = this.svg.getScreenCTM();
|
|
98
|
+
if (!m)
|
|
99
|
+
return [0, 0];
|
|
100
|
+
const p = new DOMPoint(e.clientX, e.clientY).matrixTransform(m.inverse());
|
|
101
|
+
const [ox, oy] = this.host.roomOffset();
|
|
102
|
+
return [p.x - ox, p.y - oy];
|
|
103
|
+
}
|
|
104
|
+
readSpec() {
|
|
105
|
+
const text = this.host.yaml();
|
|
106
|
+
if (text !== this.specText) {
|
|
107
|
+
this.specText = text;
|
|
108
|
+
try {
|
|
109
|
+
this.spec = parseDocument(text).toJS() ?? {};
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
/* mid-edit: keep the last good one */
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return this.spec;
|
|
116
|
+
}
|
|
117
|
+
// --- Drawing ---
|
|
118
|
+
render() {
|
|
119
|
+
this.svg.replaceChildren();
|
|
120
|
+
if (this.mode === "play")
|
|
121
|
+
return;
|
|
122
|
+
const spec = this.readSpec();
|
|
123
|
+
const [ox, oy] = this.host.roomOffset();
|
|
124
|
+
const g = el("g", { transform: `translate(${ox} ${oy})` });
|
|
125
|
+
this.svg.append(g);
|
|
126
|
+
const handle = (x, y, color, start, shape = "square") => {
|
|
127
|
+
const attrs = { fill: color, stroke: "#000", "stroke-width": 0.4, cursor: "grab" };
|
|
128
|
+
const h = shape === "circle"
|
|
129
|
+
? el("circle", { cx: x, cy: y, r: 2.4, ...attrs })
|
|
130
|
+
: shape === "diamond"
|
|
131
|
+
? el("path", { d: `M${x} ${y - 2.8}L${x + 2.8} ${y}L${x} ${y + 2.8}L${x - 2.8} ${y}Z`, ...attrs })
|
|
132
|
+
: el("rect", { x: x - 2, y: y - 2, width: 4, height: 4, ...attrs });
|
|
133
|
+
h.addEventListener("pointerdown", (e) => this.begin(e, start));
|
|
134
|
+
g.append(h);
|
|
135
|
+
return h;
|
|
136
|
+
};
|
|
137
|
+
const label = (x, y, text, color) => g.append(el("text", { x, y, fill: color, stroke: "#000", "stroke-width": 1.2, "paint-order": "stroke", "font-size": 5, "font-family": "ui-monospace, Menlo, monospace", "pointer-events": "none" }, text));
|
|
138
|
+
if (this.mode === "move") {
|
|
139
|
+
// Walk areas: every vertex can be dragged; Alt-click removes one.
|
|
140
|
+
const polygon = (points, path, color) => {
|
|
141
|
+
g.append(el("polygon", { points: points.map((p) => p.join(",")).join(" "), fill: `${color}22`, stroke: color, "stroke-width": 0.6, "pointer-events": "none" }));
|
|
142
|
+
points.forEach((p, i) => {
|
|
143
|
+
const h = handle(p[0], p[1], color, () => ({
|
|
144
|
+
start: p,
|
|
145
|
+
apply: (dx, dy) => setYamlValue(this.host.yaml(), path, points.map((q, j) => (j === i ? [clampX(p[0] + dx), clampY(p[1] + dy)] : q))),
|
|
146
|
+
}), "circle");
|
|
147
|
+
h.addEventListener("click", (e) => {
|
|
148
|
+
if (!e.altKey || points.length <= 3)
|
|
149
|
+
return;
|
|
150
|
+
this.host.setYaml(setYamlValue(this.host.yaml(), path, points.filter((_, j) => j !== i)));
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
if (spec.walkable)
|
|
155
|
+
polygon(spec.walkable, ["walkable"], COLORS.floor);
|
|
156
|
+
spec.obstacles?.forEach((o, i) => polygon(o, ["obstacles", i], COLORS.obstacle));
|
|
157
|
+
// Features and exits: drag the box to move it (and its look-at point), corners to resize.
|
|
158
|
+
for (const group of ["features", "exits"]) {
|
|
159
|
+
for (const [name, f] of Object.entries(spec[group] ?? {})) {
|
|
160
|
+
const color = group === "exits" ? COLORS.exit : COLORS.feature;
|
|
161
|
+
const r = f.rect;
|
|
162
|
+
if (r?.length === 4) {
|
|
163
|
+
const [l, t, rt, b] = r;
|
|
164
|
+
const box = el("rect", { x: l, y: t, width: rt - l, height: b - t, fill: `${color}22`, stroke: color, "stroke-width": 0.6, cursor: "move" });
|
|
165
|
+
box.addEventListener("pointerdown", (e) => this.begin(e, () => ({
|
|
166
|
+
start: [l, t],
|
|
167
|
+
apply: (dx, dy) => {
|
|
168
|
+
let text = setYamlValue(this.host.yaml(), [group, name, "rect"], [clampX(l + dx), clampY(t + dy), clampX(rt + dx), clampY(b + dy)]);
|
|
169
|
+
if (f.at)
|
|
170
|
+
text = setYamlValue(text, [group, name, "at"], [clampX(f.at[0] + dx), clampY(f.at[1] + dy)]);
|
|
171
|
+
return text;
|
|
172
|
+
},
|
|
173
|
+
})));
|
|
174
|
+
g.append(box);
|
|
175
|
+
label(l + 2, t + 6, name, color);
|
|
176
|
+
[[l, t, 0, 1], [rt, t, 2, 1], [l, b, 0, 3], [rt, b, 2, 3]].forEach(([x, y, xi, yi]) => {
|
|
177
|
+
handle(x, y, color, () => ({
|
|
178
|
+
start: [x, y],
|
|
179
|
+
apply: (dx, dy) => {
|
|
180
|
+
const next = [l, t, rt, b];
|
|
181
|
+
next[xi] = clampX(x + dx);
|
|
182
|
+
next[yi] = clampY(y + dy);
|
|
183
|
+
const [nl, nt, nr, nb] = next;
|
|
184
|
+
return setYamlValue(this.host.yaml(), [group, name, "rect"], [Math.min(nl, nr), Math.min(nt, nb), Math.max(nl, nr), Math.max(nt, nb)]);
|
|
185
|
+
},
|
|
186
|
+
}));
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
if (f.approach) {
|
|
190
|
+
const [ax, ay] = f.approach;
|
|
191
|
+
label(ax + 3, ay + 2, "approach", color);
|
|
192
|
+
handle(ax, ay, color, () => ({ start: [ax, ay], apply: (dx, dy) => setYamlValue(this.host.yaml(), [group, name, "approach"], [clampX(ax + dx), clampY(ay + dy)]) }), "diamond");
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Props: grab the sprite (or its anchor) to move its `at`.
|
|
197
|
+
for (const [name, p] of Object.entries(spec.props ?? {})) {
|
|
198
|
+
if (!p.at)
|
|
199
|
+
continue;
|
|
200
|
+
const [x, y] = p.at;
|
|
201
|
+
const box = this.host.spriteBox(name);
|
|
202
|
+
const start = () => ({
|
|
203
|
+
start: [x, y],
|
|
204
|
+
apply: (dx, dy) => setYamlValue(this.host.yaml(), ["props", name, "at"], [clampX(x + dx), clampY(y + dy)]),
|
|
205
|
+
preview: (dx, dy) => this.host.previewProp(name, [clampX(x + dx), clampY(y + dy)]),
|
|
206
|
+
});
|
|
207
|
+
if (box) {
|
|
208
|
+
const r = el("rect", { x: box.x, y: box.y, width: box.w, height: box.h, fill: `${COLORS.prop}22`, stroke: COLORS.prop, "stroke-width": 0.6, "stroke-dasharray": "2 1", cursor: "move" });
|
|
209
|
+
r.addEventListener("pointerdown", (e) => this.begin(e, start));
|
|
210
|
+
g.append(r);
|
|
211
|
+
}
|
|
212
|
+
label(x + 3, y + 2, name, COLORS.prop);
|
|
213
|
+
handle(x, y, COLORS.prop, start, "circle");
|
|
214
|
+
}
|
|
215
|
+
// The hero: where it appears, and where it walks to on arrival.
|
|
216
|
+
const hero = spec.hero;
|
|
217
|
+
if (hero?.at && hero.enterTo) {
|
|
218
|
+
g.append(el("line", { x1: hero.at[0], y1: hero.at[1], x2: hero.enterTo[0], y2: hero.enterTo[1], stroke: COLORS.hero, "stroke-width": 0.6, "stroke-dasharray": "2 1.5", "pointer-events": "none" }));
|
|
219
|
+
}
|
|
220
|
+
for (const key of ["at", "enterTo"]) {
|
|
221
|
+
const p = hero?.[key];
|
|
222
|
+
if (!p)
|
|
223
|
+
continue;
|
|
224
|
+
label(p[0] + 3, p[1] + 2, key === "at" ? "hero" : "walks to", COLORS.hero);
|
|
225
|
+
handle(p[0], p[1], COLORS.hero, () => ({ start: p, apply: (dx, dy) => setYamlValue(this.host.yaml(), ["hero", key], [clampX(p[0] + dx), clampY(p[1] + dy)]) }), key === "at" ? "circle" : "diamond");
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else if (this.stroke) {
|
|
229
|
+
// A brush stroke in progress.
|
|
230
|
+
const color = this.mode === "floor" ? COLORS.floor : COLORS.obstacle;
|
|
231
|
+
g.append(el("polyline", { points: this.stroke.map((p) => p.join(",")).join(" "), fill: `${color}33`, stroke: color, "stroke-width": 1.2, "pointer-events": "none" }));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// --- Pointer handling ---
|
|
235
|
+
begin(e, start) {
|
|
236
|
+
if (this.mode !== "move" || e.button !== 0 || e.altKey)
|
|
237
|
+
return;
|
|
238
|
+
e.stopPropagation();
|
|
239
|
+
e.preventDefault();
|
|
240
|
+
this.drag = start();
|
|
241
|
+
this.drag.from = this.point(e);
|
|
242
|
+
this.capture(e, true);
|
|
243
|
+
this.host.holdBuild(true);
|
|
244
|
+
}
|
|
245
|
+
/** Keep getting the pointer's moves outside the layer (not for synthetic events). */
|
|
246
|
+
capture(e, on) {
|
|
247
|
+
try {
|
|
248
|
+
if (on)
|
|
249
|
+
this.svg.setPointerCapture(e.pointerId);
|
|
250
|
+
else if (this.svg.hasPointerCapture(e.pointerId))
|
|
251
|
+
this.svg.releasePointerCapture(e.pointerId);
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
/* no such pointer */
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
down(e) {
|
|
258
|
+
if (this.mode !== "floor" && this.mode !== "obstacle")
|
|
259
|
+
return;
|
|
260
|
+
e.preventDefault();
|
|
261
|
+
this.capture(e, true);
|
|
262
|
+
this.stroke = [this.point(e).map(Math.round)];
|
|
263
|
+
this.host.holdBuild(true);
|
|
264
|
+
this.render();
|
|
265
|
+
}
|
|
266
|
+
move(e) {
|
|
267
|
+
const p = this.point(e);
|
|
268
|
+
if (this.drag) {
|
|
269
|
+
const from = this.drag.from;
|
|
270
|
+
const [dx, dy] = [Math.round(p[0] - from[0]), Math.round(p[1] - from[1])];
|
|
271
|
+
this.drag.preview?.(dx, dy);
|
|
272
|
+
const text = this.drag.apply(dx, dy);
|
|
273
|
+
// Show it as it moves: the YAML (and these handles) follow the pointer.
|
|
274
|
+
if (text !== this.host.yaml())
|
|
275
|
+
this.host.setYaml(text);
|
|
276
|
+
this.render();
|
|
277
|
+
}
|
|
278
|
+
else if (this.stroke) {
|
|
279
|
+
const last = this.stroke.at(-1);
|
|
280
|
+
if (Math.hypot(p[0] - last[0], p[1] - last[1]) < 4)
|
|
281
|
+
return;
|
|
282
|
+
this.stroke.push([clampX(p[0]), clampY(p[1])]);
|
|
283
|
+
this.render();
|
|
284
|
+
// The list in the editor follows the brush, a few times a second.
|
|
285
|
+
if (performance.now() - this.lastWrite > 120)
|
|
286
|
+
this.writeStroke(false);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
up(e) {
|
|
290
|
+
this.capture(e, false);
|
|
291
|
+
if (this.drag) {
|
|
292
|
+
this.drag = undefined;
|
|
293
|
+
this.host.holdBuild(false);
|
|
294
|
+
this.render();
|
|
295
|
+
}
|
|
296
|
+
else if (this.stroke) {
|
|
297
|
+
this.writeStroke(true);
|
|
298
|
+
this.stroke = undefined;
|
|
299
|
+
this.host.holdBuild(false);
|
|
300
|
+
this.render();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/** The stroke so far as a polygon in the YAML (simplified to its corners). */
|
|
304
|
+
writeStroke(final) {
|
|
305
|
+
this.lastWrite = performance.now();
|
|
306
|
+
const points = simplify(this.stroke, final ? 2.5 : 2);
|
|
307
|
+
if (points.length < 3)
|
|
308
|
+
return;
|
|
309
|
+
const yaml = this.host.yaml();
|
|
310
|
+
if (this.mode === "floor")
|
|
311
|
+
this.host.setYaml(setYamlValue(yaml, ["walkable"], points));
|
|
312
|
+
else if (final)
|
|
313
|
+
this.host.setYaml(appendYamlItem(yaml, ["obstacles"], points));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/** Ramer-Douglas-Peucker: drop points within `epsilon` of the line through their neighbours. */
|
|
317
|
+
export function simplify(points, epsilon) {
|
|
318
|
+
if (points.length < 3)
|
|
319
|
+
return points;
|
|
320
|
+
const [a, b] = [points[0], points.at(-1)];
|
|
321
|
+
let index = 0, max = 0;
|
|
322
|
+
for (let i = 1; i < points.length - 1; i++) {
|
|
323
|
+
const p = points[i];
|
|
324
|
+
const len = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;
|
|
325
|
+
const d = Math.abs((b[0] - a[0]) * (a[1] - p[1]) - (a[0] - p[0]) * (b[1] - a[1])) / len;
|
|
326
|
+
if (d > max)
|
|
327
|
+
(max = d), (index = i);
|
|
328
|
+
}
|
|
329
|
+
if (max <= epsilon)
|
|
330
|
+
return [a, b];
|
|
331
|
+
return [...simplify(points.slice(0, index + 1), epsilon).slice(0, -1), ...simplify(points.slice(index), epsilon)];
|
|
332
|
+
}
|