wallhammer-web 0.0.0-stage → 0.4.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.
Files changed (408) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -2
  3. package/dist/base/data.d.ts +67 -0
  4. package/dist/base/data.js +10 -0
  5. package/dist/base/destructible.d.ts +167 -0
  6. package/dist/base/destructible.js +47 -0
  7. package/dist/base/env-slot.d.ts +101 -0
  8. package/dist/base/env-slot.js +8 -0
  9. package/dist/base/frames.d.ts +138 -0
  10. package/dist/base/frames.js +459 -0
  11. package/dist/base/index.d.ts +21 -0
  12. package/dist/base/index.js +11 -0
  13. package/dist/base/mat34.d.ts +46 -0
  14. package/dist/base/mat34.js +173 -0
  15. package/dist/base/material.d.ts +87 -0
  16. package/dist/base/material.js +6 -0
  17. package/dist/base/math.d.ts +27 -0
  18. package/dist/base/math.js +60 -0
  19. package/dist/base/progress.d.ts +38 -0
  20. package/dist/base/progress.js +81 -0
  21. package/dist/base/rig.d.ts +177 -0
  22. package/dist/base/rig.js +97 -0
  23. package/dist/base/trig.d.ts +29 -0
  24. package/dist/base/trig.js +169 -0
  25. package/dist/base/up.d.ts +26 -0
  26. package/dist/base/up.js +40 -0
  27. package/dist/base/view.d.ts +128 -0
  28. package/dist/base/view.js +136 -0
  29. package/dist/camera/camera.d.ts +99 -0
  30. package/dist/camera/camera.js +228 -0
  31. package/dist/camera/follow.d.ts +120 -0
  32. package/dist/camera/follow.js +320 -0
  33. package/dist/camera/free.d.ts +153 -0
  34. package/dist/camera/free.js +359 -0
  35. package/dist/camera/index.d.ts +10 -0
  36. package/dist/camera/index.js +6 -0
  37. package/dist/camera/orbit.d.ts +93 -0
  38. package/dist/camera/orbit.js +261 -0
  39. package/dist/camera/path.d.ts +48 -0
  40. package/dist/camera/path.js +179 -0
  41. package/dist/core/ballistic.d.ts +66 -0
  42. package/dist/core/ballistic.js +104 -0
  43. package/dist/core/bodies.d.ts +77 -0
  44. package/dist/core/bodies.js +125 -0
  45. package/dist/core/clock.d.ts +37 -0
  46. package/dist/core/clock.js +87 -0
  47. package/dist/core/cover-fade.d.ts +14 -0
  48. package/dist/core/cover-fade.js +38 -0
  49. package/dist/core/index.d.ts +13 -0
  50. package/dist/core/index.js +8 -0
  51. package/dist/core/kepler.d.ts +74 -0
  52. package/dist/core/kepler.js +108 -0
  53. package/dist/core/level-fade.d.ts +66 -0
  54. package/dist/core/level-fade.js +123 -0
  55. package/dist/core/palette.d.ts +15 -0
  56. package/dist/core/palette.js +47 -0
  57. package/dist/env/atmosphere.d.ts +129 -0
  58. package/dist/env/atmosphere.js +707 -0
  59. package/dist/env/cloud-field.d.ts +44 -0
  60. package/dist/env/cloud-field.js +111 -0
  61. package/dist/env/environment.d.ts +217 -0
  62. package/dist/env/environment.js +441 -0
  63. package/dist/env/frame-block.d.ts +29 -0
  64. package/dist/env/frame-block.js +152 -0
  65. package/dist/env/glsl.d.ts +35 -0
  66. package/dist/env/glsl.js +246 -0
  67. package/dist/env/index.d.ts +13 -0
  68. package/dist/env/index.js +9 -0
  69. package/dist/env/sky.d.ts +48 -0
  70. package/dist/env/sky.js +227 -0
  71. package/dist/env/sun.d.ts +31 -0
  72. package/dist/env/sun.js +42 -0
  73. package/dist/gen-arch/build.d.ts +17 -0
  74. package/dist/gen-arch/build.js +414 -0
  75. package/dist/gen-arch/damage/damaged.d.ts +44 -0
  76. package/dist/gen-arch/damage/damaged.js +1119 -0
  77. package/dist/gen-arch/damage/fall-material.d.ts +10 -0
  78. package/dist/gen-arch/damage/fall-material.js +47 -0
  79. package/dist/gen-arch/damage/fall.d.ts +78 -0
  80. package/dist/gen-arch/damage/fall.js +797 -0
  81. package/dist/gen-arch/damage/rubble.d.ts +22 -0
  82. package/dist/gen-arch/damage/rubble.js +188 -0
  83. package/dist/gen-arch/index.d.ts +25 -0
  84. package/dist/gen-arch/index.js +16 -0
  85. package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
  86. package/dist/gen-arch/interior/interior-plan.js +615 -0
  87. package/dist/gen-arch/interior/interior.d.ts +97 -0
  88. package/dist/gen-arch/interior/interior.js +966 -0
  89. package/dist/gen-arch/structure/building-shader.d.ts +25 -0
  90. package/dist/gen-arch/structure/building-shader.js +946 -0
  91. package/dist/gen-arch/structure/buildings.d.ts +248 -0
  92. package/dist/gen-arch/structure/buildings.js +294 -0
  93. package/dist/gen-arch/structure/dims.d.ts +77 -0
  94. package/dist/gen-arch/structure/dims.js +39 -0
  95. package/dist/gen-arch/structure/distant.d.ts +62 -0
  96. package/dist/gen-arch/structure/distant.js +316 -0
  97. package/dist/gen-arch/structure/env-slots.d.ts +18 -0
  98. package/dist/gen-arch/structure/env-slots.js +47 -0
  99. package/dist/gen-arch/structure/facade.d.ts +193 -0
  100. package/dist/gen-arch/structure/facade.js +159 -0
  101. package/dist/gen-arch/structure/parts.d.ts +31 -0
  102. package/dist/gen-arch/structure/parts.js +69 -0
  103. package/dist/gen-arch/structure/roof.d.ts +76 -0
  104. package/dist/gen-arch/structure/roof.js +568 -0
  105. package/dist/gen-arch/structure/structure-material.d.ts +23 -0
  106. package/dist/gen-arch/structure/structure-material.js +55 -0
  107. package/dist/gen-arch/structure/writer.d.ts +94 -0
  108. package/dist/gen-arch/structure/writer.js +195 -0
  109. package/dist/gen-core/cell.d.ts +138 -0
  110. package/dist/gen-core/cell.js +150 -0
  111. package/dist/gen-core/cube-sphere.d.ts +57 -0
  112. package/dist/gen-core/cube-sphere.js +159 -0
  113. package/dist/gen-core/field.d.ts +55 -0
  114. package/dist/gen-core/field.js +154 -0
  115. package/dist/gen-core/geometry.d.ts +31 -0
  116. package/dist/gen-core/geometry.js +231 -0
  117. package/dist/gen-core/hash.d.ts +18 -0
  118. package/dist/gen-core/hash.js +58 -0
  119. package/dist/gen-core/height-field.d.ts +33 -0
  120. package/dist/gen-core/height-field.js +137 -0
  121. package/dist/gen-core/index.d.ts +22 -0
  122. package/dist/gen-core/index.js +17 -0
  123. package/dist/gen-core/instance-cells.d.ts +37 -0
  124. package/dist/gen-core/instance-cells.js +72 -0
  125. package/dist/gen-core/near-scatter.d.ts +56 -0
  126. package/dist/gen-core/near-scatter.js +120 -0
  127. package/dist/gen-core/noise.d.ts +23 -0
  128. package/dist/gen-core/noise.js +72 -0
  129. package/dist/gen-core/occupancy.d.ts +21 -0
  130. package/dist/gen-core/occupancy.js +104 -0
  131. package/dist/gen-core/sphere-patch.d.ts +138 -0
  132. package/dist/gen-core/sphere-patch.js +544 -0
  133. package/dist/gen-core/strip-surface.d.ts +34 -0
  134. package/dist/gen-core/strip-surface.js +246 -0
  135. package/dist/gen-core/strip.d.ts +102 -0
  136. package/dist/gen-core/strip.js +817 -0
  137. package/dist/gen-ground/env-slots.d.ts +48 -0
  138. package/dist/gen-ground/env-slots.js +78 -0
  139. package/dist/gen-ground/ground.d.ts +78 -0
  140. package/dist/gen-ground/ground.js +208 -0
  141. package/dist/gen-ground/index.d.ts +9 -0
  142. package/dist/gen-ground/index.js +7 -0
  143. package/dist/gen-ground/markings.d.ts +46 -0
  144. package/dist/gen-ground/markings.js +124 -0
  145. package/dist/gen-ground/materials.d.ts +18 -0
  146. package/dist/gen-ground/materials.js +22 -0
  147. package/dist/gen-ground/terrain-shader.d.ts +5 -0
  148. package/dist/gen-ground/terrain-shader.js +151 -0
  149. package/dist/gen-mesh/bare-shapes.d.ts +194 -0
  150. package/dist/gen-mesh/bare-shapes.js +596 -0
  151. package/dist/gen-mesh/bisect-surface.d.ts +47 -0
  152. package/dist/gen-mesh/bisect-surface.js +261 -0
  153. package/dist/gen-mesh/box.d.ts +18 -0
  154. package/dist/gen-mesh/box.js +2 -0
  155. package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
  156. package/dist/gen-mesh/cell-morph-mesh.js +258 -0
  157. package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
  158. package/dist/gen-mesh/cell-surface-mesh.js +140 -0
  159. package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
  160. package/dist/gen-mesh/chunk-mesh.js +112 -0
  161. package/dist/gen-mesh/drape-mesh.d.ts +36 -0
  162. package/dist/gen-mesh/drape-mesh.js +98 -0
  163. package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
  164. package/dist/gen-mesh/height-field-mesh.js +98 -0
  165. package/dist/gen-mesh/index.d.ts +26 -0
  166. package/dist/gen-mesh/index.js +15 -0
  167. package/dist/gen-mesh/line-mesh.d.ts +25 -0
  168. package/dist/gen-mesh/line-mesh.js +62 -0
  169. package/dist/gen-mesh/mesh-builder.d.ts +115 -0
  170. package/dist/gen-mesh/mesh-builder.js +315 -0
  171. package/dist/gen-mesh/packed-mesh.d.ts +53 -0
  172. package/dist/gen-mesh/packed-mesh.js +205 -0
  173. package/dist/gen-mesh/prism-mesh.d.ts +33 -0
  174. package/dist/gen-mesh/prism-mesh.js +176 -0
  175. package/dist/gen-mesh/rig-mesh.d.ts +9 -0
  176. package/dist/gen-mesh/rig-mesh.js +135 -0
  177. package/dist/gen-mesh/strip-mesh.d.ts +45 -0
  178. package/dist/gen-mesh/strip-mesh.js +258 -0
  179. package/dist/gen-plan/drain.d.ts +58 -0
  180. package/dist/gen-plan/drain.js +270 -0
  181. package/dist/gen-plan/grid.d.ts +37 -0
  182. package/dist/gen-plan/grid.js +182 -0
  183. package/dist/gen-plan/horizon.d.ts +50 -0
  184. package/dist/gen-plan/horizon.js +185 -0
  185. package/dist/gen-plan/index.d.ts +15 -0
  186. package/dist/gen-plan/index.js +13 -0
  187. package/dist/gen-plan/line-tiles.d.ts +35 -0
  188. package/dist/gen-plan/line-tiles.js +190 -0
  189. package/dist/gen-plan/lines.d.ts +31 -0
  190. package/dist/gen-plan/lines.js +183 -0
  191. package/dist/gen-plan/plots.d.ts +40 -0
  192. package/dist/gen-plan/plots.js +282 -0
  193. package/dist/gen-plan/route.d.ts +55 -0
  194. package/dist/gen-plan/route.js +297 -0
  195. package/dist/gen-plan/sites.d.ts +32 -0
  196. package/dist/gen-plan/sites.js +209 -0
  197. package/dist/gen-tex/code-stamp.d.ts +31 -0
  198. package/dist/gen-tex/code-stamp.js +137 -0
  199. package/dist/gen-tex/index.d.ts +7 -0
  200. package/dist/gen-tex/index.js +5 -0
  201. package/dist/gen-tex/light-horizon.d.ts +47 -0
  202. package/dist/gen-tex/light-horizon.js +180 -0
  203. package/dist/gen-tex/textures.d.ts +8 -0
  204. package/dist/gen-tex/textures.js +49 -0
  205. package/dist/gen-tex/value-textures.d.ts +46 -0
  206. package/dist/gen-tex/value-textures.js +102 -0
  207. package/dist/gen-vegetation/env-slots.d.ts +20 -0
  208. package/dist/gen-vegetation/env-slots.js +29 -0
  209. package/dist/gen-vegetation/index.d.ts +8 -0
  210. package/dist/gen-vegetation/index.js +7 -0
  211. package/dist/gen-vegetation/materials.d.ts +7 -0
  212. package/dist/gen-vegetation/materials.js +17 -0
  213. package/dist/gen-vegetation/planting.d.ts +170 -0
  214. package/dist/gen-vegetation/planting.js +363 -0
  215. package/dist/gen-vegetation/tree-shader.d.ts +23 -0
  216. package/dist/gen-vegetation/tree-shader.js +229 -0
  217. package/dist/gen-vegetation/trees.d.ts +59 -0
  218. package/dist/gen-vegetation/trees.js +329 -0
  219. package/dist/gpu/device.d.ts +286 -0
  220. package/dist/gpu/device.js +1157 -0
  221. package/dist/gpu/gl.d.ts +134 -0
  222. package/dist/gpu/gl.js +276 -0
  223. package/dist/gpu/index.d.ts +4 -0
  224. package/dist/gpu/index.js +3 -0
  225. package/dist/index.d.ts +11 -0
  226. package/dist/index.js +13 -0
  227. package/dist/motion/graph.d.ts +244 -0
  228. package/dist/motion/graph.js +739 -0
  229. package/dist/motion/index.d.ts +8 -0
  230. package/dist/motion/index.js +8 -0
  231. package/dist/motion/pose-ops.d.ts +30 -0
  232. package/dist/motion/pose-ops.js +232 -0
  233. package/dist/motion/reach.d.ts +45 -0
  234. package/dist/motion/reach.js +264 -0
  235. package/dist/motion/skeleton.d.ts +49 -0
  236. package/dist/motion/skeleton.js +99 -0
  237. package/dist/platform/byte-store.d.ts +21 -0
  238. package/dist/platform/byte-store.js +137 -0
  239. package/dist/platform/canvas.d.ts +29 -0
  240. package/dist/platform/canvas.js +76 -0
  241. package/dist/platform/context-loss.d.ts +8 -0
  242. package/dist/platform/context-loss.js +17 -0
  243. package/dist/platform/free-input.d.ts +51 -0
  244. package/dist/platform/free-input.js +100 -0
  245. package/dist/platform/index.d.ts +18 -0
  246. package/dist/platform/index.js +10 -0
  247. package/dist/platform/input.d.ts +48 -0
  248. package/dist/platform/input.js +117 -0
  249. package/dist/platform/keys.d.ts +12 -0
  250. package/dist/platform/keys.js +29 -0
  251. package/dist/platform/loop.d.ts +41 -0
  252. package/dist/platform/loop.js +84 -0
  253. package/dist/platform/walk-input.d.ts +74 -0
  254. package/dist/platform/walk-input.js +226 -0
  255. package/dist/platform/worker.d.ts +51 -0
  256. package/dist/platform/worker.js +170 -0
  257. package/dist/render/bone-instances.d.ts +24 -0
  258. package/dist/render/bone-instances.js +74 -0
  259. package/dist/render/cells.d.ts +70 -0
  260. package/dist/render/cells.js +144 -0
  261. package/dist/render/glsl.d.ts +66 -0
  262. package/dist/render/glsl.js +173 -0
  263. package/dist/render/index.d.ts +26 -0
  264. package/dist/render/index.js +16 -0
  265. package/dist/render/instance-sets.d.ts +97 -0
  266. package/dist/render/instance-sets.js +241 -0
  267. package/dist/render/near-instances.d.ts +75 -0
  268. package/dist/render/near-instances.js +219 -0
  269. package/dist/render/particles.d.ts +63 -0
  270. package/dist/render/particles.js +215 -0
  271. package/dist/render/pieces.d.ts +57 -0
  272. package/dist/render/pieces.js +151 -0
  273. package/dist/render/point-lights.d.ts +97 -0
  274. package/dist/render/point-lights.js +243 -0
  275. package/dist/render/renderer.d.ts +447 -0
  276. package/dist/render/renderer.js +1100 -0
  277. package/dist/render/rig.d.ts +74 -0
  278. package/dist/render/rig.js +576 -0
  279. package/dist/render/shadow-areas.d.ts +49 -0
  280. package/dist/render/shadow-areas.js +125 -0
  281. package/dist/render/shadow-glsl.d.ts +12 -0
  282. package/dist/render/shadow-glsl.js +146 -0
  283. package/dist/render/transient.d.ts +61 -0
  284. package/dist/render/transient.js +213 -0
  285. package/dist/render/value-glsl.d.ts +17 -0
  286. package/dist/render/value-glsl.js +208 -0
  287. package/dist/render/value-layers.d.ts +180 -0
  288. package/dist/render/value-layers.js +333 -0
  289. package/dist/runtime/atmosphere.d.ts +37 -0
  290. package/dist/runtime/atmosphere.js +304 -0
  291. package/dist/runtime/cloud-shell.d.ts +29 -0
  292. package/dist/runtime/cloud-shell.js +158 -0
  293. package/dist/runtime/engine.d.ts +155 -0
  294. package/dist/runtime/engine.js +305 -0
  295. package/dist/runtime/environment-link.d.ts +22 -0
  296. package/dist/runtime/environment-link.js +49 -0
  297. package/dist/runtime/index.d.ts +13 -0
  298. package/dist/runtime/index.js +12 -0
  299. package/dist/runtime/precipitation-glsl.d.ts +2 -0
  300. package/dist/runtime/precipitation-glsl.js +70 -0
  301. package/dist/runtime/precipitation.d.ts +11 -0
  302. package/dist/runtime/precipitation.js +78 -0
  303. package/dist/runtime/sky.d.ts +30 -0
  304. package/dist/runtime/sky.js +114 -0
  305. package/dist/runtime/streaming.d.ts +245 -0
  306. package/dist/runtime/streaming.js +602 -0
  307. package/dist/sim/agenda.d.ts +167 -0
  308. package/dist/sim/agenda.js +425 -0
  309. package/dist/sim/bytes.d.ts +34 -0
  310. package/dist/sim/bytes.js +125 -0
  311. package/dist/sim/destruction/apply.d.ts +59 -0
  312. package/dist/sim/destruction/apply.js +243 -0
  313. package/dist/sim/destruction/cell-states.d.ts +54 -0
  314. package/dist/sim/destruction/cell-states.js +198 -0
  315. package/dist/sim/destruction/index.d.ts +11 -0
  316. package/dist/sim/destruction/index.js +10 -0
  317. package/dist/sim/destruction/int-geometry.d.ts +17 -0
  318. package/dist/sim/destruction/int-geometry.js +131 -0
  319. package/dist/sim/destruction/pick.d.ts +13 -0
  320. package/dist/sim/destruction/pick.js +59 -0
  321. package/dist/sim/destruction/rubble.d.ts +8 -0
  322. package/dist/sim/destruction/rubble.js +159 -0
  323. package/dist/sim/destruction/state.d.ts +15 -0
  324. package/dist/sim/destruction/state.js +83 -0
  325. package/dist/sim/destruction/structure.d.ts +30 -0
  326. package/dist/sim/destruction/structure.js +139 -0
  327. package/dist/sim/events.d.ts +37 -0
  328. package/dist/sim/events.js +106 -0
  329. package/dist/sim/fixed.d.ts +21 -0
  330. package/dist/sim/fixed.js +64 -0
  331. package/dist/sim/flow-probe.d.ts +58 -0
  332. package/dist/sim/flow-probe.js +158 -0
  333. package/dist/sim/flow.d.ts +257 -0
  334. package/dist/sim/flow.js +967 -0
  335. package/dist/sim/index.d.ts +28 -0
  336. package/dist/sim/index.js +20 -0
  337. package/dist/sim/interlock.d.ts +47 -0
  338. package/dist/sim/interlock.js +252 -0
  339. package/dist/sim/movers.d.ts +228 -0
  340. package/dist/sim/movers.js +1083 -0
  341. package/dist/sim/nav.d.ts +72 -0
  342. package/dist/sim/nav.js +292 -0
  343. package/dist/sim/paths.d.ts +80 -0
  344. package/dist/sim/paths.js +197 -0
  345. package/dist/sim/regions.d.ts +87 -0
  346. package/dist/sim/regions.js +517 -0
  347. package/dist/sim/simulation.d.ts +114 -0
  348. package/dist/sim/simulation.js +291 -0
  349. package/dist/sim/states.d.ts +23 -0
  350. package/dist/sim/states.js +54 -0
  351. package/dist/sim/table.d.ts +49 -0
  352. package/dist/sim/table.js +126 -0
  353. package/dist/sim/zones.d.ts +11 -0
  354. package/dist/sim/zones.js +196 -0
  355. package/dist/tools/collision-lines.d.ts +12 -0
  356. package/dist/tools/collision-lines.js +50 -0
  357. package/dist/tools/data-view-glsl.d.ts +3 -0
  358. package/dist/tools/data-view-glsl.js +63 -0
  359. package/dist/tools/data-view-layers.d.ts +20 -0
  360. package/dist/tools/data-view-layers.js +60 -0
  361. package/dist/tools/debug-view.d.ts +30 -0
  362. package/dist/tools/debug-view.js +4 -0
  363. package/dist/tools/index.d.ts +10 -0
  364. package/dist/tools/index.js +7 -0
  365. package/dist/tools/mesh-checks.d.ts +67 -0
  366. package/dist/tools/mesh-checks.js +188 -0
  367. package/dist/tools/profile.d.ts +85 -0
  368. package/dist/tools/profile.js +173 -0
  369. package/dist/world/cell-cache.d.ts +32 -0
  370. package/dist/world/cell-cache.js +60 -0
  371. package/dist/world/cell-format.d.ts +25 -0
  372. package/dist/world/cell-format.js +232 -0
  373. package/dist/world/collide.d.ts +192 -0
  374. package/dist/world/collide.js +822 -0
  375. package/dist/world/index.d.ts +16 -0
  376. package/dist/world/index.js +12 -0
  377. package/dist/world/mesh-solids.d.ts +48 -0
  378. package/dist/world/mesh-solids.js +169 -0
  379. package/dist/world/pick.d.ts +34 -0
  380. package/dist/world/pick.js +177 -0
  381. package/dist/world/providers.d.ts +66 -0
  382. package/dist/world/providers.js +83 -0
  383. package/dist/world/sphere.d.ts +69 -0
  384. package/dist/world/sphere.js +271 -0
  385. package/dist/world/streaming.d.ts +288 -0
  386. package/dist/world/streaming.js +801 -0
  387. package/dist/world/walker.d.ts +145 -0
  388. package/dist/world/walker.js +502 -0
  389. package/docs/architecture.md +111 -0
  390. package/docs/cameras.md +302 -0
  391. package/docs/concepts.md +123 -0
  392. package/docs/destruction.md +127 -0
  393. package/docs/frames.md +199 -0
  394. package/docs/generators.md +575 -0
  395. package/docs/getting-started.md +123 -0
  396. package/docs/headless.md +50 -0
  397. package/docs/motion.md +160 -0
  398. package/docs/planet.md +152 -0
  399. package/docs/recipes.md +521 -0
  400. package/docs/reference.md +364 -0
  401. package/docs/rendering.md +823 -0
  402. package/docs/rigs.md +203 -0
  403. package/docs/simulation.md +235 -0
  404. package/docs/streaming.md +590 -0
  405. package/docs/system.md +166 -0
  406. package/docs/tools.md +175 -0
  407. package/docs/walking.md +262 -0
  408. package/package.json +131 -3
