@bendyline/molen-tooling 0.0.1 → 0.0.3
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/README.md +22 -4
- package/dist/capture/harness.js +216 -216
- package/dist/capture/worldgen-preview.js +288 -288
- package/dist/cli.mjs +134 -12
- package/dist/docs-src/guide/3d-art-guidelines.md +2 -2
- package/dist/docs-src/guide/3d-model-assets.md +49 -13
- package/dist/docs-src/guide/adaptive-performance.md +4 -0
- package/dist/docs-src/guide/agent-loop.md +6 -6
- package/dist/docs-src/guide/aircraft.md +25 -17
- package/dist/docs-src/guide/audio.md +247 -0
- package/dist/docs-src/guide/browser-mount.md +17 -1
- package/dist/docs-src/guide/building-interiors.md +7 -5
- package/dist/docs-src/guide/determinism.md +27 -22
- package/dist/docs-src/guide/earth-view.md +444 -0
- package/dist/docs-src/guide/entities.md +17 -6
- package/dist/docs-src/guide/examples.md +39 -23
- package/dist/docs-src/guide/experience-playback.md +2 -3
- package/dist/docs-src/guide/game-samples.md +50 -21
- package/dist/docs-src/guide/graphics-performance.md +40 -8
- package/dist/docs-src/guide/input.md +9 -0
- package/dist/docs-src/guide/markers.md +52 -0
- package/dist/docs-src/guide/materials.md +10 -3
- package/dist/docs-src/guide/navigation.md +144 -0
- package/dist/docs-src/guide/project.md +23 -3
- package/dist/docs-src/guide/quickstart.md +39 -30
- package/dist/docs-src/guide/recognizable-places.md +24 -17
- package/dist/docs-src/guide/rendering-backends.md +5 -4
- package/dist/docs-src/guide/scripting.md +8 -4
- package/dist/docs-src/guide/sky.md +26 -9
- package/dist/docs-src/guide/source-bundles.md +0 -1
- package/dist/docs-src/guide/structure-library.md +20 -10
- package/dist/docs-src/guide/terrain.md +51 -6
- package/dist/docs-src/guide/vehicles.md +27 -14
- package/dist/docs-src/guide/worldgen.md +34 -24
- package/dist/docs-src/llms.txt +35 -18
- package/dist/docs-src/schemas/README.md +4 -1
- package/dist/docs-src/schemas/archive-set.md +175 -0
- package/dist/docs-src/schemas/components.md +1104 -1
- package/dist/docs-src/schemas/matgraph.md +6 -0
- package/dist/docs-src/schemas/soundbank.md +251 -0
- package/dist/docs-src/schemas/structure-placements.md +496 -0
- package/dist/docs-src/schemas/terrain-package.md +120 -0
- package/dist/index.d.mts +31 -3
- package/dist/index.mjs +2 -2
- package/dist/{mcp-BFnCWrg0.mjs → mcp-BTVwVKFb.mjs} +78 -4
- package/dist/{ops-BGxhSP6w.mjs → ops-BPdHodFR.mjs} +1181 -61
- package/dist/templates/city-courier/README.md +66 -0
- package/dist/templates/city-courier/checks.json +20 -0
- package/dist/templates/city-courier/commands.json +32 -0
- package/dist/templates/city-courier/delivery-run.replay.json +214 -0
- package/dist/templates/city-courier/gen/molen-types.ts +1016 -0
- package/dist/templates/city-courier/index.html +2 -0
- package/dist/templates/city-courier/package.json +26 -0
- package/dist/templates/city-courier/project.json +8 -0
- package/dist/templates/city-courier/scene.json +3474 -0
- package/dist/templates/city-courier/scripts/game.ts +131 -0
- package/dist/templates/city-courier/scripts/molen-scripts.d.ts +1288 -0
- package/dist/templates/city-courier/scripts/tsconfig.json +16 -0
- package/dist/templates/city-courier/src/audio.ts +96 -0
- package/dist/templates/city-courier/src/game-shell.css +317 -0
- package/dist/templates/city-courier/src/main.ts +84 -0
- package/dist/templates/city-courier/src/scene.ts +17 -0
- package/dist/templates/city-courier/src/vite-env.d.ts +1 -0
- package/dist/templates/city-courier/src/worker.ts +5 -0
- package/dist/templates/city-courier/test/headless.test.ts +95 -0
- package/dist/templates/city-courier/test/playthrough.test.js +52 -0
- package/dist/templates/city-courier/test/replay.test.ts +22 -0
- package/dist/templates/city-courier/tsconfig.json +17 -0
- package/dist/templates/city-courier/vite.config.ts +19 -0
- package/dist/templates/cubes/README.md +51 -0
- package/dist/templates/cubes/index.html +16 -0
- package/dist/templates/cubes/package.json +24 -0
- package/dist/templates/cubes/scene.json +15 -0
- package/dist/templates/cubes/src/cubes.ts +100 -0
- package/dist/templates/cubes/src/main.ts +21 -0
- package/dist/templates/cubes/src/worker.ts +15 -0
- package/dist/templates/cubes/test/headless.test.ts +98 -0
- package/dist/templates/cubes/tsconfig.json +17 -0
- package/dist/templates/cubes/vite.config.ts +17 -0
- package/dist/templates/data-viz/README.md +47 -0
- package/dist/templates/data-viz/camera-track.json +12 -0
- package/dist/templates/data-viz/data.json +9 -0
- package/dist/templates/data-viz/index.html +16 -0
- package/dist/templates/data-viz/package.json +25 -0
- package/dist/templates/data-viz/scene.json +7 -0
- package/dist/templates/data-viz/src/main.ts +39 -0
- package/dist/templates/data-viz/src/viz.ts +91 -0
- package/dist/templates/data-viz/src/worker.ts +12 -0
- package/dist/templates/data-viz/test/headless.test.ts +75 -0
- package/dist/templates/data-viz/tsconfig.json +17 -0
- package/dist/templates/data-viz/vite.config.ts +16 -0
- package/dist/templates/figures-gallery/README.md +46 -0
- package/dist/templates/figures-gallery/index.html +16 -0
- package/dist/templates/figures-gallery/package.json +25 -0
- package/dist/templates/figures-gallery/scene.json +276 -0
- package/dist/templates/figures-gallery/src/main.ts +44 -0
- package/dist/templates/figures-gallery/src/worker.ts +17 -0
- package/dist/templates/figures-gallery/test/headless.test.ts +90 -0
- package/dist/templates/figures-gallery/tsconfig.json +17 -0
- package/dist/templates/figures-gallery/vite.config.ts +17 -0
- package/dist/templates/index.json +191 -0
- package/dist/templates/skybound/README.md +66 -0
- package/dist/templates/skybound/checks.json +24 -0
- package/dist/templates/skybound/commands.json +32 -0
- package/dist/templates/skybound/first-leap.replay.json +224 -0
- package/dist/templates/skybound/gen/molen-types.ts +1016 -0
- package/dist/templates/skybound/index.html +2 -0
- package/dist/templates/skybound/package.json +26 -0
- package/dist/templates/skybound/project.json +8 -0
- package/dist/templates/skybound/scene.json +1777 -0
- package/dist/templates/skybound/scripts/game.ts +122 -0
- package/dist/templates/skybound/scripts/molen-scripts.d.ts +1288 -0
- package/dist/templates/skybound/scripts/tsconfig.json +16 -0
- package/dist/templates/skybound/src/audio.ts +96 -0
- package/dist/templates/skybound/src/game-shell.css +317 -0
- package/dist/templates/skybound/src/main.ts +63 -0
- package/dist/templates/skybound/src/scene.ts +17 -0
- package/dist/templates/skybound/src/vite-env.d.ts +1 -0
- package/dist/templates/skybound/src/worker.ts +5 -0
- package/dist/templates/skybound/test/headless.test.ts +124 -0
- package/dist/templates/skybound/test/playthrough.test.js +38 -0
- package/dist/templates/skybound/test/replay.test.ts +22 -0
- package/dist/templates/skybound/tsconfig.json +17 -0
- package/dist/templates/skybound/vite.config.ts +19 -0
- package/dist/templates/terrain-flyover/README.md +46 -0
- package/dist/templates/terrain-flyover/index.html +16 -0
- package/dist/templates/terrain-flyover/package.json +26 -0
- package/dist/templates/terrain-flyover/src/flyover.ts +23 -0
- package/dist/templates/terrain-flyover/src/main.ts +93 -0
- package/dist/templates/terrain-flyover/terrain.json +15 -0
- package/dist/templates/terrain-flyover/test/headless.test.ts +72 -0
- package/dist/templates/terrain-flyover/tsconfig.json +17 -0
- package/dist/templates/terrain-flyover/vite.config.ts +15 -0
- package/dist/templates/top-down-arena/README.md +65 -0
- package/dist/templates/top-down-arena/arena-skirmish.replay.json +204 -0
- package/dist/templates/top-down-arena/index.html +16 -0
- package/dist/templates/top-down-arena/package.json +26 -0
- package/dist/templates/top-down-arena/project.json +10 -0
- package/dist/templates/top-down-arena/scene.json +182 -0
- package/dist/templates/top-down-arena/scripts/chase.ts +17 -0
- package/dist/templates/top-down-arena/scripts/combat.ts +13 -0
- package/dist/templates/top-down-arena/scripts/control.ts +8 -0
- package/dist/templates/top-down-arena/scripts/molen-scripts.d.ts +1258 -0
- package/dist/templates/top-down-arena/scripts/spawner.ts +11 -0
- package/dist/templates/top-down-arena/scripts/tsconfig.json +16 -0
- package/dist/templates/top-down-arena/src/arena.ts +8 -0
- package/dist/templates/top-down-arena/src/audio.ts +96 -0
- package/dist/templates/top-down-arena/src/gen/molen-types.ts +1020 -0
- package/dist/templates/top-down-arena/src/main.ts +34 -0
- package/dist/templates/top-down-arena/src/scene.ts +24 -0
- package/dist/templates/top-down-arena/src/vite-env.d.ts +1 -0
- package/dist/templates/top-down-arena/src/worker.ts +7 -0
- package/dist/templates/top-down-arena/test/headless.test.ts +87 -0
- package/dist/templates/top-down-arena/test/replay.test.ts +22 -0
- package/dist/templates/top-down-arena/tsconfig.json +17 -0
- package/dist/templates/top-down-arena/types/arena.types.json +54 -0
- package/dist/templates/top-down-arena/vite.config.ts +21 -0
- package/package.json +14 -14
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { runReplayFile } from '@bendyline/molen-tooling';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
|
|
6
|
+
// A committed replay fixture is the long-lived half of the determinism guarantee. The pinned hash
|
|
7
|
+
// in headless.test.ts freezes one run of the current build; this freezes a *recorded command log*
|
|
8
|
+
// plus per-tick reference hashes, so when behaviour drifts the harness reports the first tick that
|
|
9
|
+
// diverged instead of "the hash moved". It also distinguishes a non-deterministic build from an
|
|
10
|
+
// intentional behaviour change — refresh with `molen replay <fixture> --record`.
|
|
11
|
+
|
|
12
|
+
describe('skybound: recorded replay', () => {
|
|
13
|
+
it('reproduces the recorded run tick for tick', async () => {
|
|
14
|
+
const result = await runReplayFile({
|
|
15
|
+
path: join(process.cwd(), 'first-leap.replay.json'),
|
|
16
|
+
});
|
|
17
|
+
expect(result.error).toBeUndefined();
|
|
18
|
+
expect(result.ok, result.report).toBe(true);
|
|
19
|
+
expect(result.actualHash).toBe(result.expectedHash);
|
|
20
|
+
expect(result.firstDivergentTick).toBeUndefined();
|
|
21
|
+
}, 60_000);
|
|
22
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
|
7
|
+
"types": [],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noUncheckedIndexedAccess": true,
|
|
10
|
+
"verbatimModuleSyntax": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"noEmit": true,
|
|
14
|
+
"resolveJsonModule": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src", "test"]
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { molenScripts } from '@bendyline/molen-client/vite';
|
|
2
|
+
import { defineConfig } from 'vite';
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
// Scene scripts are authored in TypeScript; strip types when they are read as text.
|
|
6
|
+
plugins: [molenScripts()],
|
|
7
|
+
base: './',
|
|
8
|
+
// Vite gives worker bundles their own plugin pipeline, and the kernel runs in the Worker —
|
|
9
|
+
// without this the Worker would receive unstripped TypeScript.
|
|
10
|
+
worker: { format: 'es', plugins: () => [molenScripts()] },
|
|
11
|
+
build: {
|
|
12
|
+
target: 'es2022',
|
|
13
|
+
// main.ts mounts with a top-level `await`, and the client lazy-loads its WebGPU driver. By
|
|
14
|
+
// default Rollup puts three.js and the client in the entry chunk, so the lazy chunk imports
|
|
15
|
+
// the entry while the entry is still suspended at that `await`: the import never settles and
|
|
16
|
+
// the page stays blank in every browser with a WebGPU adapter. Their own chunk breaks the cycle.
|
|
17
|
+
rollupOptions: { output: { manualChunks: { vendor: ['three', '@bendyline/molen-client'] } } },
|
|
18
|
+
},
|
|
19
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Terrain flyover
|
|
2
|
+
|
|
3
|
+
A `molen/terrain@2` descriptor plus one deterministically generated 16-bit heightmap become a 4x4 grid of chunked, level-of-detail meshes you can fly over. This is the reference for the terrain capability in isolation: `@bendyline/molen-terrain/kernel` samples heights, `/client` meshes them, and the two agree — which is what keeps collision honest.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
## Run
|
|
8
|
+
|
|
9
|
+
Play it in the browser at [molen.dev/play/terrain-flyover](https://molen.dev/play/terrain-flyover/). To run and change
|
|
10
|
+
your own copy, make one from the npm packages (no clone of the engine repository needed):
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npx @bendyline/molen-tooling new my-terrain-flyover --template terrain-flyover
|
|
14
|
+
cd my-terrain-flyover
|
|
15
|
+
npm install
|
|
16
|
+
npm run dev
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
WASD: fly. Drag: look. Shift: boost. There is no scene manifest and no `input` block in this sample — the camera is a plain browser loop in `src/main.ts`, clamped to four metres above `hf.sampleHeight(x, z)` so you cannot fly through the island.
|
|
20
|
+
|
|
21
|
+
## Authoring map
|
|
22
|
+
|
|
23
|
+
- `terrain.json`: the descriptor — the whole terrain in fifteen lines of data. `chunkSize` 128 across a `gridSize` of [4, 4] gives a 512 m island at `tileResolution` 129, heights 0-120 m, three layers (`grass`, `rock` applied automatically above slope 0.45, `snow` above 85 m) and four LOD levels with skirts.
|
|
24
|
+
- `src/flyover.ts`: the config shared by the browser app and the golden test. It **validates** the descriptor instead of casting it, so schema defaults — `origin`, collision, layer and LOD fields — are applied; `buildChunkGeometry` reads `descriptor.origin`, which a bare cast leaves undefined. It also pins `HEIGHTMAP_SEED` 42 / `HEIGHTMAP_SIZE` 256 and the `FLYOVER_CAMERA` pose.
|
|
25
|
+
- `src/main.ts`: `generateHeightmapPng` to `heightfieldFromPng` to `createTerrainObject`, added to the world root of a kernel-less `createViewer`. No Worker, no ECS, no entities: here terrain is purely a client capability.
|
|
26
|
+
|
|
27
|
+
No heightmap image is checked in. It is generated from the fixed seed, so the demo and the tests build byte-identical terrain with no asset step.
|
|
28
|
+
|
|
29
|
+
## Verify
|
|
30
|
+
|
|
31
|
+
From this directory:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npx molen validate terrain.json
|
|
35
|
+
npm test
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`test/headless.test.ts` proves the seed is deterministic (identical heightmap twice), walks a 9x9 grid of `raycastDown` samples that must all sit inside the declared height range with the island centre above its edge, and meshes all sixteen chunks through `buildChunkGeometry` to more than 10,000 triangles.
|
|
39
|
+
|
|
40
|
+
In the engine repository, `test/golden/flyover.golden.test.ts` renders the island with `screenshotScene` and a `terrain: { descriptor, heightmapPng }` input — zero entities, 512x288, `FLYOVER_CAMERA`, more than 50,000 triangles after distance LOD — and compares it against the committed `test/golden/__goldens__/flyover.png`.
|
|
41
|
+
|
|
42
|
+
There is no pinned state hash and no replay fixture: nothing in this sample is simulated. `molen shot` can render it too, but it needs both a scene to hang the terrain on and a heightmap already on disk (`--terrain terrain.json --heightmap <png>`), which is why the golden test generates the PNG in process instead.
|
|
43
|
+
|
|
44
|
+
In the engine repository this sample lives in `examples/terrain-flyover/`. The same commands run from
|
|
45
|
+
that directory, and `pnpm --filter @bendyline/molen-examples-terrain-flyover test:unit` (or `test:golden`) runs its tests from the
|
|
46
|
+
root.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>molen — terrain flyover</title>
|
|
7
|
+
<style>
|
|
8
|
+
html, body { margin: 0; height: 100%; overflow: hidden; background: #adc8e6; }
|
|
9
|
+
#view { display: block; width: 100vw; height: 100vh; }
|
|
10
|
+
</style>
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
<canvas id="view"></canvas>
|
|
14
|
+
<script type="module" src="/src/main.ts"></script>
|
|
15
|
+
</body>
|
|
16
|
+
</html>
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"private": true,
|
|
3
|
+
"type": "module",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"dev": "vite --port 5225",
|
|
6
|
+
"build": "vite build",
|
|
7
|
+
"preview": "vite preview",
|
|
8
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
9
|
+
"test": "vitest run"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@bendyline/molen-client": "workspace:*",
|
|
13
|
+
"@bendyline/molen-kernel": "workspace:*",
|
|
14
|
+
"@bendyline/molen-schema": "workspace:*",
|
|
15
|
+
"@bendyline/molen-terrain": "workspace:*",
|
|
16
|
+
"three": "0.184.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@bendyline/molen-tooling": "workspace:*",
|
|
20
|
+
"@types/node": "^24",
|
|
21
|
+
"@types/three": "0.184.0",
|
|
22
|
+
"typescript": "^6.0.3",
|
|
23
|
+
"vite": "^6.4.0",
|
|
24
|
+
"vitest": "^5.0.1"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { validateByKind } from '@bendyline/molen-schema';
|
|
2
|
+
import { registerTerrainSchemas, type TerrainDescriptor } from '@bendyline/molen-terrain/kernel';
|
|
3
|
+
import terrainDoc from '../terrain.json';
|
|
4
|
+
|
|
5
|
+
// Shared config for the flyover (browser app + headless golden). The heightmap is generated
|
|
6
|
+
// from a fixed seed so the demo and the golden test agree without shipping image assets.
|
|
7
|
+
// Validate (not a bare cast) so schema defaults — origin, collision, layer/LOD fields — are
|
|
8
|
+
// applied; buildChunkGeometry reads descriptor.origin, which a raw cast leaves undefined.
|
|
9
|
+
registerTerrainSchemas();
|
|
10
|
+
const parsed = validateByKind('terrain', terrainDoc);
|
|
11
|
+
if (!parsed.ok) throw new Error(parsed.formatted);
|
|
12
|
+
export const TERRAIN = parsed.value as TerrainDescriptor;
|
|
13
|
+
export const HEIGHTMAP_SEED = 42;
|
|
14
|
+
export const HEIGHTMAP_SIZE = 256;
|
|
15
|
+
|
|
16
|
+
const worldW = TERRAIN.chunkSize * TERRAIN.gridSize[0];
|
|
17
|
+
const worldD = TERRAIN.chunkSize * TERRAIN.gridSize[1];
|
|
18
|
+
|
|
19
|
+
/** A camera pose looking across the island from the south-east. */
|
|
20
|
+
export const FLYOVER_CAMERA = {
|
|
21
|
+
position: [worldW * 0.5, 160, worldD + 160] as [number, number, number],
|
|
22
|
+
lookAt: [worldW * 0.5, 20, worldD * 0.5] as [number, number, number],
|
|
23
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { createViewer } from '@bendyline/molen-client';
|
|
2
|
+
import { createTerrainObject } from '@bendyline/molen-terrain/client';
|
|
3
|
+
import { generateHeightmapPng, heightfieldFromPng } from '@bendyline/molen-terrain/kernel';
|
|
4
|
+
import { FLYOVER_CAMERA, HEIGHTMAP_SEED, HEIGHTMAP_SIZE, TERRAIN } from './flyover';
|
|
5
|
+
|
|
6
|
+
// Browser entry: generate the island heightmap, build the terrain, and fly a free camera over
|
|
7
|
+
// it (WASD + mouse drag). No kernel needed — terrain is a client-side capability here.
|
|
8
|
+
const canvas = document.getElementById('view') as HTMLCanvasElement;
|
|
9
|
+
|
|
10
|
+
const png = generateHeightmapPng({
|
|
11
|
+
size: HEIGHTMAP_SIZE,
|
|
12
|
+
seed: HEIGHTMAP_SEED,
|
|
13
|
+
octaves: 6,
|
|
14
|
+
island: true,
|
|
15
|
+
});
|
|
16
|
+
const hf = heightfieldFromPng(TERRAIN, png);
|
|
17
|
+
|
|
18
|
+
// A kernel-less viewer: just a renderer + scene to add the terrain meshes to.
|
|
19
|
+
const viewer = await createViewer({
|
|
20
|
+
canvas,
|
|
21
|
+
width: window.innerWidth,
|
|
22
|
+
height: window.innerHeight,
|
|
23
|
+
clearColor: '#adc8e6',
|
|
24
|
+
});
|
|
25
|
+
viewer.renderer.worldRoot.add(createTerrainObject(hf, TERRAIN));
|
|
26
|
+
|
|
27
|
+
// --- free-fly camera ---
|
|
28
|
+
const cam = {
|
|
29
|
+
pos: [...FLYOVER_CAMERA.position] as [number, number, number],
|
|
30
|
+
yaw: -Math.PI / 2,
|
|
31
|
+
pitch: -0.35,
|
|
32
|
+
};
|
|
33
|
+
const keys = new Set<string>();
|
|
34
|
+
window.addEventListener('keydown', (e) => keys.add(e.code));
|
|
35
|
+
window.addEventListener('keyup', (e) => keys.delete(e.code));
|
|
36
|
+
let dragging = false;
|
|
37
|
+
canvas.addEventListener('mousedown', () => {
|
|
38
|
+
dragging = true;
|
|
39
|
+
});
|
|
40
|
+
window.addEventListener('mouseup', () => {
|
|
41
|
+
dragging = false;
|
|
42
|
+
});
|
|
43
|
+
window.addEventListener('mousemove', (e) => {
|
|
44
|
+
if (!dragging) return;
|
|
45
|
+
cam.yaw -= e.movementX * 0.003;
|
|
46
|
+
cam.pitch = Math.max(-1.5, Math.min(1.5, cam.pitch - e.movementY * 0.003));
|
|
47
|
+
});
|
|
48
|
+
window.addEventListener('resize', () =>
|
|
49
|
+
viewer.renderer.setSize(window.innerWidth, window.innerHeight),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
let last = performance.now();
|
|
53
|
+
function frame(now: number): void {
|
|
54
|
+
const dt = (now - last) / 1000;
|
|
55
|
+
last = now;
|
|
56
|
+
const speed = (keys.has('ShiftLeft') ? 220 : 90) * dt;
|
|
57
|
+
const fwd: [number, number, number] = [
|
|
58
|
+
Math.cos(cam.pitch) * Math.cos(cam.yaw),
|
|
59
|
+
Math.sin(cam.pitch),
|
|
60
|
+
Math.cos(cam.pitch) * Math.sin(cam.yaw),
|
|
61
|
+
];
|
|
62
|
+
const right: [number, number, number] = [
|
|
63
|
+
Math.cos(cam.yaw - Math.PI / 2),
|
|
64
|
+
0,
|
|
65
|
+
Math.sin(cam.yaw - Math.PI / 2),
|
|
66
|
+
];
|
|
67
|
+
const move = (v: [number, number, number], s: number): void => {
|
|
68
|
+
cam.pos[0] += v[0] * s;
|
|
69
|
+
cam.pos[1] += v[1] * s;
|
|
70
|
+
cam.pos[2] += v[2] * s;
|
|
71
|
+
};
|
|
72
|
+
if (keys.has('KeyW')) move(fwd, speed);
|
|
73
|
+
if (keys.has('KeyS')) move(fwd, -speed);
|
|
74
|
+
if (keys.has('KeyA')) move(right, -speed);
|
|
75
|
+
if (keys.has('KeyD')) move(right, speed);
|
|
76
|
+
// Clamp above the terrain (client-side heightfield).
|
|
77
|
+
const ground = hf.sampleHeight(cam.pos[0], cam.pos[2]) + 4;
|
|
78
|
+
if (cam.pos[1] < ground) cam.pos[1] = ground;
|
|
79
|
+
|
|
80
|
+
viewer.setCamera({
|
|
81
|
+
position: cam.pos,
|
|
82
|
+
lookAt: [cam.pos[0] + fwd[0], cam.pos[1] + fwd[1], cam.pos[2] + fwd[2]],
|
|
83
|
+
});
|
|
84
|
+
viewer.renderFrame();
|
|
85
|
+
requestAnimationFrame(frame);
|
|
86
|
+
}
|
|
87
|
+
requestAnimationFrame(frame);
|
|
88
|
+
|
|
89
|
+
const hint = document.createElement('div');
|
|
90
|
+
hint.textContent = 'WASD to fly · drag to look · Shift to boost';
|
|
91
|
+
hint.style.cssText =
|
|
92
|
+
'position:fixed;left:12px;bottom:10px;font:13px system-ui;color:#234;pointer-events:none';
|
|
93
|
+
document.body.appendChild(hint);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"format": "molen/terrain@2",
|
|
3
|
+
"name": "island",
|
|
4
|
+
"chunkSize": 128,
|
|
5
|
+
"tileResolution": 129,
|
|
6
|
+
"gridSize": [4, 4],
|
|
7
|
+
"height": { "min": 0, "max": 120 },
|
|
8
|
+
"tiles": { "heightUrl": "tiles/h_{x}_{z}.png" },
|
|
9
|
+
"layers": [
|
|
10
|
+
{ "name": "grass", "tiling": 8 },
|
|
11
|
+
{ "name": "rock", "tiling": 6, "auto": { "slopeMin": 0.45 } },
|
|
12
|
+
{ "name": "snow", "tiling": 4, "auto": { "heightMin": 85 } }
|
|
13
|
+
],
|
|
14
|
+
"lod": { "levels": 4, "distanceBands": [200, 400, 800, 1600], "skirts": true }
|
|
15
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { buildChunkGeometry } from '@bendyline/molen-terrain/client';
|
|
2
|
+
import {
|
|
3
|
+
generateHeightmap,
|
|
4
|
+
generateHeightmapPng,
|
|
5
|
+
heightfieldFromPng,
|
|
6
|
+
} from '@bendyline/molen-terrain/kernel';
|
|
7
|
+
import { describe, expect, it } from 'vitest';
|
|
8
|
+
// TERRAIN is the validated descriptor (schema defaults applied); importing it also registers
|
|
9
|
+
// the terrain schemas and throws if terrain.json is invalid.
|
|
10
|
+
import { HEIGHTMAP_SEED, HEIGHTMAP_SIZE, TERRAIN } from '../src/flyover';
|
|
11
|
+
|
|
12
|
+
describe('terrain flyover headless', () => {
|
|
13
|
+
it('heightmap is deterministic for the demo seed', () => {
|
|
14
|
+
const a = generateHeightmap({
|
|
15
|
+
size: HEIGHTMAP_SIZE,
|
|
16
|
+
seed: HEIGHTMAP_SEED,
|
|
17
|
+
octaves: 6,
|
|
18
|
+
island: true,
|
|
19
|
+
});
|
|
20
|
+
const b = generateHeightmap({
|
|
21
|
+
size: HEIGHTMAP_SIZE,
|
|
22
|
+
seed: HEIGHTMAP_SEED,
|
|
23
|
+
octaves: 6,
|
|
24
|
+
island: true,
|
|
25
|
+
});
|
|
26
|
+
expect(Array.from(a.data)).toEqual(Array.from(b.data));
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('kernel raycast samples terrain heights within range', () => {
|
|
30
|
+
const png = generateHeightmapPng({
|
|
31
|
+
size: HEIGHTMAP_SIZE,
|
|
32
|
+
seed: HEIGHTMAP_SEED,
|
|
33
|
+
octaves: 6,
|
|
34
|
+
island: true,
|
|
35
|
+
});
|
|
36
|
+
const hf = heightfieldFromPng(TERRAIN, png);
|
|
37
|
+
const worldW = TERRAIN.chunkSize * TERRAIN.gridSize[0];
|
|
38
|
+
// Sample a grid of points; every height must be within the declared range.
|
|
39
|
+
for (let i = 0; i <= 8; i++) {
|
|
40
|
+
for (let j = 0; j <= 8; j++) {
|
|
41
|
+
const x = (i / 8) * worldW;
|
|
42
|
+
const z = (j / 8) * worldW;
|
|
43
|
+
const h = hf.raycastDown(x, z);
|
|
44
|
+
expect(h).toBeGreaterThanOrEqual(TERRAIN.height.min - 0.001);
|
|
45
|
+
expect(h).toBeLessThanOrEqual(TERRAIN.height.max + 0.001);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// Island center should be higher than the edge (falloff).
|
|
49
|
+
const center = hf.sampleHeight(worldW / 2, worldW / 2);
|
|
50
|
+
const edge = hf.sampleHeight(2, 2);
|
|
51
|
+
expect(center).toBeGreaterThan(edge);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('builds chunk geometry for the whole grid', () => {
|
|
55
|
+
const png = generateHeightmapPng({
|
|
56
|
+
size: HEIGHTMAP_SIZE,
|
|
57
|
+
seed: HEIGHTMAP_SEED,
|
|
58
|
+
octaves: 6,
|
|
59
|
+
island: true,
|
|
60
|
+
});
|
|
61
|
+
const hf = heightfieldFromPng(TERRAIN, png);
|
|
62
|
+
let triangles = 0;
|
|
63
|
+
for (let cz = 0; cz < TERRAIN.gridSize[1]; cz++) {
|
|
64
|
+
for (let cx = 0; cx < TERRAIN.gridSize[0]; cx++) {
|
|
65
|
+
const geo = buildChunkGeometry(hf, TERRAIN, cx, cz, { skirt: true });
|
|
66
|
+
expect(geo.vertexCount).toBeGreaterThan(0);
|
|
67
|
+
triangles += geo.indices.length / 3;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
expect(triangles).toBeGreaterThan(10000);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
|
7
|
+
"types": [],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noUncheckedIndexedAccess": true,
|
|
10
|
+
"verbatimModuleSyntax": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"noEmit": true,
|
|
14
|
+
"resolveJsonModule": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src", "test"]
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
// Relative asset URLs, so one production build plays from any directory (molen.dev/play hosts
|
|
5
|
+
// the samples beneath subdirectories).
|
|
6
|
+
base: './',
|
|
7
|
+
build: {
|
|
8
|
+
target: 'es2022',
|
|
9
|
+
// main.ts mounts with a top-level `await`, and the client lazy-loads its WebGPU driver. By
|
|
10
|
+
// default Rollup puts three.js and the client in the entry chunk, so the lazy chunk imports
|
|
11
|
+
// the entry while the entry is still suspended at that `await`: the import never settles and
|
|
12
|
+
// the page stays blank in every browser with a WebGPU adapter. Their own chunk breaks the cycle.
|
|
13
|
+
rollupOptions: { output: { manualChunks: { vendor: ['three', '@bendyline/molen-client'] } } },
|
|
14
|
+
},
|
|
15
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Top-down arena
|
|
2
|
+
|
|
3
|
+
Survive a swarm in a walled arena: you move with WASD, chasers appear on a random edge every twelve ticks and cost you a hit point on contact. The whole game is **data** — `scene.json` declares the physics, the orthographic camera, the command and its payload schema, the input rule and four scripts, and there is no setup module at all. This is the reference for how far scene data plus a project type registry can take you, and the engine's central design bet: an agent can write this game as JSON.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
## Play
|
|
8
|
+
|
|
9
|
+
Play it in the browser at [molen.dev/play/top-down-arena](https://molen.dev/play/top-down-arena/). To run and change
|
|
10
|
+
your own copy, make one from the npm packages (no clone of the engine repository needed):
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npx @bendyline/molen-tooling new my-top-down-arena --template top-down-arena
|
|
14
|
+
cd my-top-down-arena
|
|
15
|
+
npm install
|
|
16
|
+
npm run dev
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
W/A/S/D: move. The scene folds the four keys into a single `axis2d` rule that emits `move` with `dir: [x, z]`, each component in -1..1. The command declares a JSON Schema payload, so a malformed direction is refused at submit with a JSON pointer into the payload rather than misbehaving later.
|
|
20
|
+
|
|
21
|
+
## Authoring map
|
|
22
|
+
|
|
23
|
+
- `scene.json`: `physics.engine: "kinematics"`, a `top-down-ortho` camera at `viewHeight` 26, the `move` command, the WASD `axis2d` rule, four script refs with their `config`, and six entities — the player, four walls, and an `audio` entity whose `audioEnvironment` holds the sound rules.
|
|
24
|
+
- `types/arena.types.json`: the `arena` namespace (`arena.wall`, `arena.player`, `arena.enemy`). Entities instantiate a `type` and override only what differs, and the spawner script spawns the same type by name. `project.json` binds the doc and points codegen at `src/gen/molen-types.ts`.
|
|
25
|
+
- `scripts/control.ts`: `molen.onCommand('move', ...)` writes the player's `kinematicBody.vel`.
|
|
26
|
+
- `scripts/spawner.ts`: one `arena.enemy` on a random edge every `config.interval` ticks, through `molen.rng`.
|
|
27
|
+
- `scripts/chase.ts`: every enemy steers straight at the player using `molen.math`.
|
|
28
|
+
- `scripts/combat.ts`: the kinematics layer's `collision` event costs 1 hp and destroys the enemy.
|
|
29
|
+
- `scripts/molen-scripts.d.ts` and `scripts/tsconfig.json` are generated by `molen types gen` — never hand-edit them.
|
|
30
|
+
- `src/scene.ts`: inlines the script files so the Worker, Vitest and the CLI all build an identical manifest. `src/main.ts` adds a HUD from mirrored state only.
|
|
31
|
+
|
|
32
|
+
## Sound
|
|
33
|
+
|
|
34
|
+
The `audio` entity's `audioEnvironment` plays arena music and maps the `player-hit` event, which `scripts/combat.ts` emits on contact, to a hit sound. `src/audio.ts` loads the CC0 `molen.sounds` content pack and plays the rules; without it the
|
|
35
|
+
game is silent. In your own copy, fetch the pack once:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
npx molen pack fetch https://molen.dev/packs/index.json molen.sounds --out-dir public/packs
|
|
39
|
+
npx molen audio plan scene.json --ticks 300
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`molen audio plan` lists the loops and one-shots the scene would play, without a browser. See
|
|
43
|
+
[Sound and music](https://molen.dev/guide/audio).
|
|
44
|
+
|
|
45
|
+
## Verify
|
|
46
|
+
|
|
47
|
+
From this directory:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
npx molen validate scene.json
|
|
51
|
+
npx molen sim run scene.json --ticks 90 --hash
|
|
52
|
+
npx molen replay arena-skirmish.replay.json
|
|
53
|
+
npx molen shot scene.json --ticks 150 --clear-color '#1a1d24' --out arena.png
|
|
54
|
+
npm test
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`npx molen types gen --check` reports whether the generated declarations are stale and `molen scripts check` type-checks all four scripts against them.
|
|
58
|
+
|
|
59
|
+
This sample carries **both** determinism artifacts, and they do different jobs. `test/headless.test.ts` pins a state hash at 90 ticks (through `runHeadless`, which is not the hash `sim run` prints) next to spawn, movement-stopped-by-wall, payload-rejection and contact-damage checks. `arena-skirmish.replay.json` freezes a recorded run — four `move` commands, 150 ticks, 45 events, per-tick reference hashes — and `test/replay.test.ts` replays it as part of `npm test`, so a behaviour drift reports the first divergent tick instead of only "the hash moved". Re-record an intentional change with `molen replay <fixture> --record`.
|
|
60
|
+
|
|
61
|
+
In the engine repository, three golden images cover the visuals (golden tests stay there; a template copy does not include them): the scene's own ortho camera block (`arena-ortho`), an angled top-down camera (`arena`), and a **played** scenario (`drive`) that walks the player east through `driveScene` while enemies spawn, asserting on the outcome and on two captured frames.
|
|
62
|
+
|
|
63
|
+
In the engine repository this sample lives in `examples/top-down-arena/`. The same commands run from
|
|
64
|
+
that directory, and `pnpm --filter @bendyline/molen-examples-top-down-arena test:unit` (or `test:golden`) runs its tests from the
|
|
65
|
+
root.
|