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,89 @@
|
|
|
1
|
+
// Container table: implementation of inventory.container() / transfer() and the containers save part.
|
|
2
|
+
import { checkOptions, DEV } from '../../core/validate.js';
|
|
3
|
+
import { Container } from './Container.js';
|
|
4
|
+
|
|
5
|
+
/** Container table: implementation of inventory.container() / transfer(); the player inventory is the one with id 'player'. @internal */
|
|
6
|
+
export class Containers {
|
|
7
|
+
constructor(inv, bag) { this.inv = inv; this.bag = bag; this.map = new Map([[bag.id, bag]]); this.renames = new Map(); this.loaded = new WeakSet(); }
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* content/renames.js: a container saved under an old authored id moves to the new id (null = drop it). Remembered, so a save loaded later
|
|
11
|
+
* is renamed the same way. Skipped when the new id already holds goods of its own (they cannot merge; the old one stays an orphan).
|
|
12
|
+
* @param {Record<string, string | null>} [map] new entries; without it, applies the remembered ones
|
|
13
|
+
*/
|
|
14
|
+
rename(map) {
|
|
15
|
+
for (const [a, b] of Object.entries(map || {})) this.renames.set(a, b);
|
|
16
|
+
for (const [a, b] of this.renames) {
|
|
17
|
+
const c = this.map.get(a);
|
|
18
|
+
if (!c || c === this.bag || (b != null && this.map.get(b)?._entries.length)) continue;
|
|
19
|
+
this.map.delete(a);
|
|
20
|
+
if (b != null) { c.id = b; this.map.set(b, c); }
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get(id, opts) {
|
|
25
|
+
if (typeof id !== 'string' || !id) throw new Error(`inventory.container(id): id must be a non-empty string (e.g. the chest's id in world), got ${JSON.stringify(id)}`);
|
|
26
|
+
checkOptions(`inventory.container('${id}')`, opts, ['capacity', 'maxWeight', 'items']);
|
|
27
|
+
for (const k of ['capacity', 'maxWeight']) if (opts?.[k] != null && !(opts[k] > 0)) throw new Error(`inventory.container('${id}'): ${k} must be a positive number (Infinity = no limit), got ${opts[k]}`);
|
|
28
|
+
let c = this.map.get(id);
|
|
29
|
+
if (!c) {
|
|
30
|
+
this.map.set(id, c = new Container(this.inv, id, opts));
|
|
31
|
+
// place goods only on first creation; seeding is setup, so the ops guard lets it through
|
|
32
|
+
if (opts?.items) { this.inv.engine.ops.allow(() => { for (const [item, n] of Object.entries(opts.items)) c.add(item, n, { silent: true }); }); c.seeded = true; }
|
|
33
|
+
} else { // container created first by a load, game code sets the limits afterwards; if it already exists, goods are not placed again
|
|
34
|
+
if (opts?.capacity != null) c.capacity = opts.capacity;
|
|
35
|
+
if (opts?.maxWeight != null) c.maxWeight = opts.maxWeight;
|
|
36
|
+
}
|
|
37
|
+
return c;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Takes a container or container id; an id never created throws (a typo must not silently create a new one). */
|
|
41
|
+
find(x, role) {
|
|
42
|
+
if (x instanceof Container) return x;
|
|
43
|
+
const c = typeof x === 'string' && this.map.get(x);
|
|
44
|
+
if (!c) throw new Error(`inventory.transfer: ${role} container '${x}' does not exist: create it first with inventory.container('${x}', { capacity }) (existing: ${[...this.map.keys()].join(', ')})`);
|
|
45
|
+
return c;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The entry with this key in any container, or null */
|
|
49
|
+
entry(key) { for (const c of this.map.values()) { const e = c._find(key); if (e) return e; } return null; }
|
|
50
|
+
|
|
51
|
+
transfer(from, to, x, n = 1) {
|
|
52
|
+
DEV && this.inv.engine.ops.guard('inventory.transfer');
|
|
53
|
+
const a = this.find(from, 'from'), b = this.find(to, 'to');
|
|
54
|
+
if (!(n > 0)) throw new Error(`inventory.transfer: count must be a positive number, got ${n}`);
|
|
55
|
+
if (a === b) return 0;
|
|
56
|
+
let moved = 0;
|
|
57
|
+
for (const e of a._of(x)) { // each entry keeps its data, and its key when it lands as a new entry
|
|
58
|
+
const k = Math.min(n - moved, e.count, b._room(e.id, e.data));
|
|
59
|
+
if (k <= 0) continue;
|
|
60
|
+
a._take(e, k);
|
|
61
|
+
b._put(e.id, k, e.data, e.key, false);
|
|
62
|
+
if ((moved += k) >= n) break;
|
|
63
|
+
}
|
|
64
|
+
return moved;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
remove(id) {
|
|
68
|
+
if (id === this.bag.id) throw new Error(`The player inventory ('${id}') cannot be removed`);
|
|
69
|
+
return this.map.delete(id);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Save: non-player containers that have contents or ever had starting contents placed (an emptied one is still recorded, or a load would treat it as new and place them again) */
|
|
73
|
+
save() {
|
|
74
|
+
const out = {};
|
|
75
|
+
for (const [id, c] of this.map) if (c !== this.bag && (c._entries.length || c.seeded)) out[id] = c._save();
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
/** A load follows the save: containers missing from the save are cleared; but ones that had starting contents placed (save older than the content, first time seen) keep their current goods */
|
|
79
|
+
load(d) {
|
|
80
|
+
if (d && this.renames.size) d = Object.fromEntries(Object.entries(d).flatMap(([id, v]) => { // content/renames.js first (null = dropped)
|
|
81
|
+
const b = this.renames.has(id) ? this.renames.get(id) : id;
|
|
82
|
+
return b == null || (b !== id && b in d) ? (b == null ? [] : [[id, v]]) : [[b, v]];
|
|
83
|
+
}));
|
|
84
|
+
for (const c of this.map.values()) if (c !== this.bag && !(c.seeded && !this.loaded.has(c) && !(c.id in (d || {})))) c._entries = []; // kept: starting goods of content newer than this save; not a container that only came from an earlier load / save (newGame must empty it)
|
|
85
|
+
for (const [id, list] of Object.entries(d || {})) { const c = this.get(id); c._load(list); c.seeded = true; this.loaded.add(c); }
|
|
86
|
+
// entry keys are numbered across all containers: never hand out a key a loaded container already holds
|
|
87
|
+
for (const c of this.map.values()) for (const e of c._entries) { const n = Number(e.key.split('#')[1]) || 0; if (n > this.inv._seq) this.inv._seq = n; }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// Dialogue module: data-driven dialogue (nodes + choices + conditions + callbacks), typewriter effect, confirm key advances, number keys pick choices.
|
|
2
|
+
// Typing runs on game time (frame dt accumulation), so engine.step advances it deterministically. A normal dialogue locks game input and joins the UI focus stack
|
|
3
|
+
// (Esc won't open the settings menu; the key press that opened it does not advance). say(text, { lock: false }) is a line spoken while the player keeps playing:
|
|
4
|
+
// no lock, keys stay with the game, the line moves on by itself holdSeconds after it is typed.
|
|
5
|
+
// Drawing is the UI part 'dialogue' (default: dialogueBox.js); a game replaces it or turns it off with engine.ui.set('dialogue', ...) (recipes/26-ui-parts.md).
|
|
6
|
+
//
|
|
7
|
+
// dialogue.define('老渔夫', {
|
|
8
|
+
// start: { text: '今天的鱼不太咬钩。', next: 'ask', data: { portrait: 'fisher', mood: 'tired' } }, // data: the game's own fields, handed to the UI part and 'dialogue:line'
|
|
9
|
+
// ask: { text: '你是来拍照的吧?', choices: [
|
|
10
|
+
// { text: '对,想拍日落', next: 'sunset', if: (state, engine) => true },
|
|
11
|
+
// { text: '随便走走', next: 'end' } ] },
|
|
12
|
+
// sunset:{ text: '去东边的山脊。', do: (state, engine) => engine.vars.setFlag('knowsSunset') },
|
|
13
|
+
// });
|
|
14
|
+
// dialogue.start('老渔夫');
|
|
15
|
+
// The speaker defaults to the script name; set speaker: '另一个人' on a node to switch speakers.
|
|
16
|
+
// Advancing and picking a choice are the ops dialogue.advance and dialogue.choose { index } (recipes/23-ops.md): keys, clicks, the dev bridge and tests
|
|
17
|
+
// all run them, so a choice's do (which may change gameplay state) always runs inside a logged op.
|
|
18
|
+
import { uiFocus } from '../../ui/UI.js';
|
|
19
|
+
import { checkOptions, warnMissing } from '../../core/validate.js';
|
|
20
|
+
import { readParams, keysSchema } from '../../core/params.js';
|
|
21
|
+
import { dialogueBox } from './dialogueBox.js';
|
|
22
|
+
// 'data' is the one slot for game-specific fields: other unknown keys stay typos (a misspelled next would silently end the dialogue)
|
|
23
|
+
const NODE_KEYS = ['text', 'next', 'choices', 'do', 'speaker', 'data'], CHOICE_KEYS = ['text', 'next', 'if', 'do', 'data'];
|
|
24
|
+
const NONE = Object.freeze([]);
|
|
25
|
+
const OWN_FIELDS = ' (put your own fields in data: { ... })';
|
|
26
|
+
|
|
27
|
+
export class Dialogue {
|
|
28
|
+
constructor(engine, { charsPerSecond = 40, advanceActions, choiceKeys = true, holdSeconds = 2 } = {}) {
|
|
29
|
+
this.engine = engine;
|
|
30
|
+
this.scripts = new Map();
|
|
31
|
+
this.state = { get flags() { return engine.vars.flags; } }; // story flags = engine.vars.flags (autosaved)
|
|
32
|
+
this.cps = charsPerSecond;
|
|
33
|
+
// default: confirm, plus interact when the game bound one (interact is not a core key any more, so it is only used if it exists)
|
|
34
|
+
/** @internal */ this._advDefault = advanceActions === undefined;
|
|
35
|
+
this.advanceActions = advanceActions ?? ['confirm', 'interact'];
|
|
36
|
+
this.holdSeconds = holdSeconds;
|
|
37
|
+
this.choiceKeys = choiceKeys;
|
|
38
|
+
this.active = null;
|
|
39
|
+
// choice keys are read only while a locking dialogue runs (game input is locked then), so 1-9 stay free for weapons and hotbars the rest of the time
|
|
40
|
+
if (choiceKeys) for (let i = 1; i <= 9; i++) engine.input.define('choice' + i, ['Digit' + i, 'Numpad' + i], 'dialogue.choice');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
define(speaker, nodes) {
|
|
44
|
+
for (const [id, n] of Object.entries(nodes || {})) {
|
|
45
|
+
checkOptions(`dialogue '${speaker}' ${id}${OWN_FIELDS}`, n, NODE_KEYS);
|
|
46
|
+
for (const c of n?.choices || []) checkOptions(`dialogue '${speaker}' ${id} choices${OWN_FIELDS}`, c, CHOICE_KEYS);
|
|
47
|
+
}
|
|
48
|
+
this.scripts.set(speaker, nodes);
|
|
49
|
+
}
|
|
50
|
+
/** Name of the script currently speaking (null when no dialogue). */
|
|
51
|
+
get speaking() { return this.active?.speaker ?? null; }
|
|
52
|
+
/**
|
|
53
|
+
* Say a one-off line (no define needed); the returned Promise resolves when it ends.
|
|
54
|
+
* say('天快黑了。', { speaker: '老渔夫' }), or pass a node table: say({ start: {...}, ... }). lock: false = spoken while the player keeps playing. The returned .id is the name of this one-off dialogue.
|
|
55
|
+
* Ending the playthrough (engine.session) drops the line and leaves the Promise pending: code awaiting it belonged to the old playthrough and never runs.
|
|
56
|
+
*/
|
|
57
|
+
say(text, { speaker = '', node = 'start', lock = true, onCancel = null } = {}) {
|
|
58
|
+
const id = '__say_' + (this._sayId = (this._sayId || 0) + 1);
|
|
59
|
+
this.define(id, typeof text === 'object' ? text : { start: { speaker, text } });
|
|
60
|
+
const p = new Promise(res => this.start(id, node, () => { this.scripts.delete(id); res(); }, { lock, onCancel }));
|
|
61
|
+
p.id = id;
|
|
62
|
+
return p;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** onCancel: what the back key (Esc) does while this locking dialogue is the top of the UI focus stack (default: nothing, Esc is swallowed) */
|
|
66
|
+
start(speaker, nodeId = 'start', onEnd = null, { lock = true, onCancel = null } = {}) {
|
|
67
|
+
const nodes = this.scripts.get(speaker);
|
|
68
|
+
// wrong name: warn with the fix and still call onEnd (so an NPC waiting on it and the say() Promise don't hang)
|
|
69
|
+
if (!nodes) { warnMissing('dialogue', speaker, [...this.scripts.keys()].filter(k => !k.startsWith('__say_'))); onEnd?.(); return; }
|
|
70
|
+
if (!lock && this.active?.lock) { onEnd?.(); return; } // a passing line never cuts into a conversation the player is reading
|
|
71
|
+
if (this.active) this.end(); // end a running dialogue normally first (runs its onEnd so NPCs don't hang)
|
|
72
|
+
this.active = { speaker, nodes, onEnd, lock, focus: lock ? uiFocus(this.engine).push('dialogue', { cancel: onCancel }) : null, ctx: lock ? this.engine.input.push('dialogue') : null }; // locking: join the UI focus stack, Esc is swallowed
|
|
73
|
+
this.part = this.engine.ui.get('dialogue');
|
|
74
|
+
this.part?.show();
|
|
75
|
+
this.engine.events.emit('dialogue:start', speaker);
|
|
76
|
+
this._show(nodeId);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
end() {
|
|
80
|
+
const a = this.active;
|
|
81
|
+
this.active = null;
|
|
82
|
+
this.part?.hide();
|
|
83
|
+
a?.focus?.remove();
|
|
84
|
+
a?.ctx?.remove();
|
|
85
|
+
a?.onEnd?.();
|
|
86
|
+
if (a) this.engine.events.emit('dialogue:end', a.speaker);
|
|
87
|
+
}
|
|
88
|
+
/** @internal session:end: close the dialogue without its onEnd (that callback belongs to the old playthrough: an NPC's busy flag, a say() Promise) */
|
|
89
|
+
_cancel() {
|
|
90
|
+
const a = this.active;
|
|
91
|
+
if (!a) return;
|
|
92
|
+
a.onEnd = null;
|
|
93
|
+
if (a.speaker.startsWith('__say_')) this.scripts.delete(a.speaker);
|
|
94
|
+
this.end();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
_show(id) {
|
|
98
|
+
const a = this.active, node = id && a.nodes[id];
|
|
99
|
+
if (!node && id && id !== 'end') warnMissing('node', id, Object.keys(a.nodes)); // next points at a node that doesn't exist
|
|
100
|
+
if (!node || id === 'end') { this.end(); return; }
|
|
101
|
+
a.node = node; a.id = id;
|
|
102
|
+
const text = typeof node.text === 'function' ? node.text(this.state, this.engine) : node.text;
|
|
103
|
+
node.do?.(this.state, this.engine); // read the line before running effects, so it doesn't see flags just changed
|
|
104
|
+
if (this.active !== a) return; // do ended the dialogue or started another
|
|
105
|
+
a.who = node.speaker || a.speaker;
|
|
106
|
+
this.full = String(text ?? ''); this.shown = 0; this.typing = true; this._opts = null; this._hold = 0;
|
|
107
|
+
this.engine.events.emit('dialogue:line', { script: a.speaker, node: id, speaker: a.who, text: this.full, data: node.data ?? {} });
|
|
108
|
+
if (!this.full || !(this.cps > 0)) this._finishTyping(); // charsPerSecond 0 = whole line at once
|
|
109
|
+
else this._draw();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
_finishTyping() {
|
|
113
|
+
this.typing = false;
|
|
114
|
+
this.shown = this.full.length;
|
|
115
|
+
const opts = (this.active.node.choices || []).filter(c => !c.if || c.if(this.state, this.engine));
|
|
116
|
+
this._opts = opts.length ? opts : null;
|
|
117
|
+
const label = c => typeof c.text === 'function' ? c.text(this.state, this.engine) : c.text;
|
|
118
|
+
const keys = this.choiceKeys && this.active.lock;
|
|
119
|
+
// key: the first bound key only ('1', not '1/Numpad1'), so a rebound choice key shows its new name
|
|
120
|
+
this._choices = opts.map((c, i) => ({ text: label(c), key: keys && i < 9 ? this.engine.input.label('choice' + (i + 1)).split('/')[0] : null, data: c.data ?? {} }));
|
|
121
|
+
this._draw();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** @internal hand the current line to the UI part: plain data plus advance() / choose(i), which run the ops */
|
|
125
|
+
_draw() {
|
|
126
|
+
const a = this.active, e = this.engine, P = { source: 'player' };
|
|
127
|
+
const hint = !this.typing && !this._opts && a.lock ? e.t('dialogue.continue', { key: e.input.label(this.advanceActions[0]) }) : '';
|
|
128
|
+
this.part?.update({
|
|
129
|
+
script: a.speaker, node: a.id, speaker: a.who, text: this.full.slice(0, Math.floor(this.shown)), full: this.full, typing: this.typing,
|
|
130
|
+
choices: this.typing ? NONE : this._choices, hint, data: a.node.data ?? {}, lock: a.lock,
|
|
131
|
+
advance: () => e.ops.run('dialogue.advance', {}, P), choose: index => e.ops.run('dialogue.choose', { index }, P),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** @internal run through the op dialogue.advance */
|
|
136
|
+
_advance() {
|
|
137
|
+
if (!this.active) return;
|
|
138
|
+
if (this.typing) { this._finishTyping(); return; }
|
|
139
|
+
if (this._opts) return;
|
|
140
|
+
this._show(this.active.node.next || 'end');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Choices shown for the current line (typing or not). @internal */
|
|
144
|
+
_visible() { return this._opts ?? (this.active?.node.choices || []).filter(c => !c.if || c.if(this.state, this.engine)); }
|
|
145
|
+
/** @internal run through the op dialogue.choose */
|
|
146
|
+
_choose(i) {
|
|
147
|
+
if (!this.active) return;
|
|
148
|
+
if (this.typing) this._finishTyping(); // mid-typing: finish the sentence (choices appear) before picking
|
|
149
|
+
if (!this._opts?.[i]) return;
|
|
150
|
+
const c = this._opts[i], a = this.active;
|
|
151
|
+
c.do?.(this.state, this.engine);
|
|
152
|
+
if (this.active === a) this._show(c.next || 'end'); // do ended the dialogue or started another: its next belongs to the old one
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Per frame: advance typing, read keys (a locking dialogue locks game input, hence pressedRaw). */
|
|
156
|
+
frame(dt) {
|
|
157
|
+
const a = this.active;
|
|
158
|
+
if (!a) return;
|
|
159
|
+
const ops = this.engine.ops;
|
|
160
|
+
if (this.typing) {
|
|
161
|
+
const was = Math.floor(this.shown);
|
|
162
|
+
this.shown = Math.min(this.full.length, this.shown + dt * this.cps);
|
|
163
|
+
if (this.shown >= this.full.length) this._finishTyping();
|
|
164
|
+
else if (Math.floor(this.shown) !== was) this._draw(); // redraw only when another character shows
|
|
165
|
+
}
|
|
166
|
+
if (!a.lock) { // keys belong to the game: the line moves on by itself (choices wait for a click or the op)
|
|
167
|
+
if (!this.typing && !this._opts && (this._hold += dt) >= this.holdSeconds) ops.run('dialogue.advance', {}, { source: 'game' });
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (a.focus.fresh) return; // just opened this frame: the key press that opened it (E) does not advance
|
|
171
|
+
const input = this.engine.input, P = { source: 'player' };
|
|
172
|
+
if (this.advanceActions.some(x => (!this._advDefault || input.actions.has(x)) && input.pressedRaw(x))) ops.run('dialogue.advance', {}, P);
|
|
173
|
+
if (this.choiceKeys) for (let i = 1; i <= 9; i++) if (input.pressedRaw('choice' + i) && this.active && this._visible()[i - 1]) ops.run('dialogue.choose', { index: i - 1 }, P);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const STRINGS = {
|
|
178
|
+
'zh-CN': { 'dialogue.continue': '{key} 继续 ▸', 'dialogue.choice': '选择选项' },
|
|
179
|
+
en: { 'dialogue.continue': '{key} to continue ▸', 'dialogue.choice': 'Choose option' },
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const DIALOGUE_PARAMS = keysSchema(['charsPerSecond', 'advanceActions', 'choiceKeys', 'holdSeconds']);
|
|
183
|
+
|
|
184
|
+
/** Dialogue module. Installs engine.dialogue; story flags engine.dialogue.state.flags are engine.vars.flags. Declares the UI part 'dialogue'. */
|
|
185
|
+
export function dialogue(opts = {}) {
|
|
186
|
+
readParams('dialogue', DIALOGUE_PARAMS, opts);
|
|
187
|
+
let d = null;
|
|
188
|
+
return {
|
|
189
|
+
name: 'dialogue', params: DIALOGUE_PARAMS, options: opts, label: 'Dialogue', stage: 'gameplay', after: ['interactions'],
|
|
190
|
+
install(e) {
|
|
191
|
+
e.i18n.add(STRINGS);
|
|
192
|
+
d = new Dialogue(e, opts);
|
|
193
|
+
e.events.on('session:end', () => d._cancel()); // per-playthrough state (recipes/14-custom-module.md "Per-playthrough state")
|
|
194
|
+
e.ui.define('dialogue', dialogueBox);
|
|
195
|
+
e.ops.define('dialogue.advance', {
|
|
196
|
+
desc: 'Finish typing the line, or go on to the next one',
|
|
197
|
+
net: 'local', // in co-op each player reads their own dialogue box; ops it runs (give, start a quest) still go to the host
|
|
198
|
+
can: () => d.active ? true : 'noDialogue',
|
|
199
|
+
run: () => { d._advance(); },
|
|
200
|
+
offers: () => d.active && !d._opts ? [{}] : [],
|
|
201
|
+
});
|
|
202
|
+
e.ops.define('dialogue.choose', {
|
|
203
|
+
desc: 'Pick a visible dialogue choice (0-based)', args: { index: 'int' },
|
|
204
|
+
net: 'local',
|
|
205
|
+
can: (e, { index }) => !d.active ? 'noDialogue' : d._visible()[index] ? true : 'noChoice',
|
|
206
|
+
run: (e, { index }) => { d._choose(index); },
|
|
207
|
+
offers: () => d.active ? d._visible().map((c, index) => ({ index })) : [],
|
|
208
|
+
});
|
|
209
|
+
return d;
|
|
210
|
+
},
|
|
211
|
+
frame(dt) { d.frame(dt); },
|
|
212
|
+
describe(e, ctx) {
|
|
213
|
+
const a = d.active;
|
|
214
|
+
if (!a) return;
|
|
215
|
+
const label = c => (typeof c.text === 'function' ? c.text(d.state, e) : c.text);
|
|
216
|
+
ctx.out.ui.dialogue = { speaker: a.who, text: d.full, typing: d.typing, choices: (d._opts || []).map(label), lock: a.lock };
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
// Interactions module: a prompt appears near an object ("E · 和老渔夫说话"), and the interact key fires a callback (dialogue, pickup, fishing, chopping...).
|
|
2
|
+
//
|
|
3
|
+
// engine.interactions.add(npc, { label: '和老渔夫说话', onUse: () => engine.dialogue.start('老渔夫') });
|
|
4
|
+
// Using one is the op interaction.use { id?, action? } (recipes/23-ops.md): the interact key (in fixed), a click on the prompt, the dev bridge and tests all
|
|
5
|
+
// run it,
|
|
6
|
+
// so every use is logged and checked by the same can (exists, enabled, in range, input not locked). Without id it uses the one the actor would pick now.
|
|
7
|
+
// Who interacts is engine.actor (core/actor.js), measured with engine.actorPosition(): the player by default, a vehicle or squad member when the game says so.
|
|
8
|
+
// How the one in front of you is chosen is the pick option: 'nearest' | 'facing' | 'aim' | a function (see interactions()). Each action is picked on its own,
|
|
9
|
+
// so one object can carry "Open" and "Lock" as two interactions with different action names (each bound to the game's own key).
|
|
10
|
+
// The prompt is the UI part 'prompt' (default interactionPrompt.js, CSS class .threedog-prompt): replace it or turn it off with engine.ui.set('prompt', ...);
|
|
11
|
+
// prompt height style.ui.promptHeight (meters)
|
|
12
|
+
import * as THREE from 'three';
|
|
13
|
+
import { worldToScreen } from '../../ui/UI.js';
|
|
14
|
+
import { promptPart } from './interactionPrompt.js';
|
|
15
|
+
import { checkOptions } from '../../core/validate.js';
|
|
16
|
+
import { readParams } from '../../core/params.js';
|
|
17
|
+
import { codedError } from '../../core/errorHints.js';
|
|
18
|
+
import { vec3 } from '../../core/math.js';
|
|
19
|
+
|
|
20
|
+
const PICKS = ['nearest', 'facing', 'aim'];
|
|
21
|
+
// 'facing': within 60° either side of where the actor faces (wide enough for a gamepad stick, narrow enough that doors behind you stay dark)
|
|
22
|
+
const FACING_COS = 0.5;
|
|
23
|
+
const AIM_WIDTH = 0.75; // 'aim': meters the object may sit off the screen-center ray (an object is a vertical segment from its base up to its prompt height)
|
|
24
|
+
const V = new THREE.Vector3(), W = new THREE.Vector3(), O = new THREE.Vector3(), D = new THREE.Vector3();
|
|
25
|
+
|
|
26
|
+
const SETUP_FIX = "e.g. engine.use(interactions({ ...presets.interactions.nearest })) (import { presets } from 'threedog'), or interactions({ pick: 'nearest' | 'facing' | 'aim' | fn, keys: ['KeyE'] })";
|
|
27
|
+
|
|
28
|
+
export class Interactions {
|
|
29
|
+
constructor(engine, { action = 'interact', promptHeight, radius = 2.5, pick, keys } = {}) {
|
|
30
|
+
// No default pick and no default key: which object counts as "in front of you" and which key uses it are the game's choices (genericity rule:
|
|
31
|
+
// core carries no preset). presets.interactions.* bundle common pairs, so the fix is one spread.
|
|
32
|
+
if (pick === undefined) throw codedError('interaction-setup', `interactions(): no pick given; ${SETUP_FIX}`);
|
|
33
|
+
if (typeof pick !== 'function' && !PICKS.includes(pick)) throw new Error(`interactions({ pick }): '${pick}' is not a pick mode. Use ${PICKS.map(p => `'${p}'`).join(' | ')} or a function (candidates, ctx) => candidate`);
|
|
34
|
+
if (keys) engine.input.define(action, keys, 'input.interact');
|
|
35
|
+
else if (!engine.input.actions.has(action)) throw codedError('interaction-setup', `interactions(): the '${action}' action has no keys; pass keys: ['KeyE'] (or bind it: createEngine({ bindings: { ${action}: ['KeyE'] } })); ${SETUP_FIX}`);
|
|
36
|
+
this.engine = engine;
|
|
37
|
+
this.action = action;
|
|
38
|
+
this.radius = radius;
|
|
39
|
+
this.promptHeight = promptHeight ?? engine.style.ui?.promptHeight ?? 2.4;
|
|
40
|
+
/**
|
|
41
|
+
* How the interaction in front of the actor is chosen; can be swapped at runtime (first person: 'aim', top-down: 'nearest') @type {'nearest' | 'facing' |
|
|
42
|
+
* 'aim' | Function}
|
|
43
|
+
*/
|
|
44
|
+
this.pickMode = pick;
|
|
45
|
+
this.items = [];
|
|
46
|
+
/** The interaction the prompt shows (best over all actions), null if none */
|
|
47
|
+
this.current = null;
|
|
48
|
+
/** One best interaction per action this frame ([{ action, id, label }]) for a prompt that lists several keys */
|
|
49
|
+
this.options = [];
|
|
50
|
+
this._actions = new Set();
|
|
51
|
+
this._seq = 0;
|
|
52
|
+
/** @internal non-zero during one fixed() / update() pass: each item's world position is computed once per pass, not once per action */ this._pass = 0;
|
|
53
|
+
this._passes = 0;
|
|
54
|
+
engine.input.touchButton(action, 'input.interact');
|
|
55
|
+
engine.ui.define('prompt', promptPart);
|
|
56
|
+
/** @internal clicking the prompt: the same op as the interact key */
|
|
57
|
+
this._use = () => { if (this.current) engine.ops.run('interaction.use', { id: this.current.id }, { source: 'player' }); };
|
|
58
|
+
this._shown = false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Add an interaction. target is a three.js object (followed) or a fixed position (Vector3 / [x, y, z] / { x, y, z }). label is text or a function returning
|
|
63
|
+
* text
|
|
64
|
+
* (put state-dependent text here: () => locked ? 'Unlock' : 'Open'); enabled returning false hides the prompt entirely; action defaults to 'interact';
|
|
65
|
+
* radius defaults to interactions({ radius }) (2.5 m). Add two with different actions to give one object two verbs. Returns a handle { remove() }.
|
|
66
|
+
* @example engine.interactions.add(door, { label: 'Open', onUse: open });
|
|
67
|
+
* engine.interactions.add(door, { label: 'Lock', action: 'lock', enabled: () => hasKey, onUse: lock }); // a second verb: define the 'lock' action's key
|
|
68
|
+
* first
|
|
69
|
+
* @example // onUse may return a result; interaction.use hands it back as value: const r = engine.ops.run('interaction.use'); if (r.ok && r.value?.ok ===
|
|
70
|
+
* false) say(r.value.reason) // 'full' for a pickup
|
|
71
|
+
*/
|
|
72
|
+
add(target, opts = {}) {
|
|
73
|
+
checkOptions('interactions.add()', opts, ['id', 'label', 'radius', 'action', 'onUse', 'enabled', 'height']);
|
|
74
|
+
const { label = '', radius = this.radius, action = this.action, onUse, enabled = () => true, height } = opts;
|
|
75
|
+
// stable id for the interaction.use op: given, else the world record being built (pickup, NPC, entity), else a running number
|
|
76
|
+
let id = opts.id ?? this.engine.world?.buildingId ?? 'interaction' + (++this._seq);
|
|
77
|
+
if (this.get(id)) id += '#' + (++this._seq);
|
|
78
|
+
let object = target;
|
|
79
|
+
if (!target?.isObject3D) { object = new THREE.Object3D(); vec3(target, object.position); }
|
|
80
|
+
// a fixed position never moves: its world position is known now; an object is measured once per pass (_at)
|
|
81
|
+
const it = { id, object, label, radius, action, onUse, enabled, height, _p: new THREE.Vector3(), _at: target?.isObject3D ? -1 : Infinity };
|
|
82
|
+
if (!target?.isObject3D) it._p.copy(object.position);
|
|
83
|
+
this.items.push(it);
|
|
84
|
+
return { remove: () => { this.items = this.items.filter(x => x !== it); if (this.current === it) this.current = null; } };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The interaction with this id (null if none). @param {string} id */
|
|
88
|
+
get(id) { return this.items.find(x => x.id === id) ?? null; }
|
|
89
|
+
|
|
90
|
+
/** The interaction actor would use now for action (any action when omitted), by the pick mode; null if none in range. @internal */
|
|
91
|
+
pick(actor = this.engine.actor, action) {
|
|
92
|
+
const e = this.engine, p = e.actorPosition(actor);
|
|
93
|
+
if (!p) return null;
|
|
94
|
+
const pos = W.set(p.x, p.y ?? 0, p.z), mode = this.pickMode;
|
|
95
|
+
if (typeof mode === 'function') return this._custom(mode, actor, pos, action);
|
|
96
|
+
// 'aim' follows the camera, which only the local user looks through; anyone else aims where they face
|
|
97
|
+
const aim = mode === 'aim' && actor === e.actor;
|
|
98
|
+
// the actor's body, for its facing
|
|
99
|
+
const obj = mode === 'nearest' || aim ? null : actor === 'player' && e.player ? e.player.root : e.world?.view(actor) ?? null;
|
|
100
|
+
if (obj) { obj.getWorldDirection(D); D.y = 0; D.normalize(); }
|
|
101
|
+
if (aim) { e.camera.getWorldPosition(O); e.camera.getWorldDirection(D); }
|
|
102
|
+
let best = null, bestS = Infinity;
|
|
103
|
+
for (const it of this.items) {
|
|
104
|
+
if (action !== undefined && it.action !== action) continue;
|
|
105
|
+
const q = this._where(it), d = q.distanceTo(pos);
|
|
106
|
+
if (d >= it.radius || !it.enabled()) continue;
|
|
107
|
+
let s = d;
|
|
108
|
+
if (aim) s = this._offRay(q, it.height ?? this.promptHeight);
|
|
109
|
+
else if (obj && d > 0.5) { // standing on top of it counts as facing it
|
|
110
|
+
const c = ((q.x - pos.x) * D.x + (q.z - pos.z) * D.z) / Math.hypot(q.x - pos.x, q.z - pos.z);
|
|
111
|
+
if (c < FACING_COS) continue;
|
|
112
|
+
s = d / it.radius + (1 - c); // mostly "straight ahead", then "closer"
|
|
113
|
+
}
|
|
114
|
+
if (s < bestS) { best = it; bestS = s; }
|
|
115
|
+
}
|
|
116
|
+
return best;
|
|
117
|
+
}
|
|
118
|
+
/** @internal An item's world position: cached for the current pass, live outside one (ops run from a test or the dev bridge) */
|
|
119
|
+
_where(it) {
|
|
120
|
+
if (it._at === Infinity) return it._p;
|
|
121
|
+
if (this._pass && it._at === this._pass) return it._p;
|
|
122
|
+
it._at = this._pass;
|
|
123
|
+
return it.object.getWorldPosition(it._p);
|
|
124
|
+
}
|
|
125
|
+
/** @internal 'aim': meters from the camera ray (O, D) to the object's vertical segment; Infinity when behind the camera or too far off */
|
|
126
|
+
_offRay(q, h) {
|
|
127
|
+
const t = (q.x - O.x) * D.x + (q.y - O.y) * D.y + (q.z - O.z) * D.z;
|
|
128
|
+
if (t <= 0) return Infinity;
|
|
129
|
+
const rx = O.x + D.x * t, ry = O.y + D.y * t, rz = O.z + D.z * t;
|
|
130
|
+
const off = Math.hypot(rx - q.x, ry - Math.min(Math.max(ry, q.y), q.y + h), rz - q.z);
|
|
131
|
+
return off <= AIM_WIDTH ? off : Infinity;
|
|
132
|
+
}
|
|
133
|
+
/** @internal A game's pick function gets the enabled ones in range, nearest first */
|
|
134
|
+
_custom(fn, actor, pos, action) {
|
|
135
|
+
const list = [];
|
|
136
|
+
for (const it of this.items) {
|
|
137
|
+
if (action !== undefined && it.action !== action) continue;
|
|
138
|
+
const distance = this._where(it).distanceTo(pos);
|
|
139
|
+
if (distance < it.radius && it.enabled()) list.push({ id: it.id, object: it.object, action: it.action, label: it.label, distance });
|
|
140
|
+
}
|
|
141
|
+
if (!list.length) return null;
|
|
142
|
+
list.sort((a, b) => a.distance - b.distance);
|
|
143
|
+
const c = fn(list, { engine: this.engine, actor, position: pos, action });
|
|
144
|
+
return c ? this.get(c.id) : null;
|
|
145
|
+
}
|
|
146
|
+
/** Fixed step: each action's key fires that action's pick (input snapshot, frame-rate independent; picked on the spot, not from last frame). @internal */
|
|
147
|
+
fixed() {
|
|
148
|
+
const inp = this.engine.input;
|
|
149
|
+
if (!inp.enabled) return;
|
|
150
|
+
this._pass = ++this._passes;
|
|
151
|
+
this._actions.clear();
|
|
152
|
+
for (const it of this.items) this._actions.add(it.action);
|
|
153
|
+
for (const a of this._actions) {
|
|
154
|
+
if (!inp.pressed(a)) continue;
|
|
155
|
+
const b = this.pick(undefined, a);
|
|
156
|
+
if (b) this.engine.ops.run('interaction.use', { id: b.id }, { source: 'player' });
|
|
157
|
+
}
|
|
158
|
+
this._pass = 0;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Per frame: pick per action for options, and the prompt follows the best one. @internal */
|
|
162
|
+
update() {
|
|
163
|
+
this._pass = ++this._passes;
|
|
164
|
+
try { this._update(); } finally { this._pass = 0; }
|
|
165
|
+
}
|
|
166
|
+
/** @internal */
|
|
167
|
+
_update() {
|
|
168
|
+
this.options.length = 0;
|
|
169
|
+
this._actions.clear();
|
|
170
|
+
for (const it of this.items) this._actions.add(it.action);
|
|
171
|
+
const best = this.current = this._actions.size > 1 ? this._perAction() : this.pick();
|
|
172
|
+
if (best && this._actions.size === 1) this.options.push({ action: best.action, id: best.id, label: labelOf(best) });
|
|
173
|
+
const input = this.engine.input;
|
|
174
|
+
const part = this.engine.ui.get('prompt');
|
|
175
|
+
if (!best || !input.enabled || !part) { if (this._shown) part?.hide(); this._shown = false; return; }
|
|
176
|
+
const label = labelOf(best), key = input.label(best.action);
|
|
177
|
+
best.object.getWorldPosition(V); V.y += best.height ?? this.promptHeight;
|
|
178
|
+
const s = worldToScreen(this.engine, V);
|
|
179
|
+
part.update({ id: best.id, action: best.action, key, label, text: `${key} · ${label}`, x: s.x, y: s.y, onScreen: s.onScreen, use: this._use, options: this.options });
|
|
180
|
+
this._shown = true;
|
|
181
|
+
}
|
|
182
|
+
/** @internal Several actions: fill options, return the default action's pick (else the first found) for the prompt */
|
|
183
|
+
_perAction() {
|
|
184
|
+
let main = null, first = null;
|
|
185
|
+
for (const a of this._actions) {
|
|
186
|
+
const b = this.pick(undefined, a);
|
|
187
|
+
if (!b) continue;
|
|
188
|
+
this.options.push({ action: a, id: b.id, label: labelOf(b) });
|
|
189
|
+
if (a === this.action) main = b;
|
|
190
|
+
first ??= b;
|
|
191
|
+
}
|
|
192
|
+
return main ?? first;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const labelOf = it => typeof it.label === 'function' ? it.label() : it.label;
|
|
197
|
+
|
|
198
|
+
const INTERACTION_PARAMS = {
|
|
199
|
+
action: { type: 'string', default: 'interact', desc: 'input action that uses the interaction' },
|
|
200
|
+
pick: { type: 'any', optional: true, desc: "which one counts as in front of you: 'nearest' | 'facing' | 'aim' | a function (required; checked with a fix hint on install)" },
|
|
201
|
+
keys: { type: 'array', optional: true, desc: "keys that use it, e.g. ['KeyE'] (required unless createEngine({ bindings }) binds the action)" },
|
|
202
|
+
radius: { type: 'number', default: 2.5, min: 0, desc: 'reach in meters (add() can set its own)' },
|
|
203
|
+
promptHeight: { type: 'number', optional: true, desc: 'prompt height above the target (meters)' },
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Interactions module. Installs engine.interactions.
|
|
208
|
+
* pick and keys are required (the engine picks neither; presets.interactions.* bundle them). radius: default reach of add() in meters (2.5); pick: how the
|
|
209
|
+
* interaction in front of the actor is chosen —
|
|
210
|
+
* 'nearest' (top-down and third person), 'facing' (only what the actor faces, within 60°: souls-likes, the door behind you stays dark),
|
|
211
|
+
* 'aim' (closest to the screen-center ray: first person and shooters), or a function (candidates, ctx) => one of candidates | null,
|
|
212
|
+
* where candidates are the enabled ones in range ({ id, object, action, label, distance }, nearest first) and ctx is { engine, actor, position, action }.
|
|
213
|
+
* keys: the action's keys (default action 'interact'); leave it out only when the game already bound the action (createEngine({ bindings })).
|
|
214
|
+
* @example engine.use(interactions({ ...presets.interactions.nearest })); // E, the nearest one
|
|
215
|
+
* engine.use(interactions({ pick: 'aim', keys: ['KeyF'], radius: 2 })); // first person
|
|
216
|
+
* engine.use(interactions({ pick: (list) => list.find(c => c.id.startsWith('npc')) ?? list[0], keys: ['KeyE'] })); // people before things
|
|
217
|
+
* @param {{ action?: string, promptHeight?: number, radius?: number, pick: 'nearest' | 'facing' | 'aim' | ((candidates: any[], ctx: any) => any), keys?:
|
|
218
|
+
* string[] }} opts
|
|
219
|
+
*/
|
|
220
|
+
export function interactions(opts) {
|
|
221
|
+
const o = readParams('interactions', INTERACTION_PARAMS, opts ?? {});
|
|
222
|
+
let i = null;
|
|
223
|
+
return { name: 'interactions', label: 'Interactions', params: INTERACTION_PARAMS, options: o, noSkip: 'input and prompt state only', stage: 'gameplay',
|
|
224
|
+
install(e) { i = new Interactions(e, opts); defineUse(e, i); return i; },
|
|
225
|
+
fixed() { i.fixed(); }, frame() { i.update(); },
|
|
226
|
+
// interactions are "weak" info: when an NPC, pickup or entity already reported the same object, only add "interactable: xxx"
|
|
227
|
+
describe(e, ctx) {
|
|
228
|
+
for (const it of i.items) if (it.enabled()) { const use = labelOf(it); ctx.near(it.object, { name: use, kind: 'interactable', use }, true); }
|
|
229
|
+
if (i.current) return { current: labelOf(i.current) };
|
|
230
|
+
} };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** The engine op interaction.use { id?, action? }: what the interact key, the prompt click, the dev bridge and tests all run */
|
|
234
|
+
function defineUse(e, i) {
|
|
235
|
+
const find = (id, action, ctx) => id !== undefined ? i.get(id) : i.pick(ctx.actor, action);
|
|
236
|
+
e.ops.define('interaction.use', {
|
|
237
|
+
desc: 'Use an interaction (talk, pick up, open...); without id, the one the actor would pick now (optionally for one action)',
|
|
238
|
+
args: { id: 'string?', action: 'string?' },
|
|
239
|
+
net: 'local', // the prompt and reach are this player's; game ops inside onUse are routed to the host (net: 'peer' ones) or refused
|
|
240
|
+
can(e, { id, action }, ctx) {
|
|
241
|
+
const it = find(id, action, ctx);
|
|
242
|
+
if (!it) return id === undefined ? 'nothingNear' : 'unknown';
|
|
243
|
+
if (!it.enabled()) return 'disabled';
|
|
244
|
+
if (ctx.actor === e.actor && !e.input.enabled) return 'inputLocked';
|
|
245
|
+
const p = e.actorPosition(ctx.actor);
|
|
246
|
+
return p && it.object.getWorldPosition(V).distanceTo(W.set(p.x, p.y ?? 0, p.z)) >= it.radius ? 'tooFar' : true;
|
|
247
|
+
},
|
|
248
|
+
// onUse's return value is the op's value: pickups give { ok:false, reason:'full' } when the bag is full
|
|
249
|
+
run(e, { id, action }, ctx) { const it = find(id, action, ctx); return it.onUse?.(it.object); },
|
|
250
|
+
offers(e, ctx) { const it = find(undefined, undefined, ctx); return it ? [{ id: it.id }] : []; },
|
|
251
|
+
reasons: { unknown: 'No interaction with this id', nothingNear: 'No interaction in range', disabled: 'Its enabled() is false', tooFar: 'Actor is outside its radius', inputLocked: 'A window or dialogue locks input' },
|
|
252
|
+
});
|
|
253
|
+
}
|