@@ -0,0 +1,145 @@
1
+ import type { CollisionWorld } from './collide.ts';
2
+ export interface WalkerSetup {
3
+ /** Radius of the body, metres. */
4
+ radius: number;
5
+ /** Height of the body from the feet to the top of the head, metres. */
6
+ height: number;
7
+ /** Speed when walking, m/s. */
8
+ walkSpeed: number;
9
+ /** Speed when running, m/s. */
10
+ runSpeed: number;
11
+ /** Acceleration and braking on the ground, m/s². */
12
+ acceleration: number;
13
+ /** Share of the acceleration available in the air, 0–1. */
14
+ airControl: number;
15
+ /** Upward speed of a jump, m/s; 0 disables jumping. */
16
+ jumpSpeed: number;
17
+ /** Downward acceleration, m/s². */
18
+ gravity: number;
19
+ /** Highest step the body walks up without stopping, metres. */
20
+ maxStepUp: number;
21
+ /** A base surface steeper than this is not climbed and is slid down, degrees. */
22
+ maxSlopeDeg: number;
23
+ /** Fixed steps per second. */
24
+ stepHz: number;
25
+ /** Fastest turn of `yaw` toward the wanted direction, radians per second. */
26
+ turnSpeed: number;
27
+ /**
28
+ * Seconds over which a step up or down is eased out in the drawn position (`renderPosition`), so a body climbing
29
+ * stairs does not jump; the simulated position is not affected. 0 draws the steps as they are.
30
+ */
31
+ stepSmoothing: number;
32
+ /** Most fixed steps one `advance` takes; the rest of a longer pause is dropped (no catching up after a stall). */
33
+ maxStepsPerAdvance: number;
34
+ }
35
+ export declare const DEFAULT_WALKER: Readonly<WalkerSetup>;
36
+ export interface WalkIntent {
37
+ /** Wanted direction in the world, length at most 1 (longer is clamped). */
38
+ moveX: number;
39
+ moveZ: number;
40
+ run: boolean;
41
+ /** True in the step in which a jump is asked. */
42
+ jump: boolean;
43
+ }
44
+ /** Bytes of one saved walker state (`Walker.saveState`). */
45
+ export declare const WALKER_STATE_BYTES: number;
46
+ /** Where a state part writes its bytes (the `bytes` method of sim's `ByteWriter` fits). */
47
+ export interface ByteSink {
48
+ bytes(a: ArrayBufferView): void;
49
+ }
50
+ /** Where a state part reads its bytes from (the `into` method of sim's `ByteReader` fits). */
51
+ export interface ByteSource {
52
+ into(target: ArrayBufferView): void;
53
+ }
54
+ /** A walker as one part of a simulation's state: matches `SimState` of sim (`Simulation.addState`). */
55
+ export interface WalkerSimState {
56
+ readonly name: string;
57
+ save(w: ByteSink): void;
58
+ load(r: ByteSource): void;
59
+ }
60
+ /** A fresh, idle `WalkIntent` (allocate once, reuse). */
61
+ export declare function newWalkIntent(): WalkIntent;
62
+ /**
63
+ * A kinematic body over a `CollisionWorld`. `position` is the feet (x, y, z). `yaw` is the heading with the same
64
+ * convention as a view's yaw: yaw 0 faces −z, and the facing direction is (−sin yaw, −cos yaw) in x, z, so a
65
+ * `FollowRig` with `yawDeg = yaw` in degrees looks the same way. Call `advance(dt, intent)` once per frame (or `step`
66
+ * from a script) and draw at `renderPosition`.
67
+ */
68
+ export declare class Walker {
69
+ readonly setup: WalkerSetup;
70
+ /** Feet position, metres. */
71
+ readonly position: Float64Array<ArrayBuffer>;
72
+ readonly velocity: Float64Array<ArrayBuffer>;
73
+ /** Heading of the last movement, radians (see the class comment for the convention). */
74
+ yaw: number;
75
+ grounded: boolean;
76
+ /** True while standing on a base surface steeper than `maxSlopeDeg` (sliding down it). */
77
+ steep: boolean;
78
+ /** Horizontal distance moved so far, metres (for animating a stride). */
79
+ travelled: number;
80
+ /** Fixed steps taken since construction or the last `teleport`. */
81
+ steps: number;
82
+ private readonly world;
83
+ private readonly dt;
84
+ private readonly tanMax;
85
+ private readonly smoothKeep;
86
+ private readonly prev;
87
+ private prevYaw;
88
+ /** `travelled` before the last step (drawing interpolates between the two). */
89
+ private prevTravelled;
90
+ private acc;
91
+ private pendingJump;
92
+ private stepOffset;
93
+ private prevStepOffset;
94
+ private readonly slid;
95
+ private readonly grad;
96
+ private readonly scratch;
97
+ private readonly stateScratch;
98
+ private readonly stateBytes;
99
+ constructor(world: CollisionWorld, setup?: Partial<WalkerSetup>);
100
+ /** Seconds of one fixed step. */
101
+ get stepSeconds(): number;
102
+ /** Puts the body at a point with no velocity; it settles onto the ground in the next step. */
103
+ teleport(x: number, y: number, z: number, yaw?: number): void;
104
+ /**
105
+ * Advances by real seconds in fixed steps; returns the number of steps taken. Leftover time carries to the next call,
106
+ * so one call of 1/30 s and two of 1/60 s end in the same place. A jump asked while no step is due is kept for the
107
+ * next step; it applies to the first step of the call only.
108
+ */
109
+ advance(dt: number, intent: WalkIntent): number;
110
+ /** One fixed step (for scripts and tests). */
111
+ step(intent: WalkIntent): void;
112
+ /** Share of the way from the last step to the next one, 0–1. */
113
+ get alpha(): number;
114
+ /**
115
+ * Writes the whole state (position, velocity, heading, ground contact, distance, steps, the previous step for
116
+ * drawing, leftover time, a pending jump, the step easing) into `out` (at least `WALKER_STATE_BYTES` bytes from
117
+ * `offset`; default a new array) and returns it. The setup and the collision world are not state.
118
+ */
119
+ saveState(out?: Uint8Array, offset?: number): Uint8Array;
120
+ /** Restores a state written by `saveState` (of a walker with the same setup). */
121
+ loadState(bytes: Uint8Array, offset?: number): void;
122
+ /**
123
+ * This walker as a part of a simulation's state, for `Simulation.addState(walker.simState('walker'))`. Step it from a
124
+ * system of that simulation (`step` once per tick, with `stepHz` equal to the simulation's `hz`) and draw it with
125
+ * the simulation's `alpha`; snapshots, `restore`, `seek` and `rebase` then bring it back with everything else.
126
+ */
127
+ simState(name: string): WalkerSimState;
128
+ /**
129
+ * Position between the last two steps for drawing, written to `out`. A step up or down is eased out over
130
+ * `stepSmoothing` seconds in y. `alpha` is the share of the way to the next step; default the walker's own (from
131
+ * `advance`), a walker stepped by a simulation passes the simulation's `alpha`.
132
+ */
133
+ renderPosition(out: Float64Array | number[], alpha?: number): void;
134
+ /** Heading between the last two steps for drawing, radians. `alpha` as for `renderPosition`. */
135
+ renderYaw(alpha?: number): number;
136
+ /**
137
+ * `travelled` between the last two steps for drawing (a stride kept in step with the drawn position). `alpha` as for
138
+ * `renderPosition`.
139
+ */
140
+ renderTravelled(alpha?: number): number;
141
+ /** Horizontal metres moved in the last step (divide by `stepSeconds` for the speed); 0 after a `teleport`. */
142
+ get lastStepDistance(): number;
143
+ /** Whether the base rises more steeply than the limit on both sides of (x, z) along the uphill unit (ux, uz). */
144
+ private steepBothSides;
145
+ }
@@ -0,0 +1,502 @@
1
+ // Walker: a kinematic body (an upright cylinder) that moves over a collision world in fixed steps. It speeds up and
2
+ // brakes, slides along sides and out of corners, climbs steps up to a height, falls off edges, does not climb a base
3
+ // surface steeper than a limit (and slides down one), jumps from the ground with limited control in the air, stops its
4
+ // head under undersides and turns smoothly toward where it is asked to go. Between steps it gives a position for
5
+ // drawing. Plain Node, no DOM; only + − × ÷ and Math.sqrt in a step (detAtan2 for the heading), so the same inputs
6
+ // give the same path bit for bit.
7
+ //
8
+ // State: everything a step reads or writes, also the drawing state between steps, saves to a fixed number of bytes
9
+ // (`saveState`, `loadState`). `simState` wraps that as a part of a fixed-step simulation's state (the `SimState`
10
+ // shape of sim, matched structurally, since world does not import sim), so a snapshot, `restore` and `rebase` bring the
11
+ // walker back with everything else. A walker stepped by such a simulation draws with the simulation's `alpha`.
12
+ //
13
+ // Scope: a flat world with +y up. A curved world is not built; the places where a local up would replace y are
14
+ // marked "UP:" below (gravity, the vertical span, the slope of the base and the heading plane).
15
+ import { detAtan2, detSinCos } from '../base/trig.js';
16
+ import { newSlideResult } from './collide.js';
17
+ export const DEFAULT_WALKER = Object.freeze({
18
+ radius: 0.3,
19
+ height: 1.7,
20
+ walkSpeed: 1.6,
21
+ runSpeed: 4.5,
22
+ acceleration: 20,
23
+ airControl: 0.3,
24
+ jumpSpeed: 4.2,
25
+ gravity: 9.81,
26
+ maxStepUp: 0.35,
27
+ maxSlopeDeg: 45,
28
+ stepHz: 60,
29
+ turnSpeed: 12,
30
+ stepSmoothing: 0.1,
31
+ maxStepsPerAdvance: 15,
32
+ });
33
+ /** Bytes of one saved walker state (`Walker.saveState`). */
34
+ export const WALKER_STATE_BYTES = 20 * 8;
35
+ /** First number of a saved state; another one is not a walker state of this layout. */
36
+ const STATE_VERSION = 1;
37
+ /** A fresh, idle `WalkIntent` (allocate once, reuse). */
38
+ export function newWalkIntent() {
39
+ return { moveX: 0, moveZ: 0, run: false, jump: false };
40
+ }
41
+ /** Feet within this of the base surface stand on the base (and its slope applies). */
42
+ const ON_BASE_EPS = 1e-6;
43
+ /** Horizontal speed below which a body counts as standing, m/s. */
44
+ const STILL_SPEED = 1e-4;
45
+ /** Undersides are found a hair inside the radius, so a side the body only touches is not a ceiling. */
46
+ const CEILING_INSET = 1e-3;
47
+ /** A change of ground height smaller than this (beyond what the slope explains) is not eased as a step, metres. */
48
+ const STEP_EASE_MIN = 0.02;
49
+ /**
50
+ * Reach of the one-sided slope test, metres. A point on a kink of the base (the lip or the foot of a slope too steep)
51
+ * counts as steep only when the base is too steep on both sides of it along the gradient, so a body can walk away from
52
+ * the lip of a cliff instead of being held there by a gradient that straddles the kink.
53
+ */
54
+ const KINK_REACH = 0.05;
55
+ const TWO_PI = 6.283185307179586;
56
+ const PI = 3.141592653589793;
57
+ /**
58
+ * A kinematic body over a `CollisionWorld`. `position` is the feet (x, y, z). `yaw` is the heading with the same
59
+ * convention as a view's yaw: yaw 0 faces −z, and the facing direction is (−sin yaw, −cos yaw) in x, z, so a
60
+ * `FollowRig` with `yawDeg = yaw` in degrees looks the same way. Call `advance(dt, intent)` once per frame (or `step`
61
+ * from a script) and draw at `renderPosition`.
62
+ */
63
+ export class Walker {
64
+ setup;
65
+ /** Feet position, metres. */
66
+ position = new Float64Array(3);
67
+ velocity = new Float64Array(3);
68
+ /** Heading of the last movement, radians (see the class comment for the convention). */
69
+ yaw = 0;
70
+ grounded = false;
71
+ /** True while standing on a base surface steeper than `maxSlopeDeg` (sliding down it). */
72
+ steep = false;
73
+ /** Horizontal distance moved so far, metres (for animating a stride). */
74
+ travelled = 0;
75
+ /** Fixed steps taken since construction or the last `teleport`. */
76
+ steps = 0;
77
+ world;
78
+ dt;
79
+ tanMax;
80
+ smoothKeep;
81
+ prev = new Float64Array(3);
82
+ prevYaw = 0;
83
+ /** `travelled` before the last step (drawing interpolates between the two). */
84
+ prevTravelled = 0;
85
+ acc = 0;
86
+ pendingJump = false;
87
+ stepOffset = 0;
88
+ prevStepOffset = 0;
89
+ slid = newSlideResult();
90
+ grad = new Float64Array(2);
91
+ scratch = newWalkIntent();
92
+ stateScratch = new Float64Array(WALKER_STATE_BYTES / 8);
93
+ stateBytes = new Uint8Array(this.stateScratch.buffer);
94
+ constructor(world, setup) {
95
+ this.world = world;
96
+ this.setup = { ...DEFAULT_WALKER, ...setup };
97
+ const s = this.setup;
98
+ if (!(s.stepHz > 0))
99
+ throw new Error('Walker: stepHz must be positive');
100
+ if (!(s.radius > 0) || !(s.height > s.maxStepUp))
101
+ throw new Error('Walker: radius must be positive and height above maxStepUp');
102
+ this.dt = 1 / s.stepHz;
103
+ const sc = detSinCos((s.maxSlopeDeg * PI) / 180);
104
+ this.tanMax = sc[1] > 0 ? sc[0] / sc[1] : Infinity;
105
+ // Share of the step offset kept per step: exp(−3 dt / stepSmoothing) (95 % gone after stepSmoothing seconds).
106
+ this.smoothKeep = s.stepSmoothing > 0 ? Math.exp((-3 * this.dt) / s.stepSmoothing) : 0;
107
+ }
108
+ /** Seconds of one fixed step. */
109
+ get stepSeconds() {
110
+ return this.dt;
111
+ }
112
+ /** Puts the body at a point with no velocity; it settles onto the ground in the next step. */
113
+ teleport(x, y, z, yaw) {
114
+ const p = this.position;
115
+ p[0] = x;
116
+ p[1] = y;
117
+ p[2] = z;
118
+ this.prev.set(p);
119
+ this.velocity.fill(0);
120
+ if (yaw !== undefined)
121
+ this.yaw = yaw;
122
+ this.prevYaw = this.yaw;
123
+ this.prevTravelled = this.travelled;
124
+ this.grounded = false;
125
+ this.steep = false;
126
+ this.acc = 0;
127
+ this.pendingJump = false;
128
+ this.stepOffset = 0;
129
+ this.prevStepOffset = 0;
130
+ this.steps = 0;
131
+ }
132
+ /**
133
+ * Advances by real seconds in fixed steps; returns the number of steps taken. Leftover time carries to the next call,
134
+ * so one call of 1/30 s and two of 1/60 s end in the same place. A jump asked while no step is due is kept for the
135
+ * next step; it applies to the first step of the call only.
136
+ */
137
+ advance(dt, intent) {
138
+ if (intent.jump)
139
+ this.pendingJump = true;
140
+ if (!(dt > 0))
141
+ return 0;
142
+ this.acc += dt;
143
+ const h = this.dt;
144
+ const max = this.setup.maxStepsPerAdvance;
145
+ const sc = this.scratch;
146
+ sc.moveX = intent.moveX;
147
+ sc.moveZ = intent.moveZ;
148
+ sc.run = intent.run;
149
+ let n = 0;
150
+ // The tolerance keeps a sum of equal frame times from falling a rounding short of a whole step.
151
+ while (this.acc + 1e-9 * h >= h) {
152
+ if (n >= max) {
153
+ this.acc = 0;
154
+ break;
155
+ }
156
+ sc.jump = this.pendingJump;
157
+ this.pendingJump = false;
158
+ this.step(sc);
159
+ this.acc -= h;
160
+ n++;
161
+ }
162
+ if (this.acc < 0)
163
+ this.acc = 0;
164
+ return n;
165
+ }
166
+ /** One fixed step (for scripts and tests). */
167
+ step(intent) {
168
+ const s = this.setup;
169
+ const w = this.world;
170
+ const h = this.dt;
171
+ const p = this.position;
172
+ const v = this.velocity;
173
+ const g = this.grad;
174
+ this.prev.set(p);
175
+ this.prevYaw = this.yaw;
176
+ this.prevTravelled = this.travelled;
177
+ this.prevStepOffset = this.stepOffset;
178
+ this.stepOffset *= this.smoothKeep;
179
+ this.steps++;
180
+ // Wanted horizontal velocity.
181
+ let mx = intent.moveX;
182
+ let mz = intent.moveZ;
183
+ if (!(mx === mx) || !(mz === mz)) {
184
+ mx = 0;
185
+ mz = 0;
186
+ }
187
+ const m2 = mx * mx + mz * mz;
188
+ if (m2 > 1) {
189
+ const k = 1 / Math.sqrt(m2);
190
+ mx *= k;
191
+ mz *= k;
192
+ }
193
+ const speed = intent.run ? s.runSpeed : s.walkSpeed;
194
+ let wantX = mx * speed;
195
+ let wantZ = mz * speed;
196
+ const y = p[1];
197
+ // UP: the slope of the base and the split into downhill and across would use the local up.
198
+ let steep = false;
199
+ let downX = 0;
200
+ let downZ = 0;
201
+ let slope = 0;
202
+ if (this.grounded && y <= w.baseHeightAt(p[0], p[2]) + ON_BASE_EPS) {
203
+ slope = w.baseSlopeAt(p[0], p[2], g);
204
+ if (slope > this.tanMax && this.steepBothSides(p[0], p[2], g[0] / slope, g[1] / slope)) {
205
+ steep = true;
206
+ downX = -g[0] / slope;
207
+ downZ = -g[1] / slope;
208
+ }
209
+ }
210
+ this.steep = steep;
211
+ const accel = s.acceleration * (this.grounded ? 1 : s.airControl) * h;
212
+ if (steep) {
213
+ // Across the slope the body is steered as usual; down the slope it slides (the horizontal part of gravity along
214
+ // the incline, g·sinθ·cosθ = g·tanθ / (1 + tan²θ)) and the intent may only add speed downhill, never brake or climb.
215
+ const vd0 = v[0] * downX + v[2] * downZ;
216
+ const wd = wantX * downX + wantZ * downZ;
217
+ const latX = v[0] - vd0 * downX;
218
+ const latZ = v[2] - vd0 * downZ;
219
+ const wLatX = wantX - wd * downX;
220
+ const wLatZ = wantZ - wd * downZ;
221
+ approach(latX, latZ, wLatX, wLatZ, accel, g);
222
+ let vd = (vd0 > 0 ? vd0 : 0) + ((s.gravity * slope) / (1 + slope * slope)) * h;
223
+ if (wd > vd)
224
+ vd = vd + accel < wd ? vd + accel : wd;
225
+ v[0] = g[0] + vd * downX;
226
+ v[2] = g[1] + vd * downZ;
227
+ }
228
+ else {
229
+ approach(v[0], v[2], wantX, wantZ, accel, g);
230
+ v[0] = g[0];
231
+ v[2] = g[1];
232
+ }
233
+ // A jump leaves only from the ground, and not from a slope too steep to stand on.
234
+ if (intent.jump && this.grounded && !steep && s.jumpSpeed > 0) {
235
+ v[1] = s.jumpSpeed;
236
+ this.grounded = false;
237
+ }
238
+ // Horizontal move against the sides above the step height. The slide always runs, even standing still, so a
239
+ // solid that appears over the body pushes it out.
240
+ // UP: the span would be measured along the local up.
241
+ const out = this.slid;
242
+ const lo = y + s.maxStepUp;
243
+ const hi = y + s.height;
244
+ let hx = v[0] * h;
245
+ let hz = v[2] * h;
246
+ w.slide(p[0], p[2], s.radius, lo, hi, hx, hz, out);
247
+ if (this.grounded && (hx !== 0 || hz !== 0)) {
248
+ // Walking onto a base surface that is too steep and rising: the uphill part of the move is taken away.
249
+ const bx = out.x;
250
+ const bz = out.z;
251
+ const base = w.baseHeightAt(bx, bz);
252
+ if (base > y + ON_BASE_EPS && w.groundAt(bx, bz, y, s.maxStepUp, s.radius) <= base) {
253
+ const sl = w.baseSlopeAt(bx, bz, g);
254
+ if (sl > this.tanMax) {
255
+ const ux = g[0] / sl;
256
+ const uz = g[1] / sl;
257
+ const along = hx * ux + hz * uz;
258
+ if (along > 0) {
259
+ hx -= along * ux;
260
+ hz -= along * uz;
261
+ const va = v[0] * ux + v[2] * uz;
262
+ if (va > 0) {
263
+ v[0] = v[0] - va * ux;
264
+ v[2] = v[2] - va * uz;
265
+ }
266
+ w.slide(p[0], p[2], s.radius, lo, hi, hx, hz, out);
267
+ }
268
+ }
269
+ }
270
+ }
271
+ if (out.hit) {
272
+ // Velocity into the touched side is dropped, so the body slides along it and does not press on.
273
+ const vn = v[0] * out.nx + v[2] * out.nz;
274
+ if (vn < 0) {
275
+ v[0] = v[0] - vn * out.nx;
276
+ v[2] = v[2] - vn * out.nz;
277
+ }
278
+ }
279
+ const movedX = out.x - p[0];
280
+ const movedZ = out.z - p[2];
281
+ const moved = Math.sqrt(movedX * movedX + movedZ * movedZ);
282
+ p[0] = out.x;
283
+ p[2] = out.z;
284
+ this.travelled += moved;
285
+ // Vertical. UP: gravity and heights would be along the local up.
286
+ const x = p[0];
287
+ const z = p[2];
288
+ if (this.grounded) {
289
+ const ground = w.groundAt(x, z, y, s.maxStepUp, s.radius);
290
+ // How far below the feet the ground may be and still be followed: a step down, plus the drop of the base over
291
+ // this move on a slope (so the body runs down a steep slope instead of hopping).
292
+ let snap = s.maxStepUp;
293
+ if (ground <= w.baseHeightAt(x, z) + ON_BASE_EPS)
294
+ snap += moved * w.baseSlopeAt(x, z, g);
295
+ if (ground >= y - snap) {
296
+ // Only a step (a change the slope of this move does not explain) is eased in the drawn position.
297
+ const rise = ground - y;
298
+ if ((rise > 0 ? rise : -rise) > moved * this.tanMax + STEP_EASE_MIN)
299
+ this.stepOffset += rise;
300
+ p[1] = ground;
301
+ v[1] = 0;
302
+ }
303
+ else {
304
+ // Off an edge: falling starts from rest this step.
305
+ this.grounded = false;
306
+ v[1] = -s.gravity * h;
307
+ p[1] = y - 0.5 * s.gravity * h * h;
308
+ if (p[1] < ground)
309
+ p[1] = ground;
310
+ }
311
+ }
312
+ else {
313
+ // Exact for constant gravity, so the top of a jump is jumpSpeed² / (2 gravity) at any step rate.
314
+ let ny = y + v[1] * h - 0.5 * s.gravity * h * h;
315
+ v[1] = v[1] - s.gravity * h;
316
+ if (ny > y) {
317
+ const ceiling = w.ceilingAt(x, z, y + s.height, s.radius - CEILING_INSET);
318
+ if (ny + s.height > ceiling) {
319
+ ny = ceiling - s.height;
320
+ if (ny < y)
321
+ ny = y;
322
+ if (v[1] > 0)
323
+ v[1] = 0;
324
+ }
325
+ }
326
+ const ground = w.groundAt(x, z, y, s.maxStepUp, s.radius);
327
+ if (ny <= ground) {
328
+ ny = ground;
329
+ v[1] = 0;
330
+ this.grounded = true;
331
+ }
332
+ p[1] = ny;
333
+ }
334
+ // Heading: toward the wanted direction (or, without one, the direction moved), at most turnSpeed.
335
+ let fx = wantX;
336
+ let fz = wantZ;
337
+ if (fx * fx + fz * fz < STILL_SPEED * STILL_SPEED) {
338
+ fx = moved > STILL_SPEED * h ? movedX : 0;
339
+ fz = moved > STILL_SPEED * h ? movedZ : 0;
340
+ }
341
+ if (fx !== 0 || fz !== 0) {
342
+ // UP: the heading plane would be the local horizon.
343
+ const target = detAtan2(-fx, -fz);
344
+ let d = target - this.yaw;
345
+ while (d > PI)
346
+ d -= TWO_PI;
347
+ while (d <= -PI)
348
+ d += TWO_PI;
349
+ const lim = s.turnSpeed * h;
350
+ this.yaw += d > lim ? lim : d < -lim ? -lim : d;
351
+ if (this.yaw > PI)
352
+ this.yaw -= TWO_PI;
353
+ else if (this.yaw <= -PI)
354
+ this.yaw += TWO_PI;
355
+ }
356
+ }
357
+ /** Share of the way from the last step to the next one, 0–1. */
358
+ get alpha() {
359
+ const a = this.acc / this.dt;
360
+ return a < 0 ? 0 : a > 1 ? 1 : a;
361
+ }
362
+ /**
363
+ * Writes the whole state (position, velocity, heading, ground contact, distance, steps, the previous step for
364
+ * drawing, leftover time, a pending jump, the step easing) into `out` (at least `WALKER_STATE_BYTES` bytes from
365
+ * `offset`; default a new array) and returns it. The setup and the collision world are not state.
366
+ */
367
+ saveState(out, offset = 0) {
368
+ const f = this.stateScratch;
369
+ const p = this.position;
370
+ const v = this.velocity;
371
+ f[0] = STATE_VERSION;
372
+ f[1] = p[0];
373
+ f[2] = p[1];
374
+ f[3] = p[2];
375
+ f[4] = v[0];
376
+ f[5] = v[1];
377
+ f[6] = v[2];
378
+ f[7] = this.yaw;
379
+ f[8] = (this.grounded ? 1 : 0) + (this.steep ? 2 : 0) + (this.pendingJump ? 4 : 0);
380
+ f[9] = this.travelled;
381
+ f[10] = this.steps;
382
+ f[11] = this.prev[0];
383
+ f[12] = this.prev[1];
384
+ f[13] = this.prev[2];
385
+ f[14] = this.prevYaw;
386
+ f[15] = this.acc;
387
+ f[16] = this.stepOffset;
388
+ f[17] = this.prevStepOffset;
389
+ f[18] = this.prevTravelled;
390
+ f[19] = 0;
391
+ const o = out ?? new Uint8Array(WALKER_STATE_BYTES);
392
+ if (o.length < offset + WALKER_STATE_BYTES)
393
+ throw new Error('Walker.saveState: the output is too short');
394
+ o.set(this.stateBytes, offset);
395
+ return o;
396
+ }
397
+ /** Restores a state written by `saveState` (of a walker with the same setup). */
398
+ loadState(bytes, offset = 0) {
399
+ if (bytes.length < offset + WALKER_STATE_BYTES)
400
+ throw new Error('Walker.loadState: too few bytes');
401
+ const f = this.stateScratch;
402
+ this.stateBytes.set(bytes.subarray(offset, offset + WALKER_STATE_BYTES));
403
+ if (f[0] !== STATE_VERSION)
404
+ throw new Error('Walker.loadState: not a walker state');
405
+ this.position[0] = f[1];
406
+ this.position[1] = f[2];
407
+ this.position[2] = f[3];
408
+ this.velocity[0] = f[4];
409
+ this.velocity[1] = f[5];
410
+ this.velocity[2] = f[6];
411
+ this.yaw = f[7];
412
+ const flags = f[8];
413
+ this.grounded = (flags & 1) !== 0;
414
+ this.steep = (flags & 2) !== 0;
415
+ this.pendingJump = (flags & 4) !== 0;
416
+ this.travelled = f[9];
417
+ this.steps = f[10];
418
+ this.prev[0] = f[11];
419
+ this.prev[1] = f[12];
420
+ this.prev[2] = f[13];
421
+ this.prevYaw = f[14];
422
+ this.acc = f[15];
423
+ this.stepOffset = f[16];
424
+ this.prevStepOffset = f[17];
425
+ this.prevTravelled = f[18];
426
+ }
427
+ /**
428
+ * This walker as a part of a simulation's state, for `Simulation.addState(walker.simState('walker'))`. Step it from a
429
+ * system of that simulation (`step` once per tick, with `stepHz` equal to the simulation's `hz`) and draw it with
430
+ * the simulation's `alpha`; snapshots, `restore`, `seek` and `rebase` then bring it back with everything else.
431
+ */
432
+ simState(name) {
433
+ const buf = new Uint8Array(WALKER_STATE_BYTES);
434
+ return {
435
+ name,
436
+ save: (w) => w.bytes(this.saveState(buf)),
437
+ load: (r) => {
438
+ r.into(buf);
439
+ this.loadState(buf);
440
+ },
441
+ };
442
+ }
443
+ /**
444
+ * Position between the last two steps for drawing, written to `out`. A step up or down is eased out over
445
+ * `stepSmoothing` seconds in y. `alpha` is the share of the way to the next step; default the walker's own (from
446
+ * `advance`), a walker stepped by a simulation passes the simulation's `alpha`.
447
+ */
448
+ renderPosition(out, alpha) {
449
+ const a = alpha === undefined ? this.alpha : alpha < 0 ? 0 : alpha > 1 ? 1 : alpha;
450
+ const p = this.position;
451
+ const q = this.prev;
452
+ out[0] = q[0] + (p[0] - q[0]) * a;
453
+ out[1] = q[1] + (p[1] - q[1]) * a - (this.prevStepOffset + (this.stepOffset - this.prevStepOffset) * a);
454
+ out[2] = q[2] + (p[2] - q[2]) * a;
455
+ }
456
+ /** Heading between the last two steps for drawing, radians. `alpha` as for `renderPosition`. */
457
+ renderYaw(alpha) {
458
+ let d = this.yaw - this.prevYaw;
459
+ if (d > PI)
460
+ d -= TWO_PI;
461
+ else if (d < -PI)
462
+ d += TWO_PI;
463
+ const a = alpha === undefined ? this.alpha : alpha < 0 ? 0 : alpha > 1 ? 1 : alpha;
464
+ return this.prevYaw + d * a;
465
+ }
466
+ /**
467
+ * `travelled` between the last two steps for drawing (a stride kept in step with the drawn position). `alpha` as for
468
+ * `renderPosition`.
469
+ */
470
+ renderTravelled(alpha) {
471
+ const a = alpha === undefined ? this.alpha : alpha < 0 ? 0 : alpha > 1 ? 1 : alpha;
472
+ return this.prevTravelled + (this.travelled - this.prevTravelled) * a;
473
+ }
474
+ /** Horizontal metres moved in the last step (divide by `stepSeconds` for the speed); 0 after a `teleport`. */
475
+ get lastStepDistance() {
476
+ return this.travelled - this.prevTravelled;
477
+ }
478
+ /** Whether the base rises more steeply than the limit on both sides of (x, z) along the uphill unit (ux, uz). */
479
+ steepBothSides(x, z, ux, uz) {
480
+ const w = this.world;
481
+ const e = KINK_REACH;
482
+ const h = w.baseHeightAt(x, z);
483
+ const up = w.baseHeightAt(x + ux * e, z + uz * e) - h;
484
+ const down = h - w.baseHeightAt(x - ux * e, z - uz * e);
485
+ const limit = this.tanMax * e;
486
+ return up > limit && down > limit;
487
+ }
488
+ }
489
+ /** Moves (vx, vz) toward (tx, tz) by at most `maxDelta`; writes the result to `out[0]`, `out[1]`. */
490
+ function approach(vx, vz, tx, tz, maxDelta, out) {
491
+ const ex = tx - vx;
492
+ const ez = tz - vz;
493
+ const e2 = ex * ex + ez * ez;
494
+ if (e2 <= maxDelta * maxDelta) {
495
+ out[0] = tx;
496
+ out[1] = tz;
497
+ return;
498
+ }
499
+ const k = maxDelta / Math.sqrt(e2);
500
+ out[0] = vx + ex * k;
501
+ out[1] = vz + ez * k;
502
+ }