threedog 0.0.0-stage → 0.2.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/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +39 -2
- package/core/Animator.js +268 -0
- package/core/Assets.js +256 -0
- package/core/Audio.js +267 -0
- package/core/Baked.js +72 -0
- package/core/Batching.js +247 -0
- package/core/Budget.js +221 -0
- package/core/Clock.js +94 -0
- package/core/Commits.js +77 -0
- package/core/DebugOverrides.js +143 -0
- package/core/Describe.js +145 -0
- package/core/Engine.js +184 -0
- package/core/Events.js +153 -0
- package/core/I18n.js +94 -0
- package/core/Input.js +245 -0
- package/core/Inspect.js +77 -0
- package/core/InspectScene.js +191 -0
- package/core/Jobs.js +146 -0
- package/core/Loading.js +149 -0
- package/core/MaterialInfo.js +32 -0
- package/core/Ops.js +256 -0
- package/core/Random.js +52 -0
- package/core/Rays.js +160 -0
- package/core/SaveStore.js +85 -0
- package/core/Saves.js +254 -0
- package/core/Stream.js +279 -0
- package/core/Threads.js +158 -0
- package/core/UrlState.js +94 -0
- package/core/Vars.js +35 -0
- package/core/Views.js +49 -0
- package/core/actor.js +23 -0
- package/core/assetEntry.js +89 -0
- package/core/assetLoad.js +190 -0
- package/core/assetManifest.js +35 -0
- package/core/assetReport.js +83 -0
- package/core/audioRules.js +121 -0
- package/core/audioVoice.js +225 -0
- package/core/batchGroups.js +116 -0
- package/core/canvasBox.js +63 -0
- package/core/catalog.js +169 -0
- package/core/checks.js +67 -0
- package/core/claims.js +51 -0
- package/core/dayTime.js +15 -0
- package/core/debugView.js +76 -0
- package/core/describeRender.js +45 -0
- package/core/describeText.js +143 -0
- package/core/easing.js +34 -0
- package/core/errorCodes.js +205 -0
- package/core/errorHints.js +208 -0
- package/core/errors.js +40 -0
- package/core/events.generated.js +84 -0
- package/core/ground.js +102 -0
- package/core/holds.js +106 -0
- package/core/inputContext.js +69 -0
- package/core/inputDefaults.js +22 -0
- package/core/inputPad.js +35 -0
- package/core/inputPointer.js +48 -0
- package/core/inputTouch.js +48 -0
- package/core/lifecycle.js +69 -0
- package/core/lightWatch.js +56 -0
- package/core/look/coreParts.js +112 -0
- package/core/look/layers.js +136 -0
- package/core/look/part.js +173 -0
- package/core/look/report.js +79 -0
- package/core/look/write.js +86 -0
- package/core/loop.js +200 -0
- package/core/math.js +33 -0
- package/core/modules.js +196 -0
- package/core/movement.js +19 -0
- package/core/mulberry32.js +14 -0
- package/core/opsAtomic.js +31 -0
- package/core/opsCheck.js +97 -0
- package/core/opsFail.js +20 -0
- package/core/opsGuard.js +26 -0
- package/core/opsHistory.js +27 -0
- package/core/opsRun.js +77 -0
- package/core/page.js +78 -0
- package/core/params.js +132 -0
- package/core/pureWorker.js +5 -0
- package/core/renamed.js +21 -0
- package/core/saveApply.js +128 -0
- package/core/saveFiles.js +139 -0
- package/core/saveIds.js +53 -0
- package/core/saveVersions.js +97 -0
- package/core/scatter.js +92 -0
- package/core/sceneMethods.js +100 -0
- package/core/screenToGround.js +39 -0
- package/core/services.js +73 -0
- package/core/session.js +171 -0
- package/core/shadowTypes.js +15 -0
- package/core/skip.js +104 -0
- package/core/startup.js +135 -0
- package/core/streamCells.js +24 -0
- package/core/streamSources.js +73 -0
- package/core/styleParts.js +96 -0
- package/core/targeting.js +64 -0
- package/core/thingExplain.js +68 -0
- package/core/thingOps.js +168 -0
- package/core/things.js +212 -0
- package/core/threadPure.js +120 -0
- package/core/threadServe.js +25 -0
- package/core/tier.js +11 -0
- package/core/util.js +11 -0
- package/core/validate.js +204 -0
- package/docs/AI.md +123 -0
- package/docs/API.md +83 -0
- package/docs/CODEMAP.md +347 -0
- package/docs/COMMANDS.md +108 -0
- package/docs/CONVENTIONS.md +145 -0
- package/docs/PERF.md +217 -0
- package/docs/THREE.md +86 -0
- package/docs/api/ambience.md +15 -0
- package/docs/api/assets.md +62 -0
- package/docs/api/audio.md +53 -0
- package/docs/api/budget.md +40 -0
- package/docs/api/cameraRig.md +120 -0
- package/docs/api/characters.md +144 -0
- package/docs/api/clock.md +7 -0
- package/docs/api/collision.md +39 -0
- package/docs/api/content.md +64 -0
- package/docs/api/dialogue.md +39 -0
- package/docs/api/engine.md +199 -0
- package/docs/api/entities.md +61 -0
- package/docs/api/events.md +95 -0
- package/docs/api/flow.md +27 -0
- package/docs/api/grids.md +11 -0
- package/docs/api/ground.md +16 -0
- package/docs/api/hlod.md +34 -0
- package/docs/api/hud.md +31 -0
- package/docs/api/i18n.md +10 -0
- package/docs/api/input.md +24 -0
- package/docs/api/interactions.md +31 -0
- package/docs/api/inventory.md +66 -0
- package/docs/api/jobs.md +10 -0
- package/docs/api/level.md +41 -0
- package/docs/api/lightPool.md +56 -0
- package/docs/api/music.md +34 -0
- package/docs/api/nav.md +26 -0
- package/docs/api/net.md +14 -0
- package/docs/api/openWorld.md +24 -0
- package/docs/api/particles.md +41 -0
- package/docs/api/photo.md +15 -0
- package/docs/api/post.md +10 -0
- package/docs/api/quests.md +36 -0
- package/docs/api/rays.md +26 -0
- package/docs/api/rigid.md +84 -0
- package/docs/api/saves.md +32 -0
- package/docs/api/settings.md +36 -0
- package/docs/api/sky.md +58 -0
- package/docs/api/stream.md +31 -0
- package/docs/api/style.md +134 -0
- package/docs/api/terrain.md +34 -0
- package/docs/api/timeline.md +39 -0
- package/docs/api/tools.md +57 -0
- package/docs/api/triggers.md +56 -0
- package/docs/api/vars.md +9 -0
- package/docs/api/vegetation.md +52 -0
- package/docs/api/water.md +48 -0
- package/docs/api/weather.md +29 -0
- package/docs/api/world.md +30 -0
- package/docs/recipes/01-new-game.md +148 -0
- package/docs/recipes/02-npc.md +167 -0
- package/docs/recipes/03-items-pickups.md +158 -0
- package/docs/recipes/04-quests.md +83 -0
- package/docs/recipes/05-props-bake.md +159 -0
- package/docs/recipes/06-clock-events.md +117 -0
- package/docs/recipes/07-flow-scenes-pause.md +132 -0
- package/docs/recipes/08-weather-sky.md +74 -0
- package/docs/recipes/09-style-pack.md +234 -0
- package/docs/recipes/10-blender-level.md +67 -0
- package/docs/recipes/11-vehicles.md +55 -0
- package/docs/recipes/12-navmesh.md +79 -0
- package/docs/recipes/13-animation.md +149 -0
- package/docs/recipes/14-custom-module.md +91 -0
- package/docs/recipes/15-perf.md +78 -0
- package/docs/recipes/16-dev-bridge.md +180 -0
- package/docs/recipes/18-world.md +185 -0
- package/docs/recipes/19-shooting-picking.md +76 -0
- package/docs/recipes/20-content-actions-save.md +176 -0
- package/docs/recipes/21-camera.md +153 -0
- package/docs/recipes/22-shop-gold.md +107 -0
- package/docs/recipes/23-ops.md +175 -0
- package/docs/recipes/26-ui-parts.md +138 -0
- package/docs/recipes/27-net.md +96 -0
- package/docs/recipes/28-terrain.md +80 -0
- package/docs/recipes/29-grid.md +87 -0
- package/docs/recipes/30-templates.md +131 -0
- package/docs/recipes/31-audio.md +126 -0
- package/docs/recipes/32-action-game.md +33 -0
- package/docs/recipes/33-stream-sources.md +37 -0
- package/docs/recipes/34-environment-lighting.md +36 -0
- package/docs/recipes/35-style-from-reference.md +89 -0
- package/docs/recipes/36-sky-ground-plant-looks.md +116 -0
- package/docs/recipes/37-terrain-look-hooks.md +97 -0
- package/docs/recipes/38-environment-look-hooks.md +83 -0
- package/docs/recipes/40-scene.md +108 -0
- package/docs/recipes/41-thing-registry.md +93 -0
- package/docs/recipes/42-terrain-shape.md +64 -0
- package/docs/recipes/43-shader-patches-surface-looks.md +120 -0
- package/docs/recipes/44-water-weather-look-hooks.md +135 -0
- package/docs/recipes/45-cascaded-shadows.md +51 -0
- package/docs/recipes/46-blender-art-route.md +114 -0
- package/docs/recipes/47-island-snow-backdrop.md +44 -0
- package/docs/recipes/48-make-material.md +70 -0
- package/docs/recipes/49-game-module-contract.md +71 -0
- package/docs/recipes/50-own-renderer.md +12 -0
- package/docs/recipes/51-capability-catalog.md +63 -0
- package/docs/recipes/52-look.md +108 -0
- package/examples/action/DESIGN.md +3 -0
- package/examples/action/GAMEMAP.md +127 -0
- package/examples/action/content/actions.js +8 -0
- package/examples/action/content/audio.js +9 -0
- package/examples/action/content/index.js +10 -0
- package/examples/action/content/items.js +4 -0
- package/examples/action/content/places.js +4 -0
- package/examples/action/content/props.js +11 -0
- package/examples/action/content/templates.js +10 -0
- package/examples/action/content/views.js +6 -0
- package/examples/action/look.js +5 -0
- package/examples/action/main.js +137 -0
- package/examples/action/models.js +25 -0
- package/examples/sheepisland/GAMEMAP.md +107 -0
- package/examples/sheepisland/content/index.js +6 -0
- package/examples/sheepisland/content/props.js +19 -0
- package/examples/sheepisland/content/templates.js +5 -0
- package/examples/sheepisland/content/vegetation.js +8 -0
- package/examples/sheepisland/main.js +71 -0
- package/examples/sheepisland/params.js +25 -0
- package/examples/sheepisland/style/index.js +79 -0
- package/examples/sheepisland/style/models.js +59 -0
- package/examples/sheepisland/views.js +9 -0
- package/examples/styletest/GAMEMAP.md +135 -0
- package/examples/styletest/content/index.js +9 -0
- package/examples/styletest/content/items.js +8 -0
- package/examples/styletest/content/npcs.js +8 -0
- package/examples/styletest/content/places.js +5 -0
- package/examples/styletest/content/props.js +16 -0
- package/examples/styletest/content/templates.js +9 -0
- package/examples/styletest/content/vegetation.js +31 -0
- package/examples/styletest/look.js +11 -0
- package/examples/styletest/main.js +95 -0
- package/examples/styletest/models.js +87 -0
- package/examples/styletest/overrides.js +33 -0
- package/examples/styletest/panel.js +135 -0
- package/examples/styletest/params.js +32 -0
- package/examples/styletest/views.js +31 -0
- package/examples/world/DESIGN.md +5 -0
- package/examples/world/GAMEMAP.md +171 -0
- package/examples/world/content/actions.js +8 -0
- package/examples/world/content/audio.js +25 -0
- package/examples/world/content/bodies.js +12 -0
- package/examples/world/content/containers.js +4 -0
- package/examples/world/content/dialogues.js +33 -0
- package/examples/world/content/index.js +17 -0
- package/examples/world/content/items.js +10 -0
- package/examples/world/content/npcs.js +14 -0
- package/examples/world/content/pickups.js +11 -0
- package/examples/world/content/places.js +6 -0
- package/examples/world/content/props.js +12 -0
- package/examples/world/content/quests.js +15 -0
- package/examples/world/content/rules.js +23 -0
- package/examples/world/content/templates.js +5 -0
- package/examples/world/content/vegetation.js +32 -0
- package/examples/world/main.js +168 -0
- package/examples/world/models.js +28 -0
- package/index.d.ts +306 -0
- package/index.js +122 -0
- package/llms.txt +61 -0
- package/modules/ambience/Ambience.js +186 -0
- package/modules/camera/FirstPersonCamera.js +131 -0
- package/modules/camera/FixedCamera.js +162 -0
- package/modules/camera/FollowCamera.js +91 -0
- package/modules/camera/RtsCamera.js +132 -0
- package/modules/camera/common.js +182 -0
- package/modules/camera/index.js +11 -0
- package/modules/camera/projection.js +34 -0
- package/modules/characters/Character.js +235 -0
- package/modules/characters/NPC.js +242 -0
- package/modules/characters/assetModel.js +42 -0
- package/modules/characters/astar.js +66 -0
- package/modules/characters/behaviors.js +72 -0
- package/modules/characters/footsteps.js +13 -0
- package/modules/characters/index.js +215 -0
- package/modules/characters/moveHelpers.js +29 -0
- package/modules/characters/movers/common.js +62 -0
- package/modules/characters/movers/rapier.js +74 -0
- package/modules/characters/movers/simple.js +43 -0
- package/modules/characters/npcAvoid.js +129 -0
- package/modules/characters/npcSim.js +150 -0
- package/modules/characters/npcView.js +42 -0
- package/modules/characters/pathAsync.js +113 -0
- package/modules/characters/pathJob.js +62 -0
- package/modules/characters/pathWorker.js +7 -0
- package/modules/characters/playerStrings.js +12 -0
- package/modules/characters/proceduralRig.js +58 -0
- package/modules/characters/useModel.js +22 -0
- package/modules/characters/walkTo.js +58 -0
- package/modules/collision/Collision.js +274 -0
- package/modules/collision/grid.js +63 -0
- package/modules/content/explain.js +13 -0
- package/modules/content/ids.js +95 -0
- package/modules/content/index.js +154 -0
- package/modules/content/loader.js +190 -0
- package/modules/content/models.js +69 -0
- package/modules/content/packs.js +69 -0
- package/modules/content/props.js +89 -0
- package/modules/content/rules.js +134 -0
- package/modules/content/templates.js +189 -0
- package/modules/debug/DebugPanel.js +174 -0
- package/modules/entities/Entities.js +243 -0
- package/modules/entities/build.js +25 -0
- package/modules/entities/index.js +4 -0
- package/modules/flow/index.js +101 -0
- package/modules/gameplay/Container.js +227 -0
- package/modules/gameplay/Containers.js +89 -0
- package/modules/gameplay/Dialogue.js +219 -0
- package/modules/gameplay/Interactions.js +253 -0
- package/modules/gameplay/Inventory.js +224 -0
- package/modules/gameplay/Quests.js +205 -0
- package/modules/gameplay/boxOps.js +60 -0
- package/modules/gameplay/dialogueBox.js +48 -0
- package/modules/gameplay/interactionPrompt.js +28 -0
- package/modules/gameplay/inventoryOps.js +137 -0
- package/modules/gameplay/pickups.js +115 -0
- package/modules/gameplay/questLog.js +54 -0
- package/modules/gameplay/questSave.js +71 -0
- package/modules/grid/Grid.js +184 -0
- package/modules/grid/Layer.js +142 -0
- package/modules/grid/index.js +236 -0
- package/modules/grid/ops.js +73 -0
- package/modules/grid/preview.js +85 -0
- package/modules/grid/tiles.js +79 -0
- package/modules/hlod/index.js +167 -0
- package/modules/hud/index.js +167 -0
- package/modules/hud/parts.js +80 -0
- package/modules/level/geometry.js +46 -0
- package/modules/level/heightfield.js +160 -0
- package/modules/level/index.js +233 -0
- package/modules/level/nodes.js +84 -0
- package/modules/lights/index.js +227 -0
- package/modules/music/Music.js +294 -0
- package/modules/music/index.js +2 -0
- package/modules/music/rules.js +28 -0
- package/modules/nav/index.js +156 -0
- package/modules/nav/navPath.js +17 -0
- package/modules/net/Client.js +137 -0
- package/modules/net/Host.js +117 -0
- package/modules/net/Net.js +49 -0
- package/modules/net/index.js +77 -0
- package/modules/net/sync.js +114 -0
- package/modules/net/transports.js +118 -0
- package/modules/particles/Particles.js +179 -0
- package/modules/particles/effect.js +112 -0
- package/modules/particles/shaders.js +28 -0
- package/modules/photo/Photo.js +146 -0
- package/modules/rigid/RigidBodies.js +265 -0
- package/modules/rigid/Vehicles.js +97 -0
- package/modules/rigid/sync.js +158 -0
- package/modules/settings/Settings.js +297 -0
- package/modules/settings/apply.js +21 -0
- package/modules/settings/index.js +3 -0
- package/modules/settings/sections.js +156 -0
- package/modules/settings/strings.js +53 -0
- package/modules/settings/view.js +65 -0
- package/modules/sky/Sky.js +259 -0
- package/modules/sky/calendar.js +80 -0
- package/modules/sky/cascades.js +125 -0
- package/modules/sky/clouds.js +79 -0
- package/modules/sky/domeLayers.js +33 -0
- package/modules/sky/environment.js +141 -0
- package/modules/sky/fog.js +139 -0
- package/modules/sky/frame.js +77 -0
- package/modules/sky/layers.js +69 -0
- package/modules/sky/look.js +51 -0
- package/modules/sky/lookFog.js +80 -0
- package/modules/sky/lookShadow.js +50 -0
- package/modules/sky/lookSky.js +108 -0
- package/modules/sky/schedule.js +80 -0
- package/modules/sky/shader.js +96 -0
- package/modules/sky/shadow.js +74 -0
- package/modules/sky/skyModule.js +60 -0
- package/modules/terrain/ChunkManager.js +260 -0
- package/modules/terrain/chunkJob.js +19 -0
- package/modules/terrain/chunkLevels.js +40 -0
- package/modules/terrain/chunkQueries.js +42 -0
- package/modules/terrain/chunkWorker.js +5 -0
- package/modules/terrain/index.js +118 -0
- package/modules/terrain/lookGround.js +61 -0
- package/modules/terrain/mountTerrain.js +32 -0
- package/modules/terrain/terrainColorMap.js +41 -0
- package/modules/terrain/terrainDefaults.js +39 -0
- package/modules/terrain/terrainEdit.js +139 -0
- package/modules/terrain/terrainGen.js +155 -0
- package/modules/terrain/terrainIsland.js +131 -0
- package/modules/terrain/terrainMesh.js +92 -0
- package/modules/terrain/terrainShape.js +108 -0
- package/modules/terrain/terrainShapeMain.js +41 -0
- package/modules/terrain/terrainTexture.js +44 -0
- package/modules/timeline/Timeline.js +253 -0
- package/modules/timeline/Tween.js +114 -0
- package/modules/timeline/letterbox.js +24 -0
- package/modules/timeline/shots.js +90 -0
- package/modules/triggers/index.js +268 -0
- package/modules/vegetation/Vegetation.js +256 -0
- package/modules/vegetation/groundChunks.js +177 -0
- package/modules/vegetation/index.js +43 -0
- package/modules/vegetation/lookWind.js +37 -0
- package/modules/vegetation/modelGeometry.js +21 -0
- package/modules/water/Water.js +160 -0
- package/modules/water/look.js +80 -0
- package/modules/water/surface.js +88 -0
- package/modules/weather/Weather.js +242 -0
- package/modules/weather/look.js +75 -0
- package/modules/weather/particles.js +92 -0
- package/modules/weather/shaders.js +49 -0
- package/modules/weather/strings.js +15 -0
- package/modules/world/World.js +231 -0
- package/modules/world/cells.js +42 -0
- package/modules/world/explain.js +11 -0
- package/modules/world/index.js +71 -0
- package/modules/world/persist.js +107 -0
- package/modules/world/pos.js +20 -0
- package/modules/world/records.js +89 -0
- package/modules/world/sim.js +175 -0
- package/modules/world/simFar.js +82 -0
- package/modules/world/state.js +115 -0
- package/modules/world/thingSource.js +44 -0
- package/modules/world/views.js +182 -0
- package/package.json +66 -4
- package/presets.js +96 -0
- package/render/PostFX.js +240 -0
- package/render/detail.js +105 -0
- package/render/effects.js +144 -0
- package/render/engineUniforms.js +23 -0
- package/render/fuse.js +84 -0
- package/render/lighting.js +100 -0
- package/render/lights.js +18 -0
- package/render/lookSurface.js +161 -0
- package/render/materials.js +220 -0
- package/render/patches.js +252 -0
- package/render/postLook.js +86 -0
- package/render/surface.js +69 -0
- package/render/uniformDecl.js +29 -0
- package/types/common.d.ts +34 -0
- package/types/core.d.ts +1965 -0
- package/types/gameplay.d.ts +1822 -0
- package/types/generated/core/Audio.d.ts +200 -0
- package/types/generated/core/Batching.d.ts +47 -0
- package/types/generated/core/Budget.d.ts +90 -0
- package/types/generated/core/Commits.d.ts +31 -0
- package/types/generated/core/DebugOverrides.d.ts +52 -0
- package/types/generated/core/Events.d.ts +89 -0
- package/types/generated/core/I18n.d.ts +30 -0
- package/types/generated/core/Input.d.ts +118 -0
- package/types/generated/core/Jobs.d.ts +41 -0
- package/types/generated/core/Ops.d.ts +203 -0
- package/types/generated/core/Random.d.ts +49 -0
- package/types/generated/core/Rays.d.ts +49 -0
- package/types/generated/core/Saves.d.ts +162 -0
- package/types/generated/core/Stream.d.ts +93 -0
- package/types/generated/core/Threads.d.ts +82 -0
- package/types/generated/core/Views.d.ts +33 -0
- package/types/generated/core/audioVoice.d.ts +47 -0
- package/types/generated/core/page.d.ts +18 -0
- package/types/generated/core/session.d.ts +48 -0
- package/types/generated/modules/ambience/Ambience.d.ts +48 -0
- package/types/generated/modules/debug/DebugPanel.d.ts +24 -0
- package/types/generated/modules/flow/index.d.ts +10 -0
- package/types/generated/modules/grid/Grid.d.ts +151 -0
- package/types/generated/modules/grid/Layer.d.ts +65 -0
- package/types/generated/modules/grid/index.d.ts +86 -0
- package/types/generated/modules/hlod/index.d.ts +11 -0
- package/types/generated/modules/hud/index.d.ts +10 -0
- package/types/generated/modules/lights/index.d.ts +13 -0
- package/types/generated/modules/music/Music.d.ts +111 -0
- package/types/generated/modules/net/Net.d.ts +34 -0
- package/types/generated/modules/net/index.d.ts +53 -0
- package/types/generated/modules/net/transports.d.ts +58 -0
- package/types/generated/modules/particles/Particles.d.ts +69 -0
- package/types/generated/modules/sky/Sky.d.ts +145 -0
- package/types/generated/modules/sky/skyModule.d.ts +10 -0
- package/types/generated/modules/timeline/Timeline.d.ts +51 -0
- package/types/generated/modules/water/Water.d.ts +29 -0
- package/types/generated/modules/weather/Weather.d.ts +62 -0
- package/types/generated/modules/world/World.d.ts +242 -0
- package/types/generated/modules/world/index.d.ts +15 -0
- package/types/generated/render/PostFX.d.ts +125 -0
- package/types/generated/render/materials.d.ts +100 -0
- package/types/generated/render/patches.d.ts +41 -0
- package/types/generated/ui/Focus.d.ts +11 -0
- package/types/generated/ui/LoadingScreen.d.ts +2 -0
- package/types/generated/ui/UI.d.ts +57 -0
- package/types/nav.d.ts +52 -0
- package/types/physics.d.ts +592 -0
- package/types/style.d.ts +638 -0
- package/types/worker.d.ts +11 -0
- package/types/world.d.ts +772 -0
- package/ui/Focus.js +51 -0
- package/ui/LoadingScreen.js +42 -0
- package/ui/Parts.js +199 -0
- package/ui/UI.js +167 -0
- package/ui/defaults.js +85 -0
- package/ui/theme.js +124 -0
- package/ui/touchPart.js +49 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// Background music module: tracks on the "music" bus, cross-faded; chosen by a small rule list; optional stems that follow an intensity value.
|
|
2
|
+
//
|
|
3
|
+
// engine.use(music({ tracks: {
|
|
4
|
+
// explore: '/music/explore.ogg',
|
|
5
|
+
// combat: { url: '/music/combat-base.ogg', stems: [{ url: '/music/combat-drums.ogg', at: [0.3, 0.7] }, { url: '/music/combat-brass.ogg', at: [0.7, 1] }] },
|
|
6
|
+
// win: { url: '/music/win.ogg', loop: false },
|
|
7
|
+
// } }));
|
|
8
|
+
// engine.music.play('explore') // 2 s cross-fade by default; play('explore', { fade: 0 }) cuts at once
|
|
9
|
+
// engine.music.intensity = 0.8 // stems fade in as it rises through their `at` range (0-1)
|
|
10
|
+
// engine.music.stinger('win') // a short piece over the music; the music dips under it
|
|
11
|
+
// engine.music.stop(3) // fade out over 3 s
|
|
12
|
+
//
|
|
13
|
+
// Rules (checked about once a second; the highest-priority match wins; a new result must hold `hold` seconds before switching):
|
|
14
|
+
// engine.music.rules([
|
|
15
|
+
// { track: 'combat', when: { state: 'combat' }, priority: 10 }, // engine.audio.state = 'combat' (the game sets it)
|
|
16
|
+
// { track: 'cave', when: { zone: 'cave' }, priority: 5 }, // condition keys: state, zone (triggers), time (sky), weather
|
|
17
|
+
// { track: 'night', when: { time: 'night' }, priority: 2 },
|
|
18
|
+
// { track: 'explore', priority: 0 }, // no when = always: the fallback
|
|
19
|
+
// { track: null, when: e => e.dialogue?.active, priority: 20 }, // a function works too; track: null = silence
|
|
20
|
+
// ], { hold: 2, fade: 3 });
|
|
21
|
+
// engine.music.override('boss') // pick by hand, pausing the rules; override(null) hands control back
|
|
22
|
+
// Tracks and rules may also come from createEngine({ audio: { music: { tracks, rules, fade } } }). Ducking is engine.audio.duck({ music: 0.3 });
|
|
23
|
+
// music({ duckDialogue: 0.35 }) does that during dialogue (off by default: whether dialogue lowers the music is the game's call). Before the first click play()
|
|
24
|
+
// is remembered and starts once audio is usable.
|
|
25
|
+
//
|
|
26
|
+
// Events: music:change { from, to }, music:stinger { name }
|
|
27
|
+
|
|
28
|
+
import { checkOptions, warnMissing, warnOnce } from '../../core/validate.js';
|
|
29
|
+
import { readParams, keysSchema } from '../../core/params.js';
|
|
30
|
+
import { pickTrack, evaluateRules } from './rules.js';
|
|
31
|
+
/** @import { ModuleDef } from '../../types/core' */
|
|
32
|
+
/** @import { MusicTrack, MusicRule, MusicOptions } from '../../types/gameplay' */
|
|
33
|
+
|
|
34
|
+
const DEFAULTS = { fade: 2, duckDialogue: false, duckFade: 0.4, hold: 2, interval: 1 };
|
|
35
|
+
const RULE_KEYS = ['track', 'when', 'priority'];
|
|
36
|
+
const stemLevel = (st, x) => { const [lo, hi] = st.at ?? [0, 1]; return (st.volume ?? 1) * Math.min(1, Math.max(0, hi > lo ? (x - lo) / (hi - lo) : +(x >= lo))); };
|
|
37
|
+
|
|
38
|
+
/** engine.music */
|
|
39
|
+
export class Music {
|
|
40
|
+
/** @internal */
|
|
41
|
+
constructor(engine, opts = {}) {
|
|
42
|
+
/** @internal */ this.engine = engine;
|
|
43
|
+
const st = engine.audio.config.music;
|
|
44
|
+
/** @internal */ this.opts = { ...DEFAULTS, ...st, ...opts };
|
|
45
|
+
/** Registered tracks @type {Map<string, { url?: string, volume?: number, loop?: boolean, stems?: { url: string, at?: number[], volume?: number }[] }>} */
|
|
46
|
+
this.tracks = new Map();
|
|
47
|
+
this.addTracks(st.tracks || {});
|
|
48
|
+
this.addTracks(opts.tracks || {});
|
|
49
|
+
/** Name of the track playing (or wanted) @type {string | null} */
|
|
50
|
+
this.current = null;
|
|
51
|
+
/** 0-1: how loud each stem of the playing track is (see the stems `at` range); fades in about half a second @type {number} */
|
|
52
|
+
this.intensity = 0;
|
|
53
|
+
/** @internal */ this.playing = null; // { name, gain, srcs, stems: [{ def, gain }], token }
|
|
54
|
+
/** @internal */ this.pending = null; // { name, fade } remembered while audio is still locked
|
|
55
|
+
/** @internal */ this._token = 0;
|
|
56
|
+
/** @internal */ this._rules = null;
|
|
57
|
+
/** @internal */ this._ruleOpts = {};
|
|
58
|
+
/** @internal */ this._candidate = undefined;
|
|
59
|
+
/** @internal */ this._candidateT = 0;
|
|
60
|
+
/** @internal */ this._ruleT = 0;
|
|
61
|
+
/** @internal */ this._override = undefined;
|
|
62
|
+
/** @internal */ this._duck = null;
|
|
63
|
+
/** @internal */ this._lastIntensity = -1;
|
|
64
|
+
/** @internal the game's own rules (rules() / options) and each content pack's: the rule list is both, the game's first */
|
|
65
|
+
this._base = null;
|
|
66
|
+
/** @internal */ this._packRules = new Map();
|
|
67
|
+
const rules = opts.rules ?? st.rules;
|
|
68
|
+
if (rules) this.rules(rules);
|
|
69
|
+
// content packs (audio: { music: { tracks, rules } }): tracks by name, rules appended; a pack loaded again replaces its own rules
|
|
70
|
+
// (createEngine({ audio: { music: { tracks } } }) names are checked by engine.audio.contentPack; music({ tracks }) ones here)
|
|
71
|
+
const code = new Set(Object.keys(opts.tracks || {}));
|
|
72
|
+
engine.audio.onPack((key, pack) => {
|
|
73
|
+
for (const name of Object.keys(pack.music.tracks)) if (code.has(name)) throw new Error(`music track '${name}' is defined both in music({ tracks }) `
|
|
74
|
+
+ `and in content ${pack.from}. One name, one place: keep it in one of them [audio-name-clash]`);
|
|
75
|
+
this.addTracks(pack.music.tracks);
|
|
76
|
+
this._packRules.set(key, { from: pack.from, rules: pack.music.rules });
|
|
77
|
+
this._setRules();
|
|
78
|
+
});
|
|
79
|
+
// Duck the music bus during dialogue only when the game asks: music({ duckDialogue: 0.35 }) (0-1 multiplier; false / 1 = no duck)
|
|
80
|
+
if (this.opts.duckDialogue !== false && this.opts.duckDialogue != null && this.opts.duckDialogue < 1) {
|
|
81
|
+
engine.events.on('dialogue:start', () => { this._duck?.remove(); this._duck = engine.audio.duck({ music: this.opts.duckDialogue }, { fade: this.opts.duckFade }); });
|
|
82
|
+
engine.events.on('dialogue:end', () => { this._duck?.remove(this.opts.duckFade * 2); this._duck = null; });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Register tracks: { name: url | { url, volume, loop, stems } }
|
|
88
|
+
* @param {Record<string, MusicTrack>} map
|
|
89
|
+
* @returns {this}
|
|
90
|
+
*/
|
|
91
|
+
addTracks(map) {
|
|
92
|
+
for (const [name, v] of Object.entries(map)) {
|
|
93
|
+
const tr = typeof v === 'string' ? { url: v } : { ...v };
|
|
94
|
+
if (tr.url == null && !tr.stems?.length) throw new TypeError(`music track '${name}' needs url or stems, e.g. { url: '/music/${name}.ogg' }`);
|
|
95
|
+
this.tracks.set(name, tr);
|
|
96
|
+
}
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Whether audio is usable
|
|
101
|
+
* @returns {boolean}
|
|
102
|
+
*/
|
|
103
|
+
get ready() { return this.engine.audio.running; }
|
|
104
|
+
/** Auto track rules are in use */
|
|
105
|
+
get ruled() { return !!this._rules; }
|
|
106
|
+
/**
|
|
107
|
+
* State (for debugging and tests): gain is the playing track's fade level, stems each stem's level
|
|
108
|
+
* @returns {{ current: string | null, playing: string | null, pending: string | null, override: string | null, intensity: number, gain: number, stems:
|
|
109
|
+
* number[] }}
|
|
110
|
+
*/
|
|
111
|
+
get state() {
|
|
112
|
+
const p = this.playing;
|
|
113
|
+
return { current: this.current, playing: p?.name ?? null, pending: this.pending?.name ?? null, override: this._override ?? null, intensity: this.intensity,
|
|
114
|
+
gain: p ? +p.gain.gain.value.toFixed(3) : 0, stems: p ? p.stems.map(s => +s.gain.gain.value.toFixed(3)) : [] };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ---------- playback ----------
|
|
118
|
+
/**
|
|
119
|
+
* Play a track (cross-fade over fade seconds). Does nothing if it is already playing; force replays it
|
|
120
|
+
* @param {string | null} name
|
|
121
|
+
* @param {{ fade?: number, force?: boolean }} [opts]
|
|
122
|
+
* @returns {void}
|
|
123
|
+
*/
|
|
124
|
+
play(name, opts = {}) {
|
|
125
|
+
const { fade = this.opts.fade, force = false } = opts;
|
|
126
|
+
if (name == null) return this.stop(fade);
|
|
127
|
+
const tr = this.tracks.get(name);
|
|
128
|
+
if (!tr) { warnMissing('track', name, [...this.tracks.keys()]); return; }
|
|
129
|
+
if (!force && this.current === name && (this.playing?.name === name || this.pending?.name === name)) return;
|
|
130
|
+
const from = this.current;
|
|
131
|
+
this.current = name;
|
|
132
|
+
if (from !== name) this.engine.events.emit('music:change', { from, to: name });
|
|
133
|
+
const audio = this.engine.audio;
|
|
134
|
+
if (!audio.running || !this._ensureOutput()) { this.pending = { name, fade }; return; } // cannot sound yet: remember it
|
|
135
|
+
this.pending = null;
|
|
136
|
+
this._fadeOutCurrent(fade);
|
|
137
|
+
const token = ++this._token;
|
|
138
|
+
const ctx = audio.ctx, g = ctx.createGain(), vol = tr.volume ?? 1;
|
|
139
|
+
g.gain.setValueAtTime(fade > 0 ? 0 : vol, ctx.currentTime);
|
|
140
|
+
g.connect(this.output);
|
|
141
|
+
const stems = (tr.stems || []).map(def => { const sg = ctx.createGain(); sg.gain.value = stemLevel(def, this.intensity); sg.connect(g); return { def, gain: sg }; });
|
|
142
|
+
const slot = { name, gain: g, srcs: [], stems, token };
|
|
143
|
+
this.playing = slot;
|
|
144
|
+
const parts = [...(tr.url != null ? [{ url: tr.url, out: g }] : []), ...stems.map(s => ({ url: s.def.url, out: s.gain }))];
|
|
145
|
+
// all parts decode first, then start on the same audio-clock instant: stems only line up if they begin together
|
|
146
|
+
Promise.all(parts.map(p => audio.load(p.url))).then(bufs => {
|
|
147
|
+
if (this._token !== token || this.playing !== slot) { g.disconnect(); return; } // another track was chosen while loading
|
|
148
|
+
const t = ctx.currentTime + 0.02, loop = tr.loop !== false;
|
|
149
|
+
bufs.forEach((buf, i) => { const src = ctx.createBufferSource(); src.buffer = buf; src.loop = loop; src.connect(parts[i].out); src.start(t); slot.srcs.push(src); });
|
|
150
|
+
if (fade > 0) { g.gain.setValueAtTime(0, t); g.gain.linearRampToValueAtTime(vol, t + fade); }
|
|
151
|
+
if (!loop && slot.srcs[0]) slot.srcs[0].onended = () => { if (this.playing === slot) { this.playing = null; this.current = null; this.engine.events.emit('music:change', { from: name, to: null }); } };
|
|
152
|
+
}).catch(err => { warnOnce(err.message); if (this.playing === slot) this.playing = null; }); // a missing file is said once; the game goes on silent
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Fade out and stop (fade over fade seconds)
|
|
156
|
+
* @param {number} [fade]
|
|
157
|
+
* @returns {void}
|
|
158
|
+
*/
|
|
159
|
+
stop(fade = this.opts.fade) {
|
|
160
|
+
const from = this.current;
|
|
161
|
+
this.pending = null; this.current = null; this._token++;
|
|
162
|
+
this._fadeOutCurrent(fade);
|
|
163
|
+
if (from) this.engine.events.emit('music:change', { from, to: null });
|
|
164
|
+
}
|
|
165
|
+
/** @internal */
|
|
166
|
+
_fadeOutCurrent(fade) {
|
|
167
|
+
const p = this.playing; this.playing = null;
|
|
168
|
+
if (!p) return;
|
|
169
|
+
const ctx = this.engine.audio.ctx, t = ctx.currentTime, f = Math.max(0.02, fade);
|
|
170
|
+
p.gain.gain.cancelScheduledValues(t);
|
|
171
|
+
p.gain.gain.setValueAtTime(p.gain.gain.value, t);
|
|
172
|
+
p.gain.gain.linearRampToValueAtTime(0, t + f);
|
|
173
|
+
for (const s of p.srcs) { try { s.stop(t + f + 0.05); } catch { /* already stopped */ } }
|
|
174
|
+
if (p.srcs[0]) p.srcs[0].onended = () => p.gain.disconnect(); // free the nodes once the fade has really ended
|
|
175
|
+
}
|
|
176
|
+
/** @internal the background bed: tracks connect here, then to the music bus; a stinger dips this node, never the bus (it plays on the bus itself) */
|
|
177
|
+
_ensureOutput() {
|
|
178
|
+
const a = this.engine.audio;
|
|
179
|
+
if (this.output) return true;
|
|
180
|
+
if (!a.ctx) return false;
|
|
181
|
+
/** @internal */ this.output = a.ctx.createGain();
|
|
182
|
+
this.output.connect(a.buses.music);
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Layer a one-shot piece (victory, discovered place...); the background dips to `duck` while it plays
|
|
188
|
+
* @param {string} name
|
|
189
|
+
* @param {{ duck?: number, volume?: number }} [opts]
|
|
190
|
+
* @returns {void}
|
|
191
|
+
*/
|
|
192
|
+
stinger(name, opts = {}) {
|
|
193
|
+
const { duck = 0.5, volume } = opts;
|
|
194
|
+
const tr = this.tracks.get(name);
|
|
195
|
+
if (!tr) { warnMissing('track', name, [...this.tracks.keys()]); return; }
|
|
196
|
+
const a = this.engine.audio;
|
|
197
|
+
if (!a.running || !this._ensureOutput()) return; // a one-shot piece is not worth queueing
|
|
198
|
+
this.engine.events.emit('music:stinger', { name });
|
|
199
|
+
a.play(tr.url, { bus: 'music', volume: volume ?? tr.volume ?? 1 }).then(h => {
|
|
200
|
+
const d = h.source.buffer?.duration || 1, g = this.output.gain, t = a.ctx.currentTime;
|
|
201
|
+
if (duck >= 1) return;
|
|
202
|
+
// scheduled on the audio clock (no timers): dip, hold for the stinger's length, come back
|
|
203
|
+
g.cancelScheduledValues(t); g.setValueAtTime(g.value, t); g.linearRampToValueAtTime(duck, t + 0.15);
|
|
204
|
+
g.setValueAtTime(duck, t + d); g.linearRampToValueAtTime(1, t + d + 0.3);
|
|
205
|
+
}).catch(err => warnOnce(err.message));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ---------- rules ----------
|
|
209
|
+
/**
|
|
210
|
+
* Set the auto track rules (null turns them off). opts: hold how many seconds a new result must be stable before switching, fade switch fade seconds,
|
|
211
|
+
* interval check interval
|
|
212
|
+
* @param {MusicRule[] | null} list
|
|
213
|
+
* @param {{ hold?: number, fade?: number, interval?: number }} [opts]
|
|
214
|
+
* @returns {this}
|
|
215
|
+
*/
|
|
216
|
+
rules(list, opts = {}) {
|
|
217
|
+
checkOptions('music.rules()', opts, ['hold', 'fade', 'interval']);
|
|
218
|
+
this._base = list ? [...list] : null;
|
|
219
|
+
this._ruleOpts = opts;
|
|
220
|
+
this._setRules();
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
/** @internal the live rule list: the game's rules, then every content pack's (null = no rules at all) */
|
|
224
|
+
_setRules() {
|
|
225
|
+
for (const r of this._base || []) checkOptions('music.rules() rule', r, RULE_KEYS);
|
|
226
|
+
// a pack's rule names its pack in the message, so a typo points at the right data file
|
|
227
|
+
for (const p of this._packRules.values()) for (const r of p.rules) checkOptions(`content ${p.from} audio.music.rules rule`, r, RULE_KEYS);
|
|
228
|
+
const all = [...(this._base || []), ...[...this._packRules.values()].flatMap(p => p.rules)];
|
|
229
|
+
this._rules = all.length || this._base ? all : null;
|
|
230
|
+
this._candidate = undefined; this._candidateT = 0; this._ruleT = 0;
|
|
231
|
+
if (this._rules) this._evaluate(true);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Pick a track by hand, pausing the rules; override(null) hands control back
|
|
235
|
+
* @param {string | null} name
|
|
236
|
+
* @param {{ fade?: number, force?: boolean }} [opts]
|
|
237
|
+
* @returns {void}
|
|
238
|
+
*/
|
|
239
|
+
override(name, opts) {
|
|
240
|
+
if (name == null) { this._override = undefined; if (this._rules) this._evaluate(true); return; }
|
|
241
|
+
this._override = name; this.play(name, opts);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Which track the current rules would pick (without switching)
|
|
245
|
+
* @returns {string | null | undefined}
|
|
246
|
+
*/
|
|
247
|
+
pick() { return pickTrack(this); }
|
|
248
|
+
/** @internal */
|
|
249
|
+
_evaluate(immediate = false) { return evaluateRules(this, immediate); }
|
|
250
|
+
/**
|
|
251
|
+
* Per-frame update (plays queued tracks, follows intensity, picks tracks by rules; the module calls it every frame)
|
|
252
|
+
* @param {number} dt
|
|
253
|
+
* @returns {void}
|
|
254
|
+
* @internal
|
|
255
|
+
*/
|
|
256
|
+
frame(dt) {
|
|
257
|
+
// Once audio is usable, play the queued track
|
|
258
|
+
if (this.pending && this.engine.audio.running) { const p = this.pending; this.pending = null; this.play(p.name, { fade: p.fade, force: true }); }
|
|
259
|
+
if (this.playing?.stems.length && this.intensity !== this._lastIntensity) {
|
|
260
|
+
this._lastIntensity = this.intensity;
|
|
261
|
+
const t = this.engine.audio.ctx.currentTime;
|
|
262
|
+
for (const s of this.playing.stems) s.gain.gain.setTargetAtTime(stemLevel(s.def, this.intensity), t, 0.15);
|
|
263
|
+
}
|
|
264
|
+
if (!this._rules) return;
|
|
265
|
+
const iv = this._ruleOpts.interval ?? this.opts.interval;
|
|
266
|
+
if (this._candidate !== undefined) this._candidateT += dt;
|
|
267
|
+
if ((this._ruleT -= dt) > 0) return;
|
|
268
|
+
this._ruleT = iv;
|
|
269
|
+
this._evaluate(false);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const MUSIC_PARAMS = keysSchema(['tracks', 'rules', ...Object.keys(DEFAULTS)], DEFAULTS);
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Background music -> engine.music. tracks; rules; fade: default fade seconds (2); duckDialogue: duck the music bus to this level during dialogue (default
|
|
277
|
+
* false = off; e.g. 0.35); duckFade; hold: rule debounce seconds (2)
|
|
278
|
+
* @param {MusicOptions} [opts]
|
|
279
|
+
* @returns {ModuleDef<Music>}
|
|
280
|
+
* @example engine.use(music({ tracks: { explore: '/music/explore.ogg' }, rules: [{ track: 'explore' }] }));
|
|
281
|
+
*/
|
|
282
|
+
export function music(opts = {}) {
|
|
283
|
+
readParams('music', MUSIC_PARAMS, opts);
|
|
284
|
+
let m = null;
|
|
285
|
+
return {
|
|
286
|
+
name: 'music', params: MUSIC_PARAMS, options: opts, label: 'Music', optional: ['dialogue'], stage: 'audio', presentation: true,
|
|
287
|
+
install(e) {
|
|
288
|
+
m = new Music(e, opts);
|
|
289
|
+
e.events.on('session:end', () => m.stop(0)); // a playthrough ends: its music stops at once
|
|
290
|
+
return m;
|
|
291
|
+
},
|
|
292
|
+
frame(dt) { m.frame(dt); },
|
|
293
|
+
};
|
|
294
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Music rule evaluation (helper of Music.js): which rule wins, and when to switch (debounced by `hold`, immediate on request). `m` is the Music instance.
|
|
2
|
+
import { reportError } from '../../core/errors.js';
|
|
3
|
+
|
|
4
|
+
export function pickTrack(m) {
|
|
5
|
+
if (!m._rules) return undefined;
|
|
6
|
+
let best = null;
|
|
7
|
+
for (const r of m._rules) {
|
|
8
|
+
let ok = false;
|
|
9
|
+
try { ok = m.engine.audio.matches(r.when, m); } catch (err) { reportError(m.engine, err, 'music rule when', { soft: true }); }
|
|
10
|
+
if (ok && (!best || (r.priority ?? 0) > (best.priority ?? 0))) best = r;
|
|
11
|
+
}
|
|
12
|
+
return best ? best.track ?? null : undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function evaluateRules(m, immediate = false) {
|
|
16
|
+
if (m._override !== undefined) return;
|
|
17
|
+
const want = pickTrack(m);
|
|
18
|
+
if (want === undefined) return; // no rule matches: keep the current state
|
|
19
|
+
const cur = m.current ?? null;
|
|
20
|
+
if (want === cur) { m._candidate = undefined; m._candidateT = 0; return; }
|
|
21
|
+
const hold = m._ruleOpts.hold ?? m.opts.hold;
|
|
22
|
+
if (want !== m._candidate) { m._candidate = want; m._candidateT = 0; }
|
|
23
|
+
if (immediate || m._candidateT >= hold) {
|
|
24
|
+
m._candidate = undefined; m._candidateT = 0;
|
|
25
|
+
const fade = m._ruleOpts.fade ?? m.opts.fade;
|
|
26
|
+
want == null ? m.stop(fade) : m.play(want, { fade });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Navmesh (Recast): generated after a level loads; engine.characters.findPath switches to it automatically (falls back to A* when not installed).
|
|
2
|
+
// engine.use(level({ url }), collision(), characters(), nav({ agentRadius: 0.4 }));
|
|
3
|
+
// Once installed, engine.nav offers findPath(from, to) and the native objects navMesh / query / tileCache / crowd / helper.
|
|
4
|
+
// version: +1 on every rebuild / unload. Native objects (crowd agents etc.) are destroyed on rebuild; code that cached them must compare version and re-fetch when it changed (NPCs do this).
|
|
5
|
+
//
|
|
6
|
+
// Where the mesh comes from: the ground API's navGeometry() (optional, { positions, indices }); otherwise staticTrimeshes() (the level's walkable mesh).
|
|
7
|
+
// Walls and obstacles: boxes/circles in the collision module (level col_ boxes, ones added by game collision.addBox) are all synced into TileCache dynamic obstacles,
|
|
8
|
+
// and affected tiles are rebuilt on add/remove, so level walls and later-added obstacles work the same way.
|
|
9
|
+
// Levels only for now (ground with staticTrimeshes); procedural terrain keeps using A*. Recast (about 1MB) is downloaded only at init.
|
|
10
|
+
// The mesh is built once per level: ground edits (engine.terrain.edit, 'ground:change') do not update it, so paths keep following the ground as it was
|
|
11
|
+
// at level load (rebuilding all tiles per edit is too slow to do automatically). A game that reshapes walkable ground at runtime leaves nav out:
|
|
12
|
+
// characters.findPath then uses A*, which resamples the edited area (its rasters are dropped on 'ground:change').
|
|
13
|
+
import { readParams, keysSchema } from '../../core/params.js';
|
|
14
|
+
import { xzOf } from '../../core/math.js';
|
|
15
|
+
import { navSearch } from './navPath.js';
|
|
16
|
+
|
|
17
|
+
const NAV_PARAMS = keysSchema(['cellSize', 'agentRadius', 'agentHeight', 'maxClimb', 'maxSlope', 'tileSize', 'crowd', 'debug']);
|
|
18
|
+
|
|
19
|
+
export function nav(opts = {}) {
|
|
20
|
+
readParams('nav', NAV_PARAMS, opts);
|
|
21
|
+
const { cellSize = 0.3, tileSize = 32, crowd = false, debug = false } = opts;
|
|
22
|
+
// agent size defaults come from engine.movement (filled at install); opts values override them
|
|
23
|
+
let agentRadius, agentHeight, maxClimb, maxSlope;
|
|
24
|
+
let e = null, lib = null, gen = null, pending = false, version = -1;
|
|
25
|
+
const obstacles = new Map(); // collision-module collider → Recast obstacle (big boxes are cut into several pieces)
|
|
26
|
+
|
|
27
|
+
const api = {
|
|
28
|
+
ready: false, agent: null, navMesh: null, query: null, tileCache: null, crowd: null, helper: null,
|
|
29
|
+
/** +1 on rebuild / unload: code that cached native objects such as crowd agents must re-create them when it changes */
|
|
30
|
+
version: 0,
|
|
31
|
+
/**
|
|
32
|
+
* Path [{ x, z }...] (meters) from `from` to `to`; null if not ready or unreachable.
|
|
33
|
+
* Pass { detailed: true } to get { status: 'ok' | 'noPath' | 'notReady', path } instead: tells "navmesh not generated yet" from "generated but unreachable"
|
|
34
|
+
*/
|
|
35
|
+
findPath(from, to, o = {}) {
|
|
36
|
+
const res = search(from, to, o);
|
|
37
|
+
return o.detailed ? res : res.path;
|
|
38
|
+
},
|
|
39
|
+
/** @internal +1 whenever the walkable mesh changes (rebuild, unload, obstacle sync): async pathfinding re-sends the mesh to its workers and redoes answers computed on an older one */
|
|
40
|
+
revision: 0,
|
|
41
|
+
/** @internal the current mesh as bytes, imported by the pathfinding worker */
|
|
42
|
+
exportMesh() { return lib.exportNavMesh(api.navMesh); },
|
|
43
|
+
/** @internal how far (m) a path may end short of its goal and still count as arriving, for a character of this radius */
|
|
44
|
+
reach(radius) { return (radius ?? agentRadius) + 0.6; },
|
|
45
|
+
/** Unload the navmesh (frees native objects); afterwards characters.findPath falls back to A* */
|
|
46
|
+
dispose() {
|
|
47
|
+
api.ready = false; api.version++; api.revision++;
|
|
48
|
+
if (api.helper) e.remove(api.helper); // the debug mesh is built by us: remove it completely
|
|
49
|
+
api.crowd?.destroy(); api.tileCache?.destroy(); api.navMesh?.destroy(); api.query?.destroy();
|
|
50
|
+
Object.assign(api, { navMesh: null, query: null, tileCache: null, crowd: null, helper: null });
|
|
51
|
+
obstacles.clear(); version = -1;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
function search(from, to, o) {
|
|
56
|
+
if (!api.ready) return { status: 'notReady', path: null };
|
|
57
|
+
const [sx, sz] = xzOf(from), [gx, gz] = xzOf(to);
|
|
58
|
+
return navSearch(api.query, { x: sx, y: e.surface(sx, sz), z: sz }, { x: gx, y: e.surface(gx, gz), z: gz }, api.reach(o.radius));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Walkable triangle mesh of the ground (world coordinates), merged into one */
|
|
62
|
+
function geometry() {
|
|
63
|
+
const G = e.ground;
|
|
64
|
+
if (G.navGeometry) return G.navGeometry();
|
|
65
|
+
const list = G.staticTrimeshes?.() || [];
|
|
66
|
+
let n = 0, m = 0;
|
|
67
|
+
for (const t of list) { n += t.positions.length; m += t.indices.length; }
|
|
68
|
+
const positions = new Float32Array(n), indices = new Uint32Array(m);
|
|
69
|
+
let pn = 0, im = 0;
|
|
70
|
+
for (const t of list) { positions.set(t.positions, pn); for (let i = 0; i < t.indices.length; i++) indices[im + i] = t.indices[i] + pn / 3; pn += t.positions.length; im += t.indices.length; }
|
|
71
|
+
return { positions, indices };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function build() {
|
|
75
|
+
pending = false;
|
|
76
|
+
if (api.ready) api.dispose();
|
|
77
|
+
const { positions, indices } = geometry();
|
|
78
|
+
if (!indices.length) return;
|
|
79
|
+
const ch = cellSize / 2;
|
|
80
|
+
const r = gen.generateTileCache(positions, indices, {
|
|
81
|
+
cs: cellSize, ch, tileSize, expectedLayersPerTile: 4, maxObstacles: 1024,
|
|
82
|
+
walkableSlopeAngle: maxSlope, walkableHeight: Math.ceil(agentHeight / ch), walkableClimb: Math.ceil(maxClimb / ch), walkableRadius: Math.ceil(agentRadius / cellSize),
|
|
83
|
+
});
|
|
84
|
+
if (!r.success) { console.warn('nav: ' + r.error); return; }
|
|
85
|
+
api.tileCache = r.tileCache; api.navMesh = r.navMesh;
|
|
86
|
+
api.query = new lib.NavMeshQuery(r.navMesh);
|
|
87
|
+
if (crowd) api.crowd = new lib.Crowd(r.navMesh, { maxAgents: 128, maxAgentRadius: agentRadius * 2 });
|
|
88
|
+
api.ready = true; api.version++; api.revision++;
|
|
89
|
+
syncObstacles();
|
|
90
|
+
if (debug) {
|
|
91
|
+
const three = await import('@recast-navigation/three');
|
|
92
|
+
api.helper = new three.NavMeshHelper(r.navMesh);
|
|
93
|
+
api.helper.position.y = 0.05; e.add(api.helper, { owner: 'engine:nav' });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// collision-module colliders → TileCache obstacles (only on add/remove), then rebuild affected tiles
|
|
98
|
+
function syncObstacles() {
|
|
99
|
+
const ph = e.collision;
|
|
100
|
+
if (!api.ready || !ph || ph.version === version) return;
|
|
101
|
+
version = ph.version;
|
|
102
|
+
const seen = new Set(), tc = api.tileCache;
|
|
103
|
+
ph.forEachStatic((type, o) => {
|
|
104
|
+
seen.add(o);
|
|
105
|
+
if (obstacles.has(o)) return;
|
|
106
|
+
const y = (o.y ?? e.surface(o.x, o.z)) - maxClimb, h = (o.h ?? 6) + maxClimb; // extend a bit downward: Recast ignores a bottom face lying exactly on the ground
|
|
107
|
+
const list = [];
|
|
108
|
+
if (type === 'box') {
|
|
109
|
+
// one obstacle affects at most 8 tiles: cut big boxes into pieces no larger than half a tile (along the box's own orientation)
|
|
110
|
+
const hw = o.hw + agentRadius, hd = o.hd + agentRadius, piece = tileSize * cellSize / 2; // add the agent radius so agents don't hug walls
|
|
111
|
+
const nx = Math.ceil(hw * 2 / piece), nz = Math.ceil(hd * 2 / piece), c = Math.cos(o.rot), sn = Math.sin(o.rot);
|
|
112
|
+
for (let i = 0; i < nx; i++) for (let j = 0; j < nz; j++) {
|
|
113
|
+
const lx = -hw + (i + 0.5) * hw * 2 / nx, lz = -hd + (j + 0.5) * hd * 2 / nz;
|
|
114
|
+
const res = tc.addBoxObstacle({ x: o.x + lx * c + lz * sn, y: y + h / 2, z: o.z - lx * sn + lz * c }, { x: hw / nx, y: h / 2, z: hd / nz }, -o.rot); // Detour's rotation direction is opposite to three's
|
|
115
|
+
if (res.success) list.push(res.obstacle);
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
const res = tc.addCylinderObstacle({ x: o.x, y, z: o.z }, o.r + agentRadius, h);
|
|
119
|
+
if (res.success) list.push(res.obstacle);
|
|
120
|
+
}
|
|
121
|
+
obstacles.set(o, list);
|
|
122
|
+
queued += list.length;
|
|
123
|
+
if (queued >= 48) flush();
|
|
124
|
+
});
|
|
125
|
+
for (const [o, list] of obstacles) if (!seen.has(o)) { for (const ob of list) { tc.removeObstacle(ob); if (++queued >= 48) flush(); } obstacles.delete(o); }
|
|
126
|
+
flush();
|
|
127
|
+
api.revision++;
|
|
128
|
+
api.helper?.update();
|
|
129
|
+
}
|
|
130
|
+
// at most 64 obstacle requests per batch: process once a batch has built up, until all tiles are rebuilt
|
|
131
|
+
let queued = 0;
|
|
132
|
+
function flush() { queued = 0; for (let i = 0; i < 256 && !api.tileCache.update(api.navMesh).upToDate; i++); }
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
name: 'nav', params: NAV_PARAMS, options: opts, label: 'Navmesh',
|
|
136
|
+
install(engine) {
|
|
137
|
+
e = engine;
|
|
138
|
+
const mv = e.movement;
|
|
139
|
+
({ agentRadius = mv.radius, agentHeight = mv.height, maxClimb = mv.stepHeight, maxSlope = mv.maxSlope } = opts);
|
|
140
|
+
api.agent = { radius: agentRadius, height: agentHeight, maxClimb, maxSlope };
|
|
141
|
+
e.events.on('level:loaded', () => { if (lib) build(); else pending = true; });
|
|
142
|
+
return api;
|
|
143
|
+
},
|
|
144
|
+
async init() {
|
|
145
|
+
[lib, gen] = await Promise.all([import('@recast-navigation/core'), import('@recast-navigation/generators')]);
|
|
146
|
+
await lib.init();
|
|
147
|
+
if (pending) await build();
|
|
148
|
+
},
|
|
149
|
+
isReady() { return !!lib && !pending; },
|
|
150
|
+
fixed(dt) {
|
|
151
|
+
syncObstacles();
|
|
152
|
+
api.crowd?.update(dt);
|
|
153
|
+
},
|
|
154
|
+
describe() { return { ready: api.ready, version: api.version, obstacles: obstacles.size }; },
|
|
155
|
+
};
|
|
156
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Navmesh path query: the one Recast search in the engine. engine.nav.findPath calls it with the live query,
|
|
2
|
+
// the pathfinding worker (characters/pathJob.js) with a query on an imported copy of the same mesh. Worker-safe: no imports.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {any} query a Recast NavMeshQuery
|
|
6
|
+
* @param {{ x: number, y: number, z: number }} s start (y = ground height, looked up by the caller: the worker has no ground)
|
|
7
|
+
* @param {{ x: number, y: number, z: number }} g goal
|
|
8
|
+
* @param {number} reach how far the path's end may stop short of the goal and still count as reaching it (m)
|
|
9
|
+
* @returns {{ status: 'ok' | 'noPath', path: { x: number, z: number }[] | null }}
|
|
10
|
+
*/
|
|
11
|
+
export function navSearch(query, s, g, reach) {
|
|
12
|
+
const r = query.computePath(s, g, { halfExtents: { x: 2, y: 4, z: 2 } });
|
|
13
|
+
if (!r.success || !r.path.length) return { status: 'noPath', path: null };
|
|
14
|
+
const end = r.path[r.path.length - 1];
|
|
15
|
+
if (Math.hypot(end.x - g.x, end.z - g.z) > reach) return { status: 'noPath', path: null }; // end point unreachable
|
|
16
|
+
return { status: 'ok', path: r.path.map(p => ({ x: p.x, z: p.z })) };
|
|
17
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Client side of net(): a read-only mirror of the host. It never changes shared state itself:
|
|
2
|
+
// - ops.run of a net: 'peer' op checks can locally (instant feedback, e.g. grey out "buy" without gold), sends { name, args, seq } and returns
|
|
3
|
+
// { ok: true, pending: true, seq }; other ops are refused here ('notAllowed') except net: 'local' ones, which run on this machine
|
|
4
|
+
// (ops they call are routed the same way, so a local "quick buy" button still asks the host to buy).
|
|
5
|
+
// - snapshots / diffs from the host are queued and applied in the next fixed step (inside ops.allow, the only window the client guard opens).
|
|
6
|
+
// - the own avatar is driven locally and its position sent to the host every few steps; other avatars glide between updates (plain interpolation;
|
|
7
|
+
// prediction / rollback is a later step).
|
|
8
|
+
// - world.simulate does not run here: the host simulates and the result arrives as diffs (two simulations would fight).
|
|
9
|
+
import { applyPart, idMap } from './sync.js';
|
|
10
|
+
import { failure } from '../../core/opsFail.js';
|
|
11
|
+
|
|
12
|
+
const STEP = 1 / 60;
|
|
13
|
+
const ASK_EVERY = 30; // steps between snapshot requests while out of step
|
|
14
|
+
|
|
15
|
+
/** @internal */
|
|
16
|
+
export class NetClient {
|
|
17
|
+
constructor(e, t, o) {
|
|
18
|
+
this.e = e; this.o = o;
|
|
19
|
+
this.actor = null; // this client's actor id on the host (e.g. 'guest-1'); here it is called 'player'
|
|
20
|
+
this.n = -1; this.synced = false; this.since = 0;
|
|
21
|
+
this.inbox = []; this.pending = new Map(); this.seq = 0;
|
|
22
|
+
this.glides = new Map(); this.every = o.syncEvery; this.lastPos = '';
|
|
23
|
+
this.map = idMap(null);
|
|
24
|
+
this.stats = { in: 0, out: 0, diffs: 0, snapshots: 0, gaps: 0 };
|
|
25
|
+
this.connected = false;
|
|
26
|
+
e.ops._client = true;
|
|
27
|
+
e.ops._route = (d, args, opts) => this.route(d, args, opts);
|
|
28
|
+
if (e.world) e.world._s.fixed = () => {}; // the host simulates; see the top of the file
|
|
29
|
+
this.connect(t);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** (Re)attach a transport, e.g. after a dropped connection: the host answers with a full snapshot */
|
|
33
|
+
connect(t) {
|
|
34
|
+
for (const s of this.subs || []) s.remove();
|
|
35
|
+
this.t?.close?.();
|
|
36
|
+
this.t = t; this.connected = false; this.synced = false; this.n = -1; this.inbox.length = 0;
|
|
37
|
+
this.subs = [
|
|
38
|
+
t.on('open', () => { this.connected = true; this.since = this.e.tick; this.e.events.emit('net:open', {}); }),
|
|
39
|
+
t.on('close', () => { this.connected = false; this.synced = false; this.e.events.emit('net:close', {}); }),
|
|
40
|
+
t.on('message', m => { if (m && typeof m === 'object') { this.stats.in++; this.inbox.push(m); } }),
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
send(msg) { this.stats.out++; this.t.send(msg); }
|
|
44
|
+
|
|
45
|
+
route(d, args, opts) {
|
|
46
|
+
if (d.net === 'local') return null;
|
|
47
|
+
if (d.net !== 'peer') return failure('notAllowed');
|
|
48
|
+
if (!this.synced) return failure('offline');
|
|
49
|
+
const c = this.e.ops.can(d.name, args, opts);
|
|
50
|
+
if (!c.ok) return c;
|
|
51
|
+
const seq = ++this.seq, out = {};
|
|
52
|
+
for (const k in args) out[k] = typeof args[k] === 'string' ? this.map.toHost(args[k]) : args[k]; // 'player' here means this client's actor there
|
|
53
|
+
this.pending.set(seq, d.name);
|
|
54
|
+
this.send({ t: 'op', seq, name: d.name, args: out });
|
|
55
|
+
return { ok: true, pending: true, seq };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fixed() {
|
|
59
|
+
const e = this.e;
|
|
60
|
+
if (this.inbox.length) {
|
|
61
|
+
const box = this.inbox.splice(0);
|
|
62
|
+
e.ops.allow(() => { for (const m of box) this.handle(m); });
|
|
63
|
+
}
|
|
64
|
+
if (this.synced && e.tick % this.o.posEvery === 0) this.sendPosition();
|
|
65
|
+
}
|
|
66
|
+
handle(m) {
|
|
67
|
+
const e = this.e;
|
|
68
|
+
if (m.t === 's') {
|
|
69
|
+
this.actor = m.you; this.map = idMap(m.you); this.every = m.every ?? this.every;
|
|
70
|
+
this.glides.clear();
|
|
71
|
+
this.apply(m.parts, true);
|
|
72
|
+
this.n = m.n; this.synced = true; this.stats.snapshots++;
|
|
73
|
+
e.events.emit('net:sync', { tick: m.tick, full: true });
|
|
74
|
+
} else if (m.t === 'd') {
|
|
75
|
+
if (!this.synced || m.n <= this.n) return;
|
|
76
|
+
if (m.n !== this.n + 1) return this.gap();
|
|
77
|
+
this.apply(m.parts, false);
|
|
78
|
+
this.n = m.n; this.stats.diffs++;
|
|
79
|
+
e.events.emit('net:sync', { tick: m.tick, full: false });
|
|
80
|
+
} else if (m.t === 'k') {
|
|
81
|
+
if (this.synced && m.n > this.n) this.gap();
|
|
82
|
+
} else if (m.t === 'r') {
|
|
83
|
+
this.pending.delete(m.seq);
|
|
84
|
+
e.events.emit('net:result', { seq: m.seq, name: m.name, ok: m.ok, code: m.code, value: m.value, tick: m.tick });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** a diff was lost: stop applying diffs and ask for a snapshot (frame asks again if that is lost too) */
|
|
88
|
+
gap() {
|
|
89
|
+
this.synced = false; this.stats.gaps++;
|
|
90
|
+
this.since = this.e.tick;
|
|
91
|
+
this.send({ t: 'resync' });
|
|
92
|
+
}
|
|
93
|
+
apply(parts, full) {
|
|
94
|
+
const o = this.o, opts = {
|
|
95
|
+
toLocal: this.map.toLocal, actorFields: o.actorFields, avatarFields: o.avatarFields,
|
|
96
|
+
smooth: rec => o.interpolate.includes(rec.kind),
|
|
97
|
+
glide: (id, to) => this.glides.set(id, { from: { ...this.e.world.get(id).position }, to: { ...to }, t: 0, dur: this.every * STEP }),
|
|
98
|
+
};
|
|
99
|
+
for (const key in parts) {
|
|
100
|
+
if (key !== 'world' && key !== 'vars' && key !== 'containers' && !this.e.saves.providers.has(key)) continue;
|
|
101
|
+
if (key === 'world' && !this.e.world) continue;
|
|
102
|
+
if (key === 'containers' && !this.e.inventory) continue;
|
|
103
|
+
applyPart(this.e, key, parts[key], full, opts);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
sendPosition() {
|
|
107
|
+
const rec = this.e.world?.get('player');
|
|
108
|
+
if (!rec) return;
|
|
109
|
+
const p = rec.position, f = {};
|
|
110
|
+
for (const k of this.o.avatarFields) if (k in rec) f[k] = rec[k];
|
|
111
|
+
const key = `${p.x},${p.y},${p.z},${JSON.stringify(f)}`;
|
|
112
|
+
if (key === this.lastPos && this.e.tick % 60) return; // unchanged: resend once a second anyway, in case the last one was lost
|
|
113
|
+
this.lastPos = key;
|
|
114
|
+
this.send({ t: 'p', p: [p.x, p.y, p.z], f });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
frame(dt) {
|
|
118
|
+
// out of step (snapshot or the request for it lost, or the first one never came): ask again
|
|
119
|
+
if (!this.synced && this.connected && this.e.tick - this.since >= ASK_EVERY) { this.since = this.e.tick; this.send({ t: 'resync' }); }
|
|
120
|
+
if (!this.glides.size) return;
|
|
121
|
+
const W = this.e.world;
|
|
122
|
+
for (const [id, g] of this.glides) {
|
|
123
|
+
if (!W.get(id)) { this.glides.delete(id); continue; }
|
|
124
|
+
g.t += dt;
|
|
125
|
+
const k = Math.min(1, g.t / g.dur);
|
|
126
|
+
W.move(id, { x: g.from.x + (g.to.x - g.from.x) * k, y: g.from.y + (g.to.y - g.from.y) * k, z: g.from.z + (g.to.z - g.from.z) * k });
|
|
127
|
+
if (k >= 1) this.glides.delete(id);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
dispose() {
|
|
131
|
+
for (const s of this.subs) s.remove();
|
|
132
|
+
this.t.close?.();
|
|
133
|
+
const e = this.e;
|
|
134
|
+
e.ops._client = false; e.ops._route = null;
|
|
135
|
+
if (e.world) delete e.world._s.fixed;
|
|
136
|
+
}
|
|
137
|
+
}
|