@pinkaphant/kolmo 0.4.4 → 0.4.6
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/AGENTS.md +29 -0
- package/CLAUDE.md +8 -0
- package/README.md +67 -8
- package/bin/kolmo.mjs +456 -16
- package/dist/engine/index.d.ts +2 -0
- package/dist/engine/index.js +7 -5
- package/dist/engine/kolmoActions.js +24 -9
- package/dist/engine/kolmoActions.js.map +1 -1
- package/dist/engine/kolmoActor.js +19 -31
- package/dist/engine/kolmoActor.js.map +1 -1
- package/dist/engine/kolmoActorHelpers.d.ts +24 -0
- package/dist/engine/kolmoActorHelpers.js +64 -0
- package/dist/engine/kolmoActorHelpers.js.map +1 -0
- package/dist/engine/kolmoGame.js +4 -1
- package/dist/engine/kolmoGame.js.map +1 -1
- package/dist/engine/kolmoHud.js +34 -15
- package/dist/engine/kolmoHud.js.map +1 -1
- package/dist/engine/kolmoInput.d.ts +2 -0
- package/dist/engine/kolmoInput.js +12 -12
- package/dist/engine/kolmoInput.js.map +1 -1
- package/dist/engine/kolmoLongDrive.d.ts +1 -1
- package/dist/engine/kolmoLongDrive.js +10 -8
- package/dist/engine/kolmoLongDrive.js.map +1 -1
- package/dist/engine/kolmoPilotMesh.js +189 -48
- package/dist/engine/kolmoPilotMesh.js.map +1 -1
- package/dist/engine/kolmoTouchOverlay.d.ts +21 -0
- package/dist/engine/kolmoTouchOverlay.js +326 -0
- package/dist/engine/kolmoTouchOverlay.js.map +1 -0
- package/dist/engine/kolmoTouchState.d.ts +50 -0
- package/dist/engine/kolmoTouchState.js +210 -0
- package/dist/engine/kolmoTouchState.js.map +1 -0
- package/dist/engine/kolmoVehicleConfig.d.ts +2 -0
- package/dist/engine/kolmoVehicleConfig.js +8 -4
- package/dist/engine/kolmoVehicleConfig.js.map +1 -1
- package/dist/engine/kolmoVehicleDriver.d.ts +1 -0
- package/dist/engine/kolmoVehicleDriver.js +26 -50
- package/dist/engine/kolmoVehicleDriver.js.map +1 -1
- package/dist/engine/kolmoVehicleInner.js +31 -19
- package/dist/engine/kolmoVehicleInner.js.map +1 -1
- package/dist/engine/kolmoWorld.js +6 -2
- package/dist/engine/kolmoWorld.js.map +1 -1
- package/dist/engine/longDriveKit.d.ts +6 -1
- package/dist/engine/longDriveKit.js +8 -5
- package/dist/engine/longDriveKit.js.map +1 -1
- package/dist/engine/macroCellBuilder.d.ts +10 -0
- package/dist/engine/macroCellBuilder.js +101 -18
- package/dist/engine/macroCellBuilder.js.map +1 -1
- package/dist/engine/mainlandMacroCell.js +18 -9
- package/dist/engine/mainlandMacroCell.js.map +1 -1
- package/dist/importer/DomainWorkshopPanel.d.ts +3 -3
- package/dist/importer/DomainWorkshopPanel.js +148 -44
- package/dist/importer/DomainWorkshopPanel.js.map +1 -1
- package/dist/importer/GateHudBar.d.ts +2 -0
- package/dist/importer/GateHudBar.js +113 -35
- package/dist/importer/GateHudBar.js.map +1 -1
- package/dist/importer/GlbImportGate.js +16 -3
- package/dist/importer/GlbImportGate.js.map +1 -1
- package/dist/importer/LiveDrivePads.js +5 -3
- package/dist/importer/LiveDrivePads.js.map +1 -1
- package/dist/importer/Step1CalibrationPlinth.js +2 -2
- package/dist/importer/WheelRingHandles3D.js +2 -1
- package/dist/importer/WheelRingHandles3D.js.map +1 -1
- package/dist/importer/chopShopActions.js +20 -16
- package/dist/importer/chopShopActions.js.map +1 -1
- package/dist/importer/chopShopHandlers.js +16 -9
- package/dist/importer/chopShopHandlers.js.map +1 -1
- package/dist/importer/chopShopIoActions.d.ts +2 -0
- package/dist/importer/chopShopIoActions.js +33 -2
- package/dist/importer/chopShopIoActions.js.map +1 -1
- package/dist/importer/cylinderBooleanSlice.js.map +1 -1
- package/dist/importer/fusedWheelDetect.d.ts +2 -2
- package/dist/importer/fusedWheelDetect.js +227 -106
- package/dist/importer/fusedWheelDetect.js.map +1 -1
- package/dist/importer/fusedWheelFit.d.ts +6 -0
- package/dist/importer/fusedWheelFit.js +82 -1
- package/dist/importer/fusedWheelFit.js.map +1 -1
- package/dist/importer/gateStore.d.ts +2 -2
- package/dist/importer/gateStore.js +4 -3
- package/dist/importer/gateStore.js.map +1 -1
- package/dist/importer/glbNormalizer.js +5 -0
- package/dist/importer/glbNormalizer.js.map +1 -1
- package/dist/importer/importerWebMcpTools.js +3 -2
- package/dist/importer/importerWebMcpTools.js.map +1 -1
- package/dist/importer/index.d.ts +4 -2
- package/dist/importer/index.js +7 -3
- package/dist/importer/khrCarGeometryMass.d.ts +20 -0
- package/dist/importer/khrCarGeometryMass.js +125 -0
- package/dist/importer/khrCarGeometryMass.js.map +1 -0
- package/dist/importer/khrCarPlanHelpers.js +12 -22
- package/dist/importer/khrCarPlanHelpers.js.map +1 -1
- package/dist/importer/khrCarPlanner.js +52 -85
- package/dist/importer/khrCarPlanner.js.map +1 -1
- package/dist/importer/proceduralC12AndCraft.d.ts +18 -0
- package/dist/importer/proceduralC12AndCraft.js +302 -0
- package/dist/importer/proceduralC12AndCraft.js.map +1 -0
- package/dist/importer/proceduralDomainBuilders.d.ts +10 -0
- package/dist/importer/proceduralDomainBuilders.js +346 -0
- package/dist/importer/proceduralDomainBuilders.js.map +1 -0
- package/dist/importer/proceduralPersonAndProp.d.ts +8 -0
- package/dist/importer/proceduralPersonAndProp.js +303 -0
- package/dist/importer/proceduralPersonAndProp.js.map +1 -0
- package/dist/importer/starterScene.d.ts +3 -1
- package/dist/importer/starterScene.js +69 -13
- package/dist/importer/starterScene.js.map +1 -1
- package/dist/importer/universalGateBake.js +43 -11
- package/dist/importer/universalGateBake.js.map +1 -1
- package/dist/importer/useChopShopController.d.ts +3 -1
- package/dist/importer/useChopShopController.js +33 -18
- package/dist/importer/useChopShopController.js.map +1 -1
- package/dist/importer/useChopShopState.d.ts +1 -1
- package/dist/importer/useChopShopState.js +35 -9
- package/dist/importer/useChopShopState.js.map +1 -1
- package/dist/importer/wheelCutterHelpers.d.ts +7 -4
- package/dist/importer/wheelCutterHelpers.js +58 -28
- package/dist/importer/wheelCutterHelpers.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +20 -7
- package/dist/junk/DestructibleTarget.js +9 -3
- package/dist/junk/DestructibleTarget.js.map +1 -1
- package/dist/junk/ShardPieces.js +10 -2
- package/dist/junk/ShardPieces.js.map +1 -1
- package/dist/junk/index.d.ts +1 -0
- package/dist/junk/index.js +2 -1
- package/dist/junk/junkProceduralScenes.d.ts +12 -0
- package/dist/junk/junkProceduralScenes.js +220 -0
- package/dist/junk/junkProceduralScenes.js.map +1 -0
- package/dist/sand3d/KolmoSand3D.d.ts +19 -0
- package/dist/sand3d/KolmoSand3D.js +92 -0
- package/dist/sand3d/KolmoSand3D.js.map +1 -0
- package/dist/sand3d/Sand3dWorkshopSection.d.ts +8 -0
- package/dist/sand3d/Sand3dWorkshopSection.js +117 -0
- package/dist/sand3d/Sand3dWorkshopSection.js.map +1 -0
- package/dist/sand3d/gateBakeHelpers.d.ts +21 -0
- package/dist/sand3d/gateBakeHelpers.js +225 -0
- package/dist/sand3d/gateBakeHelpers.js.map +1 -0
- package/dist/sand3d/gateDoorwayBake.d.ts +12 -0
- package/dist/sand3d/gateDoorwayBake.js +203 -0
- package/dist/sand3d/gateDoorwayBake.js.map +1 -0
- package/dist/sand3d/index.d.ts +3 -0
- package/dist/sand3d/index.js +4 -0
- package/dist/sand3d/index.js.map +1 -0
- package/dist/sand3d/sand3dAssetLoader.d.ts +28 -0
- package/dist/sand3d/sand3dAssetLoader.js +177 -0
- package/dist/sand3d/sand3dAssetLoader.js.map +1 -0
- package/dist/suika/KolmoSuikaOrb.d.ts +33 -0
- package/dist/suika/KolmoSuikaOrb.js +375 -0
- package/dist/suika/KolmoSuikaOrb.js.map +1 -0
- package/dist/suika/index.d.ts +4 -0
- package/dist/suika/index.js +5 -0
- package/dist/suika/index.js.map +1 -0
- package/dist/suika/suikaArbiter.d.ts +60 -0
- package/dist/suika/suikaArbiter.js +265 -0
- package/dist/suika/suikaArbiter.js.map +1 -0
- package/dist/suika/suikaGeometry.d.ts +10 -0
- package/dist/suika/suikaGeometry.js +251 -0
- package/dist/suika/suikaGeometry.js.map +1 -0
- package/dist/suika/suikaTypes.d.ts +74 -0
- package/dist/suika/suikaTypes.js +196 -0
- package/dist/suika/suikaTypes.js.map +1 -0
- package/dist/world/index.d.ts +1 -1
- package/dist/world/index.js +2 -2
- package/dist/world/worldTypes.d.ts +4 -0
- package/dist/world/worldTypes.js +21 -1
- package/dist/world/worldTypes.js.map +1 -1
- package/docs/api-reference.md +82 -125
- package/package.json +22 -11
- package/skills/kolmo-engine/SKILL.md +111 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { KolmoGravityContext } from "../physics/index.js";
|
|
2
|
+
import { findNearestInteractableVehicleId, shiftActorCameraOrigin, syncActorFollowCamera } from "./kolmoActorHelpers.js";
|
|
2
3
|
import { useKolmoInput } from "./kolmoInput.js";
|
|
3
4
|
import { DefaultPilotMesh } from "./kolmoPilotMesh.js";
|
|
4
|
-
import { ActorTrait,
|
|
5
|
+
import { ActorTrait, PilotLink, PilotedTag, TransformTrait, VehicleTrait, VelocityTrait, WorldSessionTrait, WorldSpawnTrait, ensureWorldSession, pilotedVehicleQuery, useActiveKolmoWorld, writeEntityKinematics } from "./kolmoTraits.js";
|
|
5
6
|
import { kolmoActions, kolmoTelemetry } from "./kolmoActions.js";
|
|
6
7
|
import { useCallback, useContext, useLayoutEffect, useMemo, useRef } from "react";
|
|
7
8
|
import * as THREE from "three";
|
|
@@ -12,9 +13,6 @@ import { useRapier } from "@react-three/rapier";
|
|
|
12
13
|
import { Ecctrl } from "@pinkaphant/ecctrl";
|
|
13
14
|
import { EcctrlCameraControls } from "@pinkaphant/ecctrl/camera";
|
|
14
15
|
// src/engine/kolmoActor.tsx
|
|
15
|
-
var _seatVec = new THREE.Vector3();
|
|
16
|
-
var _camPos = new THREE.Vector3();
|
|
17
|
-
var _camTgt = new THREE.Vector3();
|
|
18
16
|
function KolmoActor({ position, enabled = true, getInput, ecctrlProps, cameraProps, children }) {
|
|
19
17
|
const customGravity = useContext(KolmoGravityContext);
|
|
20
18
|
const activeWorld = useActiveKolmoWorld();
|
|
@@ -37,6 +35,7 @@ function KolmoActor({ position, enabled = true, getInput, ecctrlProps, cameraPro
|
|
|
37
35
|
const camRef = useRef(null);
|
|
38
36
|
const followPos = useRef(new THREE.Vector3());
|
|
39
37
|
const followUp = useRef(new THREE.Vector3(0, 1, 0));
|
|
38
|
+
const camSyncedRef = useRef(false);
|
|
40
39
|
const prevInteractRef = useRef(false);
|
|
41
40
|
const prevNearIdRef = useRef("");
|
|
42
41
|
const cooldownRef = useRef(0);
|
|
@@ -60,12 +59,7 @@ function KolmoActor({ position, enabled = true, getInput, ecctrlProps, cameraPro
|
|
|
60
59
|
ecctrlRef.current.currPos.x -= dx;
|
|
61
60
|
ecctrlRef.current.currPos.z -= dz;
|
|
62
61
|
}
|
|
63
|
-
|
|
64
|
-
if (cam?.getPosition && cam?.getTarget && cam?.setLookAt) {
|
|
65
|
-
cam.getPosition(_camPos);
|
|
66
|
-
cam.getTarget(_camTgt);
|
|
67
|
-
cam.setLookAt(_camPos.x - dx, _camPos.y, _camPos.z - dz, _camTgt.x - dx, _camTgt.y, _camTgt.z - dz, false);
|
|
68
|
-
}
|
|
62
|
+
shiftActorCameraOrigin(camRef.current, dx, dz);
|
|
69
63
|
}, []);
|
|
70
64
|
useLayoutEffect(() => {
|
|
71
65
|
const ent = activeWorld.spawn(ActorTrait({
|
|
@@ -151,9 +145,11 @@ function KolmoActor({ position, enabled = true, getInput, ecctrlProps, cameraPro
|
|
|
151
145
|
});
|
|
152
146
|
}, [onOriginShift]);
|
|
153
147
|
const { world: rapierWorld } = useRapier();
|
|
154
|
-
const onFoot = enabled && !
|
|
155
|
-
if (!onFoot)
|
|
156
|
-
|
|
148
|
+
const onFoot = enabled && !pilotedVehEnt;
|
|
149
|
+
if (!onFoot) {
|
|
150
|
+
spawnTupleRef.current = null;
|
|
151
|
+
camSyncedRef.current = false;
|
|
152
|
+
} else if (!spawnTupleRef.current) {
|
|
157
153
|
const p = spawnOverrideRef.current.pos ?? ecctrlProps?.position ?? defaultPos;
|
|
158
154
|
const r = ecctrlProps?.rotation ?? [
|
|
159
155
|
0,
|
|
@@ -213,17 +209,7 @@ function KolmoActor({ position, enabled = true, getInput, ecctrlProps, cameraPro
|
|
|
213
209
|
kolmoTelemetry.gear = 0;
|
|
214
210
|
kolmoTelemetry.throttle = +handle.isMoving;
|
|
215
211
|
kolmoTelemetry.boost = inp.boost && handle.isMoving ? 1 : 0;
|
|
216
|
-
|
|
217
|
-
for (const ent of activeWorld.query(VehicleTrait, InteractableTrait)) {
|
|
218
|
-
const v = ent.get(VehicleTrait), it = ent.get(InteractableTrait);
|
|
219
|
-
if (!v?.handle || !it) continue;
|
|
220
|
-
_seatVec.set(v.seatOffset[0], v.seatOffset[1], v.seatOffset[2]).applyQuaternion(v.handle.currQuat).add(v.handle.currPos);
|
|
221
|
-
const dSq = handle.currPos.distanceToSquared(_seatVec);
|
|
222
|
-
if (dSq <= it.radius * it.radius && dSq < bestDistSq) {
|
|
223
|
-
bestDistSq = dSq;
|
|
224
|
-
nearestId = v.id;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
212
|
+
const nearestId = findNearestInteractableVehicleId(activeWorld, handle.currPos);
|
|
227
213
|
kolmoTelemetry.nearVehicleId = nearestId;
|
|
228
214
|
const nearStr = nearestId ?? "";
|
|
229
215
|
if (nearStr !== prevNearIdRef.current) {
|
|
@@ -237,13 +223,15 @@ function KolmoActor({ position, enabled = true, getInput, ecctrlProps, cameraPro
|
|
|
237
223
|
if (edge && nearestId && cooldownRef.current === 0) {
|
|
238
224
|
cooldownRef.current = .25;
|
|
239
225
|
kolmoActions(activeWorld).boardVehicle(nearestId);
|
|
240
|
-
} else if (camRef.current && handle.currPos.lengthSq() > 0) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
226
|
+
} else if (camRef.current && handle.currPos.lengthSq() > 0) syncActorFollowCamera({
|
|
227
|
+
cam: camRef.current,
|
|
228
|
+
camera: state.camera,
|
|
229
|
+
handle,
|
|
230
|
+
followPos: followPos.current,
|
|
231
|
+
followUp: followUp.current,
|
|
232
|
+
camSyncedRef,
|
|
233
|
+
dt
|
|
234
|
+
});
|
|
247
235
|
});
|
|
248
236
|
if (!onFoot || !spawnTupleRef.current) return null;
|
|
249
237
|
return /* @__PURE__ */ jsxs(Fragment, { children: [hasDom && cameraProps !== false && cameraProps !== null && /* @__PURE__ */ jsx(EcctrlCameraControls, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kolmoActor.js","names":["_Fragment"],"sources":["../../src/engine/kolmoActor.tsx"],"sourcesContent":["import React, { useCallback, useContext, useLayoutEffect, useMemo, useRef, type ComponentProps, type ReactNode } from \"react\";\nimport * as THREE from \"three\";\nimport { useFrame, useThree } from \"@react-three/fiber\";\nimport { useRapier } from \"@react-three/rapier\";\nimport { useQueryFirst } from \"koota/react\";\nimport { Ecctrl, type EcctrlHandle, type EcctrlProps } from \"@pinkaphant/ecctrl\";\nimport { EcctrlCameraControls } from \"@pinkaphant/ecctrl/camera\";\nimport { KolmoGravityContext } from \"../physics/index.js\";\nimport { useKolmoInput, type KolmoInputGetter } from \"./kolmoInput.js\";\nimport { DefaultPilotMesh } from \"./kolmoPilotMesh.js\";\nimport {\n ActorTrait,\n InteractableTrait,\n PilotLink,\n PilotedTag,\n TransformTrait,\n VehicleTrait,\n VelocityTrait,\n WorldSessionTrait,\n WorldSpawnTrait,\n ensureWorldSession,\n kolmoActions,\n kolmoTelemetry,\n pilotedVehicleQuery,\n useActiveKolmoWorld,\n writeEntityKinematics,\n} from \"./kolmoStore.js\";\n\nconst _seatVec = new THREE.Vector3();\nconst _camPos = new THREE.Vector3();\nconst _camTgt = new THREE.Vector3();\n\nexport interface KolmoActorProps {\n readonly position?: [number, number, number];\n readonly enabled?: boolean;\n readonly getInput?: KolmoInputGetter;\n readonly ecctrlProps?: Omit<EcctrlProps, \"ref\">;\n readonly cameraProps?: Omit<ComponentProps<typeof EcctrlCameraControls>, \"ref\"> | null | false;\n readonly children?: ReactNode;\n}\n\nexport function KolmoActor({\n position,\n enabled = true,\n getInput,\n ecctrlProps,\n cameraProps,\n children,\n}: KolmoActorProps): React.JSX.Element | null {\n const customGravity = useContext(KolmoGravityContext);\n const activeWorld = useActiveKolmoWorld();\n useLayoutEffect(() => { ensureWorldSession(activeWorld); }, [activeWorld]);\n\n const worldSpawn = activeWorld.has(WorldSpawnTrait) ? activeWorld.get(WorldSpawnTrait) : undefined;\n const hasExplicitPos = position !== undefined || ecctrlProps?.position !== undefined;\n const defaultPos: [number, number, number] = position ?? worldSpawn?.actorPos ?? [0, 1.4, 0];\n const snappedAutoSpawnRef = useRef(hasExplicitPos || !!worldSpawn);\n\n const pilotedVehEnt = useQueryFirst(VehicleTrait, PilotedTag);\n const readInput = useKolmoInput(getInput);\n const hasDom = useThree((s) => typeof s.gl?.domElement?.setAttribute === \"function\");\n\n const actorEntRef = useRef<ReturnType<typeof activeWorld.spawn> | null>(null);\n const ecctrlRef = useRef<EcctrlHandle | null>(null);\n const camRef = useRef<ComponentProps<typeof EcctrlCameraControls> extends never ? never : any>(null);\n const followPos = useRef(new THREE.Vector3());\n const followUp = useRef(new THREE.Vector3(0, 1, 0));\n const prevInteractRef = useRef(false);\n const prevNearIdRef = useRef(\"\");\n const cooldownRef = useRef(0);\n const spawnOverrideRef = useRef<{ pos: [number, number, number] | null; yaw: number }>({ pos: null, yaw: 0 });\n const spawnTupleRef = useRef<{ pos: [number, number]; y: number; rot: [number, number, number] } | null>(null);\n const moveCmdRef = useRef({ forward: false, backward: false, leftward: false, rightward: false, run: false, jump: false });\n\n const onOriginShift = useCallback((dx: number, dz: number) => {\n followPos.current.x -= dx;\n followPos.current.z -= dz;\n if (ecctrlRef.current) { ecctrlRef.current.currPos.x -= dx; ecctrlRef.current.currPos.z -= dz; }\n const cam = camRef.current;\n if (cam?.getPosition && cam?.getTarget && cam?.setLookAt) {\n cam.getPosition(_camPos);\n cam.getTarget(_camTgt);\n cam.setLookAt(_camPos.x - dx, _camPos.y, _camPos.z - dz, _camTgt.x - dx, _camTgt.y, _camTgt.z - dz, false);\n }\n }, []);\n\n useLayoutEffect(() => {\n const ent = activeWorld.spawn(\n ActorTrait({ id: \"pilot\", handle: ecctrlRef.current, exitSpawnPos: null, exitSpawnYaw: 0, exitLinVel: null, onOriginShift }),\n TransformTrait,\n VelocityTrait,\n );\n actorEntRef.current = ent;\n const prePiloted = activeWorld.queryFirst(pilotedVehicleQuery);\n if (prePiloted) ent.add(PilotLink(prePiloted));\n const unsub = activeWorld.onChange(ActorTrait, (changed) => {\n if (changed !== ent) return;\n const d = ent.get(ActorTrait);\n if (d) spawnOverrideRef.current = { pos: d.exitSpawnPos, yaw: d.exitSpawnYaw };\n });\n return () => { unsub(); actorEntRef.current = null; ent.destroy(); };\n }, [activeWorld, onOriginShift]);\n\n useLayoutEffect(() => {\n const ent = actorEntRef.current;\n if (ent && pilotedVehEnt && !ent.has(PilotLink(pilotedVehEnt))) {\n ent.add(PilotLink(pilotedVehEnt));\n }\n }, [pilotedVehEnt]);\n\n useLayoutEffect(() => {\n if (snappedAutoSpawnRef.current || hasExplicitPos) return;\n const applySpawn = (): void => {\n if (snappedAutoSpawnRef.current) return;\n const sp = activeWorld.has(WorldSpawnTrait) ? activeWorld.get(WorldSpawnTrait) : undefined;\n const body = ecctrlRef.current?.body;\n if (sp && body && !spawnOverrideRef.current.pos) {\n snappedAutoSpawnRef.current = true;\n body.setTranslation({ x: sp.actorPos[0], y: sp.actorPos[1], z: sp.actorPos[2] }, true);\n body.setLinvel({ x: 0, y: 0, z: 0 }, true);\n body.setAngvel({ x: 0, y: 0, z: 0 }, true);\n }\n };\n applySpawn();\n const unAdd = activeWorld.onAdd(WorldSpawnTrait, applySpawn);\n const unChg = activeWorld.onChange(WorldSpawnTrait, applySpawn);\n return () => { unAdd(); unChg(); };\n }, [activeWorld, hasExplicitPos]);\n\n const assignRef = useCallback((handle: EcctrlHandle | null) => {\n ecctrlRef.current = handle;\n const ent = actorEntRef.current;\n if (!ent || !ent.isAlive()) return;\n const cur = ent.get(ActorTrait);\n if (handle?.body && cur?.exitLinVel)\n handle.body.setLinvel({ x: cur.exitLinVel[0], y: cur.exitLinVel[1], z: cur.exitLinVel[2] }, true);\n if (handle?.body) spawnOverrideRef.current.pos = null;\n ent.set(ActorTrait, {\n id: cur?.id ?? \"pilot\", handle,\n exitSpawnPos: handle?.body ? null : cur?.exitSpawnPos ?? null,\n exitSpawnYaw: cur?.exitSpawnYaw ?? 0,\n exitLinVel: handle?.body ? null : cur?.exitLinVel ?? null,\n onOriginShift,\n });\n }, [onOriginShift]);\n\n const { world: rapierWorld } = useRapier();\n const isPilotingVehicle = Boolean(pilotedVehEnt);\n const onFoot = enabled && !isPilotingVehicle;\n\n if (!onFoot) {\n spawnTupleRef.current = null;\n } else if (!spawnTupleRef.current) {\n const p = spawnOverrideRef.current.pos ?? (ecctrlProps?.position as [number, number, number] | undefined) ?? defaultPos;\n const r = (ecctrlProps?.rotation as [number, number, number] | undefined) ?? [0, spawnOverrideRef.current.yaw, 0];\n spawnTupleRef.current = { pos: [p[0], p[2]], y: p[1], rot: [r[0], r[1], r[2]] };\n }\n\n const mergedUserData = useMemo(() => {\n const base = (ecctrlProps?.userData ?? {}) as Record<string, unknown>;\n const ecctrlMeta = (base.ecctrl ?? {}) as Record<string, unknown>;\n return { ...base, ecctrl: { excludeVehicleRay: true, ...ecctrlMeta } };\n }, [ecctrlProps?.userData]);\n\n useFrame((state, delta) => {\n if (!enabled) return;\n const dt = rapierWorld.timestep > 0 ? rapierWorld.timestep : Math.min(Math.max(delta, 1e-4), 1 / 30);\n if (cooldownRef.current > 0) cooldownRef.current = Math.max(0, cooldownRef.current - dt);\n const inp = readInput();\n const edge = inp.interact && !prevInteractRef.current;\n prevInteractRef.current = inp.interact;\n\n if (activeWorld.queryFirst(pilotedVehicleQuery)) {\n if (edge && cooldownRef.current === 0) {\n cooldownRef.current = 0.25;\n kolmoActions(activeWorld).exitVehicle();\n }\n return;\n }\n\n const handle = ecctrlRef.current;\n if (!handle || !handle.body) return;\n const cmd = moveCmdRef.current;\n cmd.forward = inp.forward; cmd.backward = inp.backward;\n cmd.leftward = inp.left; cmd.rightward = inp.right;\n cmd.run = inp.boost; cmd.jump = inp.brake;\n handle.setMovement(cmd);\n\n const speedKmh = handle.moveSpeed * 3.6;\n if (actorEntRef.current?.isAlive()) {\n writeEntityKinematics(activeWorld, actorEntRef.current, handle.currPos, handle.currQuat, handle.currLinVel, speedKmh);\n }\n kolmoTelemetry.mode = \"foot\";\n kolmoTelemetry.speedKmh = speedKmh;\n kolmoTelemetry.rpm = 0; kolmoTelemetry.gear = 0;\n kolmoTelemetry.throttle = +handle.isMoving;\n kolmoTelemetry.boost = inp.boost && handle.isMoving ? 1 : 0;\n\n let nearestId: string | null = null, bestDistSq = Infinity;\n for (const ent of activeWorld.query(VehicleTrait, InteractableTrait)) {\n const v = ent.get(VehicleTrait), it = ent.get(InteractableTrait);\n if (!v?.handle || !it) continue;\n _seatVec.set(v.seatOffset[0], v.seatOffset[1], v.seatOffset[2]).applyQuaternion(v.handle.currQuat).add(v.handle.currPos);\n const dSq = handle.currPos.distanceToSquared(_seatVec);\n if (dSq <= it.radius * it.radius && dSq < bestDistSq) { bestDistSq = dSq; nearestId = v.id; }\n }\n kolmoTelemetry.nearVehicleId = nearestId;\n const nearStr = nearestId ?? \"\";\n if (nearStr !== prevNearIdRef.current) {\n prevNearIdRef.current = nearStr;\n const curSession = activeWorld.get(WorldSessionTrait);\n activeWorld.set(WorldSessionTrait, {\n activeRetroPreset: curSession?.activeRetroPreset ?? \"dreamcast\",\n nearVehicleId: nearStr,\n });\n }\n if (edge && nearestId && cooldownRef.current === 0) {\n cooldownRef.current = 0.25;\n kolmoActions(activeWorld).boardVehicle(nearestId);\n } else if (camRef.current && handle.currPos.lengthSq() > 0) {\n followPos.current.copy(handle.currPos).addScaledVector(handle.bodyYAxis, 0.55);\n followUp.current.copy(handle.upAxis);\n camRef.current.moveTo(followPos.current.x, followPos.current.y, followPos.current.z, true);\n state.camera.up.lerp(followUp.current, 0.14);\n camRef.current.setUp(state.camera.up);\n }\n });\n\n if (!onFoot || !spawnTupleRef.current) return null;\n\n return (\n <>\n {hasDom && cameraProps !== false && cameraProps !== null && (\n <EcctrlCameraControls ref={camRef} makeDefault minPolarAngle={0.12} maxPolarAngle={Math.PI - 0.12} smoothTime={0.08} {...(cameraProps ?? {})} />\n )}\n <Ecctrl\n ref={assignRef} enableCustomGravity={customGravity} maxWalkVel={3.2} maxRunVel={7.2} jumpVel={5.8}\n {...ecctrlProps}\n position={[spawnTupleRef.current.pos[0], spawnTupleRef.current.y, spawnTupleRef.current.pos[1]]}\n rotation={spawnTupleRef.current.rot}\n userData={mergedUserData}\n >\n {children ?? <DefaultPilotMesh />}\n </Ecctrl>\n </>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;AA4BA,IAAM,WAAW,IAAI,MAAM,QAAQ;AACnC,IAAM,UAAU,IAAI,MAAM,QAAQ;AAClC,IAAM,UAAU,IAAI,MAAM,QAAQ;AAWlC,SAAgB,WAAW,EACvB,UACA,UAAU,MACV,UACA,aACA,aACA,YAC0C;CAC1C,MAAM,gBAAgB,WAAW,mBAAmB;CACpD,MAAM,cAAc,oBAAoB;CACxC,sBAAsB;EAAE,mBAAmB,WAAW;CAAG,GAAG,CAAC,WAAW,CAAC;CAEzE,MAAM,aAAa,YAAY,IAAI,eAAe,IAAI,YAAY,IAAI,eAAe,IAAI,KAAA;CACzF,MAAM,iBAAiB,aAAa,KAAA,KAAa,aAAa,aAAa,KAAA;CAC3E,MAAM,aAAuC,YAAY,YAAY,YAAY;EAAC;EAAG;EAAK;CAAC;CAC3F,MAAM,sBAAsB,OAAO,kBAAkB,CAAC,CAAC,UAAU;CAEjE,MAAM,gBA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AAM,eAAe,KAAK,KAAK;EAAM,YAAY;EAAM,GAAK,eAAe,CAAC;CAAK,CAAA,GAEnJ,oBAAC,QAAD;EACI,KAAK;EAAW,qBAAqB;EAAe,YAAY;EAAK,WAAW;EAAK,SAAS;EAC9F,GAAI;EACJ,UAAU;GAAC,cAAc,QAAQ,IAAI;GAAI,cAAc,QAAQ;GAAG,cAAc,QAAQ,IAAI;EAAE;EAC9F,UAAU,cAAc,QAAQ;EAChC,UAAU;EAET,UAAA,YAAY,oBAAC,kBAAD,CAAmB,CAAA;CAC5B,CAAA,CACV,EAAA,CAAA;AAEV"}
|
|
1
|
+
{"version":3,"file":"kolmoActor.js","names":["_Fragment"],"sources":["../../src/engine/kolmoActor.tsx"],"sourcesContent":["import React, { useCallback, useContext, useLayoutEffect, useMemo, useRef, type ComponentProps, type ReactNode } from \"react\";\nimport * as THREE from \"three\";\nimport { useFrame, useThree } from \"@react-three/fiber\";\nimport { useRapier } from \"@react-three/rapier\";\nimport { useQueryFirst } from \"koota/react\";\nimport { Ecctrl, type EcctrlHandle, type EcctrlProps } from \"@pinkaphant/ecctrl\";\nimport { EcctrlCameraControls } from \"@pinkaphant/ecctrl/camera\";\nimport { KolmoGravityContext } from \"../physics/index.js\";\nimport {\n findNearestInteractableVehicleId,\n shiftActorCameraOrigin,\n syncActorFollowCamera,\n type ActorCameraController,\n} from \"./kolmoActorHelpers.js\";\nimport { useKolmoInput, type KolmoInputGetter } from \"./kolmoInput.js\";\nimport { DefaultPilotMesh } from \"./kolmoPilotMesh.js\";\nimport {\n ActorTrait,\n PilotLink,\n PilotedTag,\n TransformTrait,\n VehicleTrait,\n VelocityTrait,\n WorldSessionTrait,\n WorldSpawnTrait,\n ensureWorldSession,\n kolmoActions,\n kolmoTelemetry,\n pilotedVehicleQuery,\n useActiveKolmoWorld,\n writeEntityKinematics,\n} from \"./kolmoStore.js\";\n\nexport interface KolmoActorProps {\n readonly position?: [number, number, number];\n readonly enabled?: boolean;\n readonly getInput?: KolmoInputGetter;\n readonly ecctrlProps?: Omit<EcctrlProps, \"ref\">;\n readonly cameraProps?: Omit<ComponentProps<typeof EcctrlCameraControls>, \"ref\"> | null | false;\n readonly children?: ReactNode;\n}\n\nexport function KolmoActor({\n position,\n enabled = true,\n getInput,\n ecctrlProps,\n cameraProps,\n children,\n}: KolmoActorProps): React.JSX.Element | null {\n const customGravity = useContext(KolmoGravityContext);\n const activeWorld = useActiveKolmoWorld();\n useLayoutEffect(() => { ensureWorldSession(activeWorld); }, [activeWorld]);\n\n const worldSpawn = activeWorld.has(WorldSpawnTrait) ? activeWorld.get(WorldSpawnTrait) : undefined;\n const hasExplicitPos = position !== undefined || ecctrlProps?.position !== undefined;\n const defaultPos: [number, number, number] = position ?? worldSpawn?.actorPos ?? [0, 1.4, 0];\n const snappedAutoSpawnRef = useRef(hasExplicitPos || !!worldSpawn);\n\n const pilotedVehEnt = useQueryFirst(VehicleTrait, PilotedTag);\n const readInput = useKolmoInput(getInput);\n const hasDom = useThree((s) => typeof s.gl?.domElement?.setAttribute === \"function\");\n\n const actorEntRef = useRef<ReturnType<typeof activeWorld.spawn> | null>(null);\n const ecctrlRef = useRef<EcctrlHandle | null>(null);\n const camRef = useRef<ActorCameraController | null>(null);\n const followPos = useRef(new THREE.Vector3());\n const followUp = useRef(new THREE.Vector3(0, 1, 0));\n const camSyncedRef = useRef(false);\n const prevInteractRef = useRef(false);\n const prevNearIdRef = useRef(\"\");\n const cooldownRef = useRef(0);\n const spawnOverrideRef = useRef<{ pos: [number, number, number] | null; yaw: number }>({ pos: null, yaw: 0 });\n const spawnTupleRef = useRef<{ pos: [number, number]; y: number; rot: [number, number, number] } | null>(null);\n const moveCmdRef = useRef({ forward: false, backward: false, leftward: false, rightward: false, run: false, jump: false });\n\n const onOriginShift = useCallback((dx: number, dz: number) => {\n followPos.current.x -= dx;\n followPos.current.z -= dz;\n if (ecctrlRef.current) { ecctrlRef.current.currPos.x -= dx; ecctrlRef.current.currPos.z -= dz; }\n shiftActorCameraOrigin(camRef.current, dx, dz);\n }, []);\n\n useLayoutEffect(() => {\n const ent = activeWorld.spawn(\n ActorTrait({ id: \"pilot\", handle: ecctrlRef.current, exitSpawnPos: null, exitSpawnYaw: 0, exitLinVel: null, onOriginShift }),\n TransformTrait,\n VelocityTrait,\n );\n actorEntRef.current = ent;\n const prePiloted = activeWorld.queryFirst(pilotedVehicleQuery);\n if (prePiloted) ent.add(PilotLink(prePiloted));\n const unsub = activeWorld.onChange(ActorTrait, (changed) => {\n if (changed !== ent) return;\n const d = ent.get(ActorTrait);\n if (d) spawnOverrideRef.current = { pos: d.exitSpawnPos, yaw: d.exitSpawnYaw };\n });\n return () => { unsub(); actorEntRef.current = null; ent.destroy(); };\n }, [activeWorld, onOriginShift]);\n\n useLayoutEffect(() => {\n const ent = actorEntRef.current;\n if (ent && pilotedVehEnt && !ent.has(PilotLink(pilotedVehEnt))) ent.add(PilotLink(pilotedVehEnt));\n }, [pilotedVehEnt]);\n\n useLayoutEffect(() => {\n if (snappedAutoSpawnRef.current || hasExplicitPos) return;\n const applySpawn = (): void => {\n if (snappedAutoSpawnRef.current) return;\n const sp = activeWorld.has(WorldSpawnTrait) ? activeWorld.get(WorldSpawnTrait) : undefined;\n const body = ecctrlRef.current?.body;\n if (sp && body && !spawnOverrideRef.current.pos) {\n snappedAutoSpawnRef.current = true;\n body.setTranslation({ x: sp.actorPos[0], y: sp.actorPos[1], z: sp.actorPos[2] }, true);\n body.setLinvel({ x: 0, y: 0, z: 0 }, true);\n body.setAngvel({ x: 0, y: 0, z: 0 }, true);\n }\n };\n applySpawn();\n const unAdd = activeWorld.onAdd(WorldSpawnTrait, applySpawn);\n const unChg = activeWorld.onChange(WorldSpawnTrait, applySpawn);\n return () => { unAdd(); unChg(); };\n }, [activeWorld, hasExplicitPos]);\n\n const assignRef = useCallback((handle: EcctrlHandle | null) => {\n ecctrlRef.current = handle;\n const ent = actorEntRef.current;\n if (!ent || !ent.isAlive()) return;\n const cur = ent.get(ActorTrait);\n if (handle?.body && cur?.exitLinVel)\n handle.body.setLinvel({ x: cur.exitLinVel[0], y: cur.exitLinVel[1], z: cur.exitLinVel[2] }, true);\n if (handle?.body) spawnOverrideRef.current.pos = null;\n ent.set(ActorTrait, {\n id: cur?.id ?? \"pilot\", handle,\n exitSpawnPos: handle?.body ? null : cur?.exitSpawnPos ?? null,\n exitSpawnYaw: cur?.exitSpawnYaw ?? 0,\n exitLinVel: handle?.body ? null : cur?.exitLinVel ?? null,\n onOriginShift,\n });\n }, [onOriginShift]);\n\n const { world: rapierWorld } = useRapier();\n const onFoot = enabled && !pilotedVehEnt;\n\n if (!onFoot) {\n spawnTupleRef.current = null;\n camSyncedRef.current = false;\n } else if (!spawnTupleRef.current) {\n const p = spawnOverrideRef.current.pos ?? (ecctrlProps?.position as [number, number, number] | undefined) ?? defaultPos;\n const r = (ecctrlProps?.rotation as [number, number, number] | undefined) ?? [0, spawnOverrideRef.current.yaw, 0];\n spawnTupleRef.current = { pos: [p[0], p[2]], y: p[1], rot: [r[0], r[1], r[2]] };\n }\n\n const mergedUserData = useMemo(() => {\n const base = (ecctrlProps?.userData ?? {}) as Record<string, unknown>;\n const ecctrlMeta = (base.ecctrl ?? {}) as Record<string, unknown>;\n return { ...base, ecctrl: { excludeVehicleRay: true, ...ecctrlMeta } };\n }, [ecctrlProps?.userData]);\n\n useFrame((state, delta) => {\n if (!enabled) return;\n const dt = rapierWorld.timestep > 0 ? rapierWorld.timestep : Math.min(Math.max(delta, 1e-4), 1 / 30);\n if (cooldownRef.current > 0) cooldownRef.current = Math.max(0, cooldownRef.current - dt);\n const inp = readInput();\n const edge = inp.interact && !prevInteractRef.current;\n prevInteractRef.current = inp.interact;\n\n if (activeWorld.queryFirst(pilotedVehicleQuery)) {\n if (edge && cooldownRef.current === 0) {\n cooldownRef.current = 0.25;\n kolmoActions(activeWorld).exitVehicle();\n }\n return;\n }\n\n const handle = ecctrlRef.current;\n if (!handle || !handle.body) return;\n const cmd = moveCmdRef.current;\n cmd.forward = inp.forward; cmd.backward = inp.backward;\n cmd.leftward = inp.left; cmd.rightward = inp.right;\n cmd.run = inp.boost; cmd.jump = inp.brake;\n handle.setMovement(cmd);\n\n const speedKmh = handle.moveSpeed * 3.6;\n if (actorEntRef.current?.isAlive())\n writeEntityKinematics(activeWorld, actorEntRef.current, handle.currPos, handle.currQuat, handle.currLinVel, speedKmh);\n kolmoTelemetry.mode = \"foot\";\n kolmoTelemetry.speedKmh = speedKmh;\n kolmoTelemetry.rpm = 0; kolmoTelemetry.gear = 0;\n kolmoTelemetry.throttle = +handle.isMoving;\n kolmoTelemetry.boost = inp.boost && handle.isMoving ? 1 : 0;\n\n const nearestId = findNearestInteractableVehicleId(activeWorld, handle.currPos);\n kolmoTelemetry.nearVehicleId = nearestId;\n const nearStr = nearestId ?? \"\";\n if (nearStr !== prevNearIdRef.current) {\n prevNearIdRef.current = nearStr;\n const curSession = activeWorld.get(WorldSessionTrait);\n activeWorld.set(WorldSessionTrait, { activeRetroPreset: curSession?.activeRetroPreset ?? \"dreamcast\", nearVehicleId: nearStr });\n }\n if (edge && nearestId && cooldownRef.current === 0) {\n cooldownRef.current = 0.25;\n kolmoActions(activeWorld).boardVehicle(nearestId);\n } else if (camRef.current && handle.currPos.lengthSq() > 0) {\n syncActorFollowCamera({ cam: camRef.current, camera: state.camera, handle, followPos: followPos.current, followUp: followUp.current, camSyncedRef, dt });\n }\n });\n\n if (!onFoot || !spawnTupleRef.current) return null;\n\n return (\n <>\n {hasDom && cameraProps !== false && cameraProps !== null && (\n <EcctrlCameraControls ref={camRef as never} makeDefault minPolarAngle={0.12} maxPolarAngle={Math.PI - 0.12} smoothTime={0.08} {...(cameraProps ?? {})} />\n )}\n <Ecctrl\n ref={assignRef} enableCustomGravity={customGravity} maxWalkVel={3.2} maxRunVel={7.2} jumpVel={5.8}\n {...ecctrlProps}\n position={[spawnTupleRef.current.pos[0], spawnTupleRef.current.y, spawnTupleRef.current.pos[1]]}\n rotation={spawnTupleRef.current.r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AAI;EAAE;EAC9F,UAAU,cAAc,QAAQ;EAChC,UAAU;EAET,UAAA,YAAY,oBAAC,kBAAD,CAAmB,CAAA;CAC5B,CAAA,CACV,EAAA,CAAA;AAEV"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
import type { World } from "koota";
|
|
3
|
+
import type { EcctrlHandle } from "@pinkaphant/ecctrl";
|
|
4
|
+
import { type KolmoLookControls } from "./kolmoTouchState.js";
|
|
5
|
+
export interface ActorCameraController extends KolmoLookControls {
|
|
6
|
+
getPosition?: (out: THREE.Vector3) => void;
|
|
7
|
+
getTarget?: (out: THREE.Vector3) => void;
|
|
8
|
+
setLookAt?: (px: number, py: number, pz: number, tx: number, ty: number, tz: number, enableTransition?: boolean) => void;
|
|
9
|
+
moveTo?: (tx: number, ty: number, tz: number, enableTransition?: boolean) => void;
|
|
10
|
+
setUp?: (up: THREE.Vector3) => void;
|
|
11
|
+
}
|
|
12
|
+
export declare function shiftActorCameraOrigin(cam: ActorCameraController | null | undefined, dx: number, dz: number): void;
|
|
13
|
+
export declare function findNearestInteractableVehicleId(activeWorld: World, actorPos: THREE.Vector3): string | null;
|
|
14
|
+
export declare function syncActorFollowCamera(params: {
|
|
15
|
+
cam: ActorCameraController;
|
|
16
|
+
camera: THREE.Camera;
|
|
17
|
+
handle: EcctrlHandle;
|
|
18
|
+
followPos: THREE.Vector3;
|
|
19
|
+
followUp: THREE.Vector3;
|
|
20
|
+
camSyncedRef: {
|
|
21
|
+
current: boolean;
|
|
22
|
+
};
|
|
23
|
+
dt: number;
|
|
24
|
+
}): void;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { applyKolmoTouchLook } from "./kolmoTouchState.js";
|
|
2
|
+
import { InteractableTrait, VehicleTrait } from "./kolmoTraits.js";
|
|
3
|
+
import * as THREE from "three";
|
|
4
|
+
// src/engine/kolmoActorHelpers.ts
|
|
5
|
+
var _seatVec = new THREE.Vector3();
|
|
6
|
+
var _camPos = new THREE.Vector3();
|
|
7
|
+
var _camTgt = new THREE.Vector3();
|
|
8
|
+
var _camFwd = new THREE.Vector3();
|
|
9
|
+
function shiftActorCameraOrigin(cam, dx, dz) {
|
|
10
|
+
if (cam?.getPosition && cam.getTarget && cam.setLookAt) {
|
|
11
|
+
cam.getPosition(_camPos);
|
|
12
|
+
cam.getTarget(_camTgt);
|
|
13
|
+
cam.setLookAt(_camPos.x - dx, _camPos.y, _camPos.z - dz, _camTgt.x - dx, _camTgt.y, _camTgt.z - dz, false);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function findNearestInteractableVehicleId(activeWorld, actorPos) {
|
|
17
|
+
let nearestId = null;
|
|
18
|
+
let bestDistSq = Infinity;
|
|
19
|
+
for (const ent of activeWorld.query(VehicleTrait, InteractableTrait)) {
|
|
20
|
+
const v = ent.get(VehicleTrait);
|
|
21
|
+
const it = ent.get(InteractableTrait);
|
|
22
|
+
if (!v?.handle || !it) continue;
|
|
23
|
+
_seatVec.set(v.seatOffset[0], v.seatOffset[1], v.seatOffset[2]).applyQuaternion(v.handle.currQuat).add(v.handle.currPos);
|
|
24
|
+
const dSq = actorPos.distanceToSquared(_seatVec);
|
|
25
|
+
if (dSq <= it.radius * it.radius && dSq < bestDistSq) {
|
|
26
|
+
bestDistSq = dSq;
|
|
27
|
+
nearestId = v.id;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return nearestId;
|
|
31
|
+
}
|
|
32
|
+
function syncActorFollowCamera(params) {
|
|
33
|
+
const { cam, camera, handle, followPos, followUp, camSyncedRef, dt } = params;
|
|
34
|
+
applyKolmoTouchLook(cam);
|
|
35
|
+
const hasValidUp = handle.upAxis.lengthSq() > .25;
|
|
36
|
+
if (hasValidUp) followUp.copy(handle.upAxis).normalize();
|
|
37
|
+
followPos.copy(handle.currPos).addScaledVector(followUp, .55);
|
|
38
|
+
if (!camSyncedRef.current) {
|
|
39
|
+
camSyncedRef.current = true;
|
|
40
|
+
if (hasValidUp) {
|
|
41
|
+
camera.up.copy(followUp);
|
|
42
|
+
cam.setUp?.(followUp);
|
|
43
|
+
}
|
|
44
|
+
_camFwd.copy(handle.bodyZAxis).projectOnPlane(followUp);
|
|
45
|
+
if (_camFwd.lengthSq() < 1e-4) {
|
|
46
|
+
_camFwd.set(0, 0, -1).projectOnPlane(followUp);
|
|
47
|
+
if (_camFwd.lengthSq() < 1e-4) _camFwd.set(1, 0, 0).projectOnPlane(followUp);
|
|
48
|
+
}
|
|
49
|
+
_camFwd.normalize();
|
|
50
|
+
const cx = followPos.x - _camFwd.x * 7.5 + followUp.x * 2.4;
|
|
51
|
+
const cy = followPos.y - _camFwd.y * 7.5 + followUp.y * 2.4;
|
|
52
|
+
const cz = followPos.z - _camFwd.z * 7.5 + followUp.z * 2.4;
|
|
53
|
+
cam.setLookAt?.(cx, cy, cz, followPos.x, followPos.y, followPos.z, false);
|
|
54
|
+
} else {
|
|
55
|
+
cam.moveTo?.(followPos.x, followPos.y, followPos.z, true);
|
|
56
|
+
if (hasValidUp) {
|
|
57
|
+
camera.up.lerp(followUp, 1 - Math.exp(-8 * dt)).normalize();
|
|
58
|
+
cam.setUp?.(camera.up);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export { findNearestInteractableVehicleId, shiftActorCameraOrigin, syncActorFollowCamera };
|
|
63
|
+
|
|
64
|
+
//# sourceMappingURL=kolmoActorHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kolmoActorHelpers.js","names":[],"sources":["../../src/engine/kolmoActorHelpers.ts"],"sourcesContent":["import * as THREE from \"three\";\nimport type { World } from \"koota\";\nimport type { EcctrlHandle } from \"@pinkaphant/ecctrl\";\nimport { applyKolmoTouchLook, type KolmoLookControls } from \"./kolmoTouchState.js\";\nimport { InteractableTrait, VehicleTrait } from \"./kolmoStore.js\";\n\nconst _seatVec = new THREE.Vector3();\nconst _camPos = new THREE.Vector3();\nconst _camTgt = new THREE.Vector3();\nconst _camFwd = new THREE.Vector3();\n\nexport interface ActorCameraController extends KolmoLookControls {\n getPosition?: (out: THREE.Vector3) => void;\n getTarget?: (out: THREE.Vector3) => void;\n setLookAt?: (\n px: number, py: number, pz: number,\n tx: number, ty: number, tz: number,\n enableTransition?: boolean,\n ) => void;\n moveTo?: (tx: number, ty: number, tz: number, enableTransition?: boolean) => void;\n setUp?: (up: THREE.Vector3) => void;\n}\n\nexport function shiftActorCameraOrigin(cam: ActorCameraController | null | undefined, dx: number, dz: number): void {\n if (cam?.getPosition && cam.getTarget && cam.setLookAt) {\n cam.getPosition(_camPos);\n cam.getTarget(_camTgt);\n cam.setLookAt(_camPos.x - dx, _camPos.y, _camPos.z - dz, _camTgt.x - dx, _camTgt.y, _camTgt.z - dz, false);\n }\n}\n\nexport function findNearestInteractableVehicleId(activeWorld: World, actorPos: THREE.Vector3): string | null {\n let nearestId: string | null = null;\n let bestDistSq = Infinity;\n for (const ent of activeWorld.query(VehicleTrait, InteractableTrait)) {\n const v = ent.get(VehicleTrait);\n const it = ent.get(InteractableTrait);\n if (!v?.handle || !it) continue;\n _seatVec.set(v.seatOffset[0], v.seatOffset[1], v.seatOffset[2]).applyQuaternion(v.handle.currQuat).add(v.handle.currPos);\n const dSq = actorPos.distanceToSquared(_seatVec);\n if (dSq <= it.radius * it.radius && dSq < bestDistSq) {\n bestDistSq = dSq;\n nearestId = v.id;\n }\n }\n return nearestId;\n}\n\nexport function syncActorFollowCamera(params: {\n cam: ActorCameraController;\n camera: THREE.Camera;\n handle: EcctrlHandle;\n followPos: THREE.Vector3;\n followUp: THREE.Vector3;\n camSyncedRef: { current: boolean };\n dt: number;\n}): void {\n const { cam, camera, handle, followPos, followUp, camSyncedRef, dt } = params;\n applyKolmoTouchLook(cam);\n const hasValidUp = handle.upAxis.lengthSq() > 0.25;\n if (hasValidUp) followUp.copy(handle.upAxis).normalize();\n followPos.copy(handle.currPos).addScaledVector(followUp, 0.55);\n\n if (!camSyncedRef.current) {\n camSyncedRef.current = true;\n if (hasValidUp) {\n camera.up.copy(followUp);\n cam.setUp?.(followUp);\n }\n _camFwd.copy(handle.bodyZAxis).projectOnPlane(followUp);\n if (_camFwd.lengthSq() < 1e-4) {\n _camFwd.set(0, 0, -1).projectOnPlane(followUp);\n if (_camFwd.lengthSq() < 1e-4) _camFwd.set(1, 0, 0).projectOnPlane(followUp);\n }\n _camFwd.normalize();\n const cx = followPos.x - _camFwd.x * 7.5 + followUp.x * 2.4;\n const cy = followPos.y - _camFwd.y * 7.5 + followUp.y * 2.4;\n const cz = followPos.z - _camFwd.z * 7.5 + followUp.z * 2.4;\n cam.setLookAt?.(cx, cy, cz, followPos.x, followPos.y, followPos.z, false);\n } else {\n cam.moveTo?.(followPos.x, followPos.y, followPos.z, true);\n if (hasValidUp) {\n camera.up.lerp(followUp, 1 - Math.exp(-8 * dt)).normalize();\n cam.setUp?.(camera.up);\n }\n }\n}\n"],"mappings":";;;;AAMA,IAAM,WAAW,IAAI,MAAM,QAAQ;AACnC,IAAM,UAAU,IAAI,MAAM,QAAQ;AAClC,IAAM,UAAU,IAAI,MAAM,QAAQ;AAClC,IAAM,UAAU,IAAI,MAAM,QAAQ;AAclC,SAAgB,uBAAuB,KAA+C,IAAY,IAAkB;CAChH,IAAI,KAAK,eAAe,IAAI,aAAa,IAAI,WAAW;EACpD,IAAI,YAAY,OAAO;EACvB,IAAI,UAAU,OAAO;EACrB,IAAI,UAAU,QAAQ,IAAI,IAAI,QAAQ,GAAG,QAAQ,IAAI,IAAI,QAAQ,IAAI,IAAI,QAAQ,GAAG,QAAQ,IAAI,IAAI,KAAK;CAC7G;AACJ;AAEA,SAAgB,iCAAiC,aAAoB,UAAwC;CACzG,IAAI,YAA2B;CAC/B,IAAI,aAAa;CACjB,KAAK,MAAM,OAAO,YAAY,MAAM,cAAc,iBAAiB,GAAG;EAClE,MAAM,IAAI,IAAI,IAAI,YAAY;EAC9B,MAAM,KAAK,IAAI,IAAI,iBAAiB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AAI,IAAI,IAAI,UAAU,GAAG,UAAU,GAAG,UAAU,GAAG,KAAK;CAC5E,OAAO;EACH,IAAI,SAAS,UAAU,GAAG,UAAU,GAAG,UAAU,GAAG,IAAI;EACxD,IAAI,YAAY;GACZ,OAAO,GAAG,KAAK,UAAU,IAAI,KAAK,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,UAAU;GAC1D,IAAI,QAAQ,OAAO,EAAE;EACzB;CACJ;AACJ"}
|
package/dist/engine/kolmoGame.js
CHANGED
|
@@ -48,8 +48,11 @@ function KolmoGame({ retro = RETRO_DREAMCAST, gravity = "planar", skybox = "day"
|
|
|
48
48
|
style: {
|
|
49
49
|
position: "relative",
|
|
50
50
|
width: "100vw",
|
|
51
|
-
height: "
|
|
51
|
+
height: "100dvh",
|
|
52
52
|
overflow: "hidden",
|
|
53
|
+
touchAction: "none",
|
|
54
|
+
userSelect: "none",
|
|
55
|
+
WebkitUserSelect: "none",
|
|
53
56
|
...style
|
|
54
57
|
},
|
|
55
58
|
children: [/* @__PURE__ */ jsx(KolmoCanvas, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kolmoGame.js","names":["_Fragment"],"sources":["../../src/engine/kolmoGame.tsx"],"sourcesContent":["import React, { useMemo, useSyncExternalStore, type CSSProperties, type ReactNode } from \"react\";\nimport { type World } from \"koota\";\nimport { WorldProvider } from \"koota/react\";\nimport { useOptionalKootaWorld, type KolmoGravitySpec, type KolmoTimeControlValue } from \"../physics/index.js\";\nimport { RETRO_DREAMCAST, type RetroPreset, type RetroPresetId } from \"../shaders/retroShader.js\";\nimport {\n KolmoHud,\n KolmoOverlayContext,\n useKolmoGame,\n type KolmoGameHookState,\n type KolmoHudProps,\n} from \"./kolmoHud.js\";\nimport { kolmoWorld } from \"./kolmoStore.js\";\nimport { KolmoCanvas, KolmoWorld } from \"./kolmoWorld.js\";\n\nexport { KolmoHud, KolmoOverlayContext, useKolmoGame, type KolmoGameHookState, type KolmoHudProps };\n\ninterface OverlayStore {\n readonly set: (id: string, node: ReactNode | null) => void;\n readonly subscribe: (listener: () => void) => () => void;\n readonly getSnapshot: () => ReadonlyArray<[string, ReactNode]>;\n}\n\nfunction createOverlayStore(): OverlayStore {\n const map = new Map<string, ReactNode>();\n const listeners = new Set<() => void>();\n let snap: ReadonlyArray<[string, ReactNode]> = [];\n return {\n set(id, node) {\n if (node === null) {\n if (!map.delete(id)) return;\n } else {\n map.set(id, node);\n }\n snap = [...map.entries()];\n for (const l of listeners) l();\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n },\n getSnapshot: () => snap,\n };\n}\n\nfunction KolmoOverlayHost({ store }: { store: OverlayStore }): React.JSX.Element | null {\n const entries = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n if (entries.length === 0) return null;\n return (\n <>\n {entries.map(([id, node]) => (\n <React.Fragment key={id}>{node}</React.Fragment>\n ))}\n </>\n );\n}\n\nexport interface KolmoGameProps {\n readonly retro?: RetroPreset | RetroPresetId;\n readonly gravity?: KolmoGravitySpec;\n readonly skybox?: \"nebula\" | \"day\" | \"none\";\n readonly lighting?: \"default\" | \"none\";\n readonly fogExp2?: [color: string, density: number] | null;\n readonly paused?: boolean;\n readonly timeScale?: KolmoTimeControlValue;\n readonly maxDelta?: KolmoTimeControlValue;\n readonly physicsDebug?: boolean;\n readonly dpr?: number | [number, number];\n readonly kootaWorld?: World;\n readonly style?: CSSProperties;\n readonly className?: string;\n readonly children?: ReactNode;\n}\n\n/**\n * Pure declarative engine shell (`<KolmoCanvas>` + `<KolmoWorld>` + `<WorldProvider>` + `<Hud>` overlay tunnel).\n * Compose `<Island />`, `<LongDrive />`, `<Surface />`, `<Vehicle />`, `<Actor />`, `<Prop />`, `<Junk />`, and `<Hud />` directly as children.\n */\nexport function KolmoGame({\n retro = RETRO_DREAMCAST,\n gravity = \"planar\",\n skybox = \"day\",\n lighting = \"default\",\n fogExp2,\n paused = false,\n timeScale = 1,\n maxDelta,\n physicsDebug = false,\n dpr,\n kootaWorld = kolmoWorld,\n style,\n className,\n children,\n}: KolmoGameProps): React.JSX.Element {\n const outerWorld = useOptionalKootaWorld();\n const store = useMemo(createOverlayStore, []);\n\n const shell = (\n <KolmoOverlayContext.Provider value={store.set}>\n <div\n className={className}\n style={{
|
|
1
|
+
{"version":3,"file":"kolmoGame.js","names":["_Fragment"],"sources":["../../src/engine/kolmoGame.tsx"],"sourcesContent":["import React, { useMemo, useSyncExternalStore, type CSSProperties, type ReactNode } from \"react\";\nimport { type World } from \"koota\";\nimport { WorldProvider } from \"koota/react\";\nimport { useOptionalKootaWorld, type KolmoGravitySpec, type KolmoTimeControlValue } from \"../physics/index.js\";\nimport { RETRO_DREAMCAST, type RetroPreset, type RetroPresetId } from \"../shaders/retroShader.js\";\nimport {\n KolmoHud,\n KolmoOverlayContext,\n useKolmoGame,\n type KolmoGameHookState,\n type KolmoHudProps,\n} from \"./kolmoHud.js\";\nimport { kolmoWorld } from \"./kolmoStore.js\";\nimport { KolmoCanvas, KolmoWorld } from \"./kolmoWorld.js\";\n\nexport { KolmoHud, KolmoOverlayContext, useKolmoGame, type KolmoGameHookState, type KolmoHudProps };\n\ninterface OverlayStore {\n readonly set: (id: string, node: ReactNode | null) => void;\n readonly subscribe: (listener: () => void) => () => void;\n readonly getSnapshot: () => ReadonlyArray<[string, ReactNode]>;\n}\n\nfunction createOverlayStore(): OverlayStore {\n const map = new Map<string, ReactNode>();\n const listeners = new Set<() => void>();\n let snap: ReadonlyArray<[string, ReactNode]> = [];\n return {\n set(id, node) {\n if (node === null) {\n if (!map.delete(id)) return;\n } else {\n map.set(id, node);\n }\n snap = [...map.entries()];\n for (const l of listeners) l();\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n },\n getSnapshot: () => snap,\n };\n}\n\nfunction KolmoOverlayHost({ store }: { store: OverlayStore }): React.JSX.Element | null {\n const entries = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n if (entries.length === 0) return null;\n return (\n <>\n {entries.map(([id, node]) => (\n <React.Fragment key={id}>{node}</React.Fragment>\n ))}\n </>\n );\n}\n\nexport interface KolmoGameProps {\n readonly retro?: RetroPreset | RetroPresetId;\n readonly gravity?: KolmoGravitySpec;\n readonly skybox?: \"nebula\" | \"day\" | \"none\";\n readonly lighting?: \"default\" | \"none\";\n readonly fogExp2?: [color: string, density: number] | null;\n readonly paused?: boolean;\n readonly timeScale?: KolmoTimeControlValue;\n readonly maxDelta?: KolmoTimeControlValue;\n readonly physicsDebug?: boolean;\n readonly dpr?: number | [number, number];\n readonly kootaWorld?: World;\n readonly style?: CSSProperties;\n readonly className?: string;\n readonly children?: ReactNode;\n}\n\n/**\n * Pure declarative engine shell (`<KolmoCanvas>` + `<KolmoWorld>` + `<WorldProvider>` + `<Hud>` overlay tunnel).\n * Compose `<Island />`, `<LongDrive />`, `<Surface />`, `<Vehicle />`, `<Actor />`, `<Prop />`, `<Junk />`, and `<Hud />` directly as children.\n */\nexport function KolmoGame({\n retro = RETRO_DREAMCAST,\n gravity = \"planar\",\n skybox = \"day\",\n lighting = \"default\",\n fogExp2,\n paused = false,\n timeScale = 1,\n maxDelta,\n physicsDebug = false,\n dpr,\n kootaWorld = kolmoWorld,\n style,\n className,\n children,\n}: KolmoGameProps): React.JSX.Element {\n const outerWorld = useOptionalKootaWorld();\n const store = useMemo(createOverlayStore, []);\n\n const shell = (\n <KolmoOverlayContext.Provider value={store.set}>\n <div\n className={className}\n style={{\n position: \"relative\", width: \"100vw\", height: \"100dvh\", overflow: \"hidden\",\n touchAction: \"none\", userSelect: \"none\", WebkitUserSelect: \"none\", ...style,\n }}\n >\n <KolmoCanvas dpr={dpr}>\n <KolmoWorld\n gravity={gravity}\n paused={paused}\n timeScale={timeScale}\n maxDelta={maxDelta}\n retro={retro}\n skybox={skybox}\n lighting={lighting}\n fogExp2={fogExp2}\n physicsDebug={physicsDebug}\n world={kootaWorld}\n >\n {children}\n </KolmoWorld>\n </KolmoCanvas>\n <KolmoOverlayHost store={store} />\n </div>\n </KolmoOverlayContext.Provider>\n );\n\n return kootaWorld !== outerWorld ? <WorldProvider world={kootaWorld}>{shell}</WorldProvider> : shell;\n}\n"],"mappings":";;;;;;;;;;AAuBA,SAAS,qBAAmC;CACxC,MAAM,sBAAM,IAAI,IAAuB;CACvC,MAAM,4BAAY,IAAI,IAAgB;CACtC,IAAI,OAA2C,CAAC;CAChD,OAAO;EACH,IAAI,IAAI,MAAM;GACV,IAAI,SAAS,MACL;QAAA,CAAC,IAAI,OAAO,EAAE,GAAG;GAAA,OAErB,IAAI,IAAI,IAAI,IAAI;GAEpB,OAAO,CAAC,GAAG,IAAI,QAAQ,CAAC;GACxB,KAAK,MAAM,KAAK,WAAW,EAAE;EACjC;EACA,UAAU,UAAU;GAChB,UAAU,IAAI,QAAQ;GACtB,aAAa;IAAE,UAAU,OAAO,QAAQ;GAAG;EAC/C;EACA,mBAAmB;CACvB;AACJ;AAEA,SAAS,iBAAiB,EAAE,SAA4D;CACpF,MAAM,UAAU,qBAAqB,MAAM,WAAW,MAAM,aAAa,MAAM,WAAW;CAC1F,IAAI,QAAQ,WAAW,GAAG,OAAO;CACjC,OACI,oBAAAA,UAAA,EAAA,UACK,QAAQ,KAAK,CAAC,IAAI,UACf,oBAAC,MAAM,UAAP,EAAA,UAA0B,KAAqB,GAA1B,EAA0B,CAClD,EACH,CAAA;AAEV;;;;;AAuBA,SAAgB,UAAU,EACtB,QAAQ,iBACR,UAAU,UACV,SAAS,OACT,WAAW,WACX,SACA,SAAS,OACT,YAAY,GACZ,UACA,eAAe,OACf,KACA,aAAa,YACb,OACA,WACA,YACkC;CAClC,MAAM,aAAa,sBAAsB;CACzC,MAAM,QAAQ,QAAQ,oBAAoB,CAAC,CAAC;CAE5C,MAAM,QACF,oBAAC,oBAAoB,UAArB;EAA8B,OAAO,MAAM;EACvC,UAAA,qBAAC,OAAD;GACe;GACX,OAAO;IACH,UAAU;IAAY,OAAO;IAAS,QAAQ;IAAU,UAAU;IAClE,aAAa;IAAQ,YAAY;IAAQ,kBAAkB;IAAQ,GAAG;GAC1E;GALJ,UAAA,CAOI,oBAAC,aAAD;IAAkB;IACd,UAAA,oBAAC,YAAD;KACa;KACD;KACG;KACD;KACH;KACC;KACE;KACD;KACK;KACd,OAAO;KAEN;IACO,CAAA;GACH,CAAA,GACb,oBAAC,kBAAD,EAAyB,MAAQ,CAAA,CAChC;;CACqB,CAAA;CAGlC,OAAO,eAAe,aAAa,oBAAC,eAAD;EAAe,OAAO;EAAa,UAAA;CAAqB,CAAA,IAAI;AACnG"}
|
package/dist/engine/kolmoHud.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { useOptionalKootaWorld } from "../physics/index.js";
|
|
2
2
|
import { RETRO_PRESETS } from "../shaders/retroUniforms.js";
|
|
3
|
+
import { detectTouchDevice } from "./kolmoTouchState.js";
|
|
3
4
|
import { ActorTrait, PilotLink, PilotedTag, VehicleTrait, WorldSessionTrait, ensureWorldSession, kolmoWorld, pilotedVehicleQuery, useActiveKolmoWorld } from "./kolmoTraits.js";
|
|
4
5
|
import { kolmoActions, kolmoTelemetry } from "./kolmoActions.js";
|
|
5
6
|
import { KolmoInCanvasContext } from "./kolmoWorld.js";
|
|
6
|
-
import { createContext, useContext, useEffect, useId, useLayoutEffect, useReducer, useRef } from "react";
|
|
7
|
+
import { createContext, useContext, useEffect, useId, useLayoutEffect, useReducer, useRef, useState } from "react";
|
|
7
8
|
import "koota";
|
|
8
9
|
import { WorldProvider, useTrait } from "koota/react";
|
|
9
10
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -21,12 +22,14 @@ function useKolmoGame(explicitWorld) {
|
|
|
21
22
|
const u3 = targetWorld.onRemove(PilotedTag, onChange);
|
|
22
23
|
const u4 = targetWorld.onAdd(PilotLink, onChange);
|
|
23
24
|
const u5 = targetWorld.onRemove(PilotLink, onChange);
|
|
25
|
+
const u6 = targetWorld.onChange(VehicleTrait, onChange);
|
|
24
26
|
return () => {
|
|
25
27
|
u1();
|
|
26
28
|
u2();
|
|
27
29
|
u3();
|
|
28
30
|
u4();
|
|
29
31
|
u5();
|
|
32
|
+
u6();
|
|
30
33
|
};
|
|
31
34
|
}, [targetWorld]);
|
|
32
35
|
const session = targetWorld.has(WorldSessionTrait) ? targetWorld.get(WorldSessionTrait) : void 0;
|
|
@@ -50,17 +53,22 @@ function KolmoHudDom({ showPresets = true, style }) {
|
|
|
50
53
|
const session = useTrait(world, WorldSessionTrait);
|
|
51
54
|
const telemetryRef = useRef(null);
|
|
52
55
|
const promptRef = useRef(null);
|
|
56
|
+
const [isTouch, setIsTouch] = useState(detectTouchDevice);
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
setIsTouch(detectTouchDevice());
|
|
59
|
+
}, []);
|
|
53
60
|
const curPreset = session?.activeRetroPreset ?? kolmoTelemetry.activeRetroPreset;
|
|
54
61
|
useEffect(() => {
|
|
55
62
|
let raf = 0, lastLine = "", lastPrompt = "";
|
|
56
63
|
const tick = () => {
|
|
57
64
|
const kmh = Math.round(kolmoTelemetry.speedKmh), rpm = Math.round(kolmoTelemetry.rpm);
|
|
58
|
-
const
|
|
65
|
+
const modeLabel = kolmoTelemetry.mode === "flight" ? "Flight" : "Car";
|
|
66
|
+
const line = kolmoTelemetry.mode === "foot" ? `On foot · ${kmh} km/h` : `${modeLabel} · ${kmh} km/h · G${kolmoTelemetry.gear} · ${rpm} rpm`;
|
|
59
67
|
if (line !== lastLine && telemetryRef.current) {
|
|
60
68
|
telemetryRef.current.textContent = line;
|
|
61
69
|
lastLine = line;
|
|
62
70
|
}
|
|
63
|
-
const prompt = kolmoTelemetry.activeVehicleId ? kolmoTelemetry.mode === "flight" ? "[WASD]
|
|
71
|
+
const prompt = kolmoTelemetry.activeVehicleId ? kolmoTelemetry.mode === "flight" ? "[WASD] Fly · [Space/X] Rise/Dive · [Shift] Boost · [G] Mode · [V] Cam · [F] Exit" : "[WASD] Drive · [Shift] Boost · [Space] Brake · [G] Mode · [V] Cam · [R] Flip · [F] Exit" : kolmoTelemetry.nearVehicleId ? "[F] Board vehicle · [WASD] Walk · [Shift] Sprint · [Space] Jump" : "[WASD] Walk · [Shift] Sprint · [Space] Jump";
|
|
64
72
|
if (prompt !== lastPrompt && promptRef.current) {
|
|
65
73
|
promptRef.current.textContent = prompt;
|
|
66
74
|
lastPrompt = prompt;
|
|
@@ -70,10 +78,16 @@ function KolmoHudDom({ showPresets = true, style }) {
|
|
|
70
78
|
raf = requestAnimationFrame(tick);
|
|
71
79
|
return () => cancelAnimationFrame(raf);
|
|
72
80
|
}, []);
|
|
81
|
+
const presetLabel = RETRO_PRESETS.find((p) => p.id === curPreset)?.label ?? curPreset;
|
|
73
82
|
const cyclePreset = () => {
|
|
74
83
|
const idx = PRESET_ORDER.indexOf(curPreset);
|
|
75
84
|
kolmoActions(world).setRetroPreset(PRESET_ORDER[(idx + 1) % PRESET_ORDER.length]);
|
|
76
85
|
};
|
|
86
|
+
const btnSize = isTouch ? {
|
|
87
|
+
minHeight: 44,
|
|
88
|
+
minWidth: 44,
|
|
89
|
+
padding: "8px 12px"
|
|
90
|
+
} : { padding: "6px 9px" };
|
|
77
91
|
return /* @__PURE__ */ jsxs("div", {
|
|
78
92
|
style: {
|
|
79
93
|
position: "fixed",
|
|
@@ -87,8 +101,9 @@ function KolmoHudDom({ showPresets = true, style }) {
|
|
|
87
101
|
children: [/* @__PURE__ */ jsxs("div", {
|
|
88
102
|
style: {
|
|
89
103
|
position: "absolute",
|
|
90
|
-
top:
|
|
91
|
-
left:
|
|
104
|
+
top: "max(10px, env(safe-area-inset-top))",
|
|
105
|
+
left: "max(12px, env(safe-area-inset-left))",
|
|
106
|
+
maxWidth: "calc(100vw - 24px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px))",
|
|
92
107
|
display: "flex",
|
|
93
108
|
flexWrap: "wrap",
|
|
94
109
|
alignItems: "center",
|
|
@@ -105,15 +120,15 @@ function KolmoHudDom({ showPresets = true, style }) {
|
|
|
105
120
|
borderRadius: 4,
|
|
106
121
|
fontSize: 12,
|
|
107
122
|
fontWeight: 700,
|
|
108
|
-
letterSpacing: "0.
|
|
123
|
+
letterSpacing: "0.02em"
|
|
109
124
|
},
|
|
110
|
-
children: "
|
|
125
|
+
children: "On foot · 0 km/h"
|
|
111
126
|
}),
|
|
112
127
|
showPresets && /* @__PURE__ */ jsx("button", {
|
|
113
128
|
type: "button",
|
|
114
129
|
onClick: cyclePreset,
|
|
115
130
|
style: {
|
|
116
|
-
|
|
131
|
+
...btnSize,
|
|
117
132
|
background: "rgba(9, 13, 22, 0.82)",
|
|
118
133
|
border: "1px solid #ffb000",
|
|
119
134
|
borderRadius: 4,
|
|
@@ -123,13 +138,15 @@ function KolmoHudDom({ showPresets = true, style }) {
|
|
|
123
138
|
fontWeight: 700,
|
|
124
139
|
cursor: "pointer"
|
|
125
140
|
},
|
|
126
|
-
children: `
|
|
141
|
+
children: `Shader: ${presetLabel}`
|
|
127
142
|
}),
|
|
128
143
|
/* @__PURE__ */ jsx("button", {
|
|
129
144
|
type: "button",
|
|
145
|
+
title: "Flip vehicle (R)",
|
|
146
|
+
"aria-label": "Flip vehicle",
|
|
130
147
|
onClick: () => kolmoActions(world).resetVehicle(),
|
|
131
148
|
style: {
|
|
132
|
-
|
|
149
|
+
...btnSize,
|
|
133
150
|
background: "rgba(9, 13, 22, 0.82)",
|
|
134
151
|
border: "1px solid #10b981",
|
|
135
152
|
borderRadius: 4,
|
|
@@ -139,25 +156,27 @@ function KolmoHudDom({ showPresets = true, style }) {
|
|
|
139
156
|
fontWeight: 700,
|
|
140
157
|
cursor: "pointer"
|
|
141
158
|
},
|
|
142
|
-
children: "
|
|
159
|
+
children: "Flip"
|
|
143
160
|
})
|
|
144
161
|
]
|
|
145
|
-
}), /* @__PURE__ */ jsx("div", {
|
|
162
|
+
}), !isTouch && /* @__PURE__ */ jsx("div", {
|
|
146
163
|
ref: promptRef,
|
|
147
164
|
style: {
|
|
148
165
|
position: "absolute",
|
|
149
|
-
bottom:
|
|
166
|
+
bottom: "max(12px, env(safe-area-inset-bottom))",
|
|
150
167
|
left: "50%",
|
|
151
168
|
transform: "translateX(-50%)",
|
|
169
|
+
maxWidth: "calc(100vw - 24px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px))",
|
|
152
170
|
padding: "5px 12px",
|
|
153
171
|
background: "rgba(9, 13, 22, 0.78)",
|
|
154
172
|
border: "1px solid #1c2632",
|
|
155
173
|
borderRadius: 4,
|
|
156
174
|
fontSize: 11,
|
|
157
175
|
color: "#94a3b8",
|
|
158
|
-
whiteSpace: "
|
|
176
|
+
whiteSpace: "normal",
|
|
177
|
+
textAlign: "center"
|
|
159
178
|
},
|
|
160
|
-
children: "[WASD]
|
|
179
|
+
children: "[WASD] Drive · [Shift] Boost · [Space] Brake · [G] Mode · [V] Cam · [R] Flip · [F] Exit"
|
|
161
180
|
})]
|
|
162
181
|
});
|
|
163
182
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kolmoHud.js","names":[],"sources":["../../src/engine/kolmoHud.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useId, useLayoutEffect, useReducer, useRef, type CSSProperties, type ReactNode } from \"react\";\nimport { type World } from \"koota\";\nimport { WorldProvider, useTrait } from \"koota/react\";\nimport { useOptionalKootaWorld } from \"../physics/index.js\";\nimport { RETRO_PRESETS, type RetroPreset, type RetroPresetId } from \"../shaders/retroShader.js\";\nimport {\n ActorTrait,\n PilotLink,\n PilotedTag,\n VehicleTrait,\n WorldSessionTrait,\n ensureWorldSession,\n kolmoActions,\n kolmoTelemetry,\n kolmoWorld,\n pilotedVehicleQuery,\n useActiveKolmoWorld,\n type KolmoControlMode,\n} from \"./kolmoStore.js\";\nimport { KolmoInCanvasContext } from \"./kolmoWorld.js\";\n\nexport type KolmoOverlayRegister = (id: string, node: ReactNode | null) => void;\nexport const KolmoOverlayContext = createContext<KolmoOverlayRegister | null>(null);\n\nexport interface KolmoGameHookState {\n readonly world: World;\n readonly mode: KolmoControlMode;\n readonly activeVehicleId: string;\n readonly nearVehicleId: string | null;\n readonly retroPreset: RetroPresetId;\n readonly boardVehicle: (id: string) => void;\n readonly exitVehicle: () => void;\n readonly setRetroPreset: (preset: RetroPreset | RetroPresetId) => void;\n readonly resetVehicle: (id?: string) => void;\n}\n\nexport function useKolmoGame(explicitWorld?: World | null): KolmoGameHookState {\n const active = useActiveKolmoWorld();\n const targetWorld = explicitWorld ?? active;\n const [, bump] = useReducer((x: number) => x + 1, 0);\n\n useEffect(() => {\n ensureWorldSession(targetWorld);\n const onChange = () => bump();\n const u1 = targetWorld.onChange(WorldSessionTrait, onChange);\n const u2 = targetWorld.onAdd(PilotedTag, onChange);\n const u3 = targetWorld.onRemove(PilotedTag, onChange);\n const u4 = targetWorld.onAdd(PilotLink, onChange);\n const u5 = targetWorld.onRemove(PilotLink, onChange);\n return () => { u1(); u2(); u3(); u4(); u5(); };\n }, [targetWorld]);\n\n const session = targetWorld.has(WorldSessionTrait) ? targetWorld.get(WorldSessionTrait) : undefined;\n const pilotedEnt = targetWorld.queryFirst(ActorTrait)?.targetFor(PilotLink) ?? targetWorld.queryFirst(pilotedVehicleQuery);\n const pilotedData = pilotedEnt?.get(VehicleTrait);\n const actions = kolmoActions(targetWorld);\n\n return {\n world: targetWorld,\n mode: pilotedData ? (pilotedData.isFlight ? \"flight\" : \"car\") : \"foot\",\n activeVehicleId: pilotedData?.id ?? \"\",\n nearVehicleId: session?.nearVehicleId || kolmoTelemetry.nearVehicleId,\n retroPreset: session?.activeRetroPreset ?? kolmoTelemetry.activeRetroPreset,\n boardVehicle: actions.boardVehicle,\n exitVehicle: actions.exitVehicle,\n setRetroPreset: actions.setRetroPreset,\n resetVehicle: actions.resetVehicle,\n };\n}\n\nexport interface KolmoHudProps {\n readonly showPresets?: boolean;\n readonly style?: CSSProperties;\n}\n\nconst PRESET_ORDER: readonly RetroPresetId[] = RETRO_PRESETS.map((p) => p.id);\n\nfunction KolmoHudDom({ showPresets = true, style }: KolmoHudProps): React.JSX.Element {\n const world = useActiveKolmoWorld();\n const session = useTrait(world, WorldSessionTrait);\n const telemetryRef = useRef<HTMLDivElement | null>(null);\n const promptRef = useRef<HTMLDivElement | null>(null);\n const curPreset = session?.activeRetroPreset ?? kolmoTelemetry.activeRetroPreset;\n\n useEffect(() => {\n let raf = 0, lastLine = \"\", lastPrompt = \"\";\n const tick = (): void => {\n const kmh = Math.round(kolmoTelemetry.speedKmh), rpm = Math.round(kolmoTelemetry.rpm);\n const line = kolmoTelemetry.mode === \"foot\"\n ? `ON FOOT · ${kmh} KM/H`\n : `${kolmoTelemetry.mode.toUpperCase()} · ${kmh} KM/H · G${kolmoTelemetry.gear} · ${rpm} RPM`;\n if (line !== lastLine && telemetryRef.current) { telemetryRef.current.textContent = line; lastLine = line; }\n const prompt = kolmoTelemetry.activeVehicleId\n ? kolmoTelemetry.mode === \"flight\"\n ? \"[WASD] FLY [SPACE/X] RISE/DIVE [SHIFT] BOOST [G] MODE [V] CAM [F] EXIT\"\n : \"[WASD] DRIVE [SHIFT] BOOST [SPACE] BRAKE [G] MODE [V] CAM [R] FLIP [F] EXIT\"\n : kolmoTelemetry.nearVehicleId\n ? \"[F] BOARD VEHICLE · [WASD] WALK [SHIFT] SPRINT [SPACE] JUMP\"\n : \"[WASD] WALK [SHIFT] SPRINT [SPACE] JUMP\";\n if (prompt !== lastPrompt && promptRef.current) { promptRef.current.textContent = prompt; lastPrompt = prompt; }\n raf = requestAnimationFrame(tick);\n };\n raf = requestAnimationFrame(tick);\n return () => cancelAnimationFrame(raf);\n }, []);\n\n const cyclePreset = (): void => {\n const idx = PRESET_ORDER.indexOf(curPreset);\n kolmoActions(world).setRetroPreset(PRESET_ORDER[(idx + 1) % PRESET_ORDER.length]);\n };\n\n return (\n <div\n style={{\n position: \"fixed\", inset: 0, pointerEvents: \"none\", zIndex: 30,\n fontFamily: \"ui-monospace, SFMono-Regular, Menlo, monospace\", color: \"#e6edf3\", ...style,\n }}\n >\n <div style={{ position: \"absolute\", top: 12, left: 12, display: \"flex\", flexWrap: \"wrap\", alignItems: \"center\", gap: 8, pointerEvents: \"auto\" }}>\n <div\n ref={telemetryRef}\n style={{\n padding: \"6px 10px\", background: \"rgba(9, 13, 22, 0.82)\", border: \"1px solid #00e5ff\",\n borderRadius: 4, fontSize: 12, fontWeight: 700, letterSpacing: \"0.04em\",\n }}\n >\n ON FOOT · 0 KM/H\n </div>\n {showPresets && (\n <button\n type=\"button\" onClick={cyclePreset}\n style={{\n padding: \"6px 9px\", background: \"rgba(9, 13, 22, 0.82)\", border: \"1px solid #ffb000\",\n borderRadius: 4, color: \"#ffb000\", fontFamily: \"inherit\", fontSize: 11, fontWeight: 700, cursor: \"pointer\",\n }}\n >\n {`SHADER: ${curPreset.toUpperCase()}`}\n </button>\n )}\n <button\n type=\"button\" onClick={() => kolmoActions(world).resetVehicle()}\n style={{\n padding: \"6px 9px\", background: \"rgba(9, 13, 22, 0.82)\", border: \"1px solid #10b981\",\n borderRadius: 4, color: \"#10b981\", fontFamily: \"inherit\", fontSize: 11, fontWeight: 700, cursor: \"pointer\",\n }}\n >\n [R] FLIP\n </button>\n </div>\n <div\n ref={promptRef}\n style={{\n position: \"absolute\", bottom: 12, left: \"50%\", transform: \"translateX(-50%)\",\n padding: \"5px 12px\", background: \"rgba(9, 13, 22, 0.78)\", border: \"1px solid #1c2632\",\n borderRadius: 4, fontSize: 11, color: \"#94a3b8\", whiteSpace: \"nowrap\",\n }}\n >\n [WASD] MOVE [SHIFT] BOOST [F] ENTER/EXIT\n </div>\n </div>\n );\n}\n\nexport function KolmoHud({ showPresets = true, style }: KolmoHudProps): React.JSX.Element | null {\n const hudId = useId();\n const inCanvas = useContext(KolmoInCanvasContext);\n const registerOverlay = useContext(KolmoOverlayContext);\n const outerWorld = useOptionalKootaWorld();\n\n useLayoutEffect(() => {\n if (!inCanvas || !registerOverlay) return;\n registerOverlay(hudId, <KolmoHudDom showPresets={showPresets} style={style} />);\n return () => registerOverlay(hudId, null);\n }, [inCanvas, registerOverlay, hudId, showPresets, style]);\n\n if (inCanvas) return null;\n const node = <KolmoHudDom showPresets={showPresets} style={style} />;\n return outerWorld ? node : <WorldProvider world={kolmoWorld}>{node}</WorldProvider>;\n}\n"],"mappings":";;;;;;;;;;AAsBA,IAAa,sBAAsB,cAA2C,IAAI;AAclF,SAAgB,aAAa,eAAkD;CAC3E,MAAM,SAAS,oBAAoB;CACnC,MAAM,cAAc,iBAAiB;CACrC,MAAM,GAAG,QAAQ,YAAY,MAAc,IAAI,GAAG,CAAC;CAEnD,gBAAgB;EACZ,mBAAmB,WAAW;EAC9B,MAAM,iBAAiB,KAAK;EAC5B,MAAM,KAAK,YAAY,SAAS,mBAAmB,QAAQ;EAC3D,MAAM,KAAK,YAAY,MAAM,YAAY,QAAQ;EACjD,MAAM,KAAK,YAAY,SAAS,YAAY,QAAQ;EACpD,MAAM,KAAK,YAAY,MAAM,WAAW,QAAQ;EAChD,MAAM,KAAK,YAAY,SAAS,WAAW,QAAQ;EACnD,aAAa;GAAE,GAAG;GAAG,GAAG;GAAG,GAAG;GAAG,GAAG;GAAG,GAAG;EAAG;CACjD,GAAG,CAAC,WAAW,CAAC;CAEhB,MAAM,UAAU,YAAY,IAAI,iBAAiB,IAAI,YAAY,IAAI,iBAAiB,IAAI,KAAA;CAE1F,MAAM,eADa,YAAY,WAAW,UAAU,CAAC,EAAE,UAAU,SAAS,KAAK,YAAY,WAAW,mBAAmB,EAAA,EACzF,IAAI,YAAY;CAChD,MAAM,UAAU,aAAa,WAAW;CAExC,OAAO;EACH,OAAO;EACP,MAAM,cAAe,YAAY,WAAW,WAAW,QAAS;EAChE,iBAAiB,aAAa,MAAM;EACpC,eAAe,SAAS,iBAAiB,eAAe;EACxD,aAAa,SAAS,qBAAqB,eAAe;EAC1D,cAAc,QAAQ;EACtB,aAAa,QAAQ;EACrB,gBAAgB,QAAQ;EACxB,cAAc,QAA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aAAa,gBAAgB,OAAO,IAAI;CAC5C,GAAG;EAAC;EAAU;EAAiB;EAAO;EAAa;CAAK,CAAC;CAEzD,IAAI,UAAU,OAAO;CACrB,MAAM,OAAO,oBAAC,aAAD;EAA0B;EAAoB;CAAQ,CAAA;CACnE,OAAO,aAAa,OAAO,oBAAC,eAAD;EAAe,OAAO;EAAa,UAAA;CAAoB,CAAA;AACtF"}
|
|
1
|
+
{"version":3,"file":"kolmoHud.js","names":[],"sources":["../../src/engine/kolmoHud.tsx"],"sourcesContent":["import React, { createContext, useContext, useEffect, useId, useLayoutEffect, useReducer, useRef, useState, type CSSProperties, type ReactNode } from \"react\";\nimport { type World } from \"koota\";\nimport { WorldProvider, useTrait } from \"koota/react\";\nimport { useOptionalKootaWorld } from \"../physics/index.js\";\nimport { RETRO_PRESETS, type RetroPreset, type RetroPresetId } from \"../shaders/retroShader.js\";\nimport { detectTouchDevice } from \"./kolmoTouchState.js\";\nimport {\n ActorTrait,\n PilotLink,\n PilotedTag,\n VehicleTrait,\n WorldSessionTrait,\n ensureWorldSession,\n kolmoActions,\n kolmoTelemetry,\n kolmoWorld,\n pilotedVehicleQuery,\n useActiveKolmoWorld,\n type KolmoControlMode,\n} from \"./kolmoStore.js\";\nimport { KolmoInCanvasContext } from \"./kolmoWorld.js\";\n\nexport type KolmoOverlayRegister = (id: string, node: ReactNode | null) => void;\nexport const KolmoOverlayContext = createContext<KolmoOverlayRegister | null>(null);\n\nexport interface KolmoGameHookState {\n readonly world: World;\n readonly mode: KolmoControlMode;\n readonly activeVehicleId: string;\n readonly nearVehicleId: string | null;\n readonly retroPreset: RetroPresetId;\n readonly boardVehicle: (id: string) => void;\n readonly exitVehicle: () => void;\n readonly setRetroPreset: (preset: RetroPreset | RetroPresetId) => void;\n readonly resetVehicle: (id?: string) => void;\n}\n\nexport function useKolmoGame(explicitWorld?: World | null): KolmoGameHookState {\n const active = useActiveKolmoWorld();\n const targetWorld = explicitWorld ?? active;\n const [, bump] = useReducer((x: number) => x + 1, 0);\n\n useEffect(() => {\n ensureWorldSession(targetWorld);\n const onChange = () => bump();\n const u1 = targetWorld.onChange(WorldSessionTrait, onChange);\n const u2 = targetWorld.onAdd(PilotedTag, onChange);\n const u3 = targetWorld.onRemove(PilotedTag, onChange);\n const u4 = targetWorld.onAdd(PilotLink, onChange);\n const u5 = targetWorld.onRemove(PilotLink, onChange);\n const u6 = targetWorld.onChange(VehicleTrait, onChange);\n return () => { u1(); u2(); u3(); u4(); u5(); u6(); };\n }, [targetWorld]);\n\n const session = targetWorld.has(WorldSessionTrait) ? targetWorld.get(WorldSessionTrait) : undefined;\n const pilotedEnt = targetWorld.queryFirst(ActorTrait)?.targetFor(PilotLink) ?? targetWorld.queryFirst(pilotedVehicleQuery);\n const pilotedData = pilotedEnt?.get(VehicleTrait);\n const actions = kolmoActions(targetWorld);\n\n return {\n world: targetWorld,\n mode: pilotedData ? (pilotedData.isFlight ? \"flight\" : \"car\") : \"foot\",\n activeVehicleId: pilotedData?.id ?? \"\",\n nearVehicleId: session?.nearVehicleId || kolmoTelemetry.nearVehicleId,\n retroPreset: session?.activeRetroPreset ?? kolmoTelemetry.activeRetroPreset,\n boardVehicle: actions.boardVehicle,\n exitVehicle: actions.exitVehicle,\n setRetroPreset: actions.setRetroPreset,\n resetVehicle: actions.resetVehicle,\n };\n}\n\nexport interface KolmoHudProps {\n readonly showPresets?: boolean;\n readonly style?: CSSProperties;\n}\n\nconst PRESET_ORDER: readonly RetroPresetId[] = RETRO_PRESETS.map((p) => p.id);\n\nfunction KolmoHudDom({ showPresets = true, style }: KolmoHudProps): React.JSX.Element {\n const world = useActiveKolmoWorld();\n const session = useTrait(world, WorldSessionTrait);\n const telemetryRef = useRef<HTMLDivElement | null>(null);\n const promptRef = useRef<HTMLDivElement | null>(null);\n const [isTouch, setIsTouch] = useState(detectTouchDevice);\n useEffect(() => { setIsTouch(detectTouchDevice()); }, []);\n const curPreset = session?.activeRetroPreset ?? kolmoTelemetry.activeRetroPreset;\n\n useEffect(() => {\n let raf = 0, lastLine = \"\", lastPrompt = \"\";\n const tick = (): void => {\n const kmh = Math.round(kolmoTelemetry.speedKmh), rpm = Math.round(kolmoTelemetry.rpm);\n const modeLabel = kolmoTelemetry.mode === \"flight\" ? \"Flight\" : \"Car\";\n const line = kolmoTelemetry.mode === \"foot\"\n ? `On foot · ${kmh} km/h`\n : `${modeLabel} · ${kmh} km/h · G${kolmoTelemetry.gear} · ${rpm} rpm`;\n if (line !== lastLine && telemetryRef.current) { telemetryRef.current.textContent = line; lastLine = line; }\n const prompt = kolmoTelemetry.activeVehicleId\n ? kolmoTelemetry.mode === \"flight\"\n ? \"[WASD] Fly · [Space/X] Rise/Dive · [Shift] Boost · [G] Mode · [V] Cam · [F] Exit\"\n : \"[WASD] Drive · [Shift] Boost · [Space] Brake · [G] Mode · [V] Cam · [R] Flip · [F] Exit\"\n : kolmoTelemetry.nearVehicleId\n ? \"[F] Board vehicle · [WASD] Walk · [Shift] Sprint · [Space] Jump\"\n : \"[WASD] Walk · [Shift] Sprint · [Space] Jump\";\n if (prompt !== lastPrompt && promptRef.current) { promptRef.current.textContent = prompt; lastPrompt = prompt; }\n raf = requestAnimationFrame(tick);\n };\n raf = requestAnimationFrame(tick);\n return () => cancelAnimationFrame(raf);\n }, []);\n\n const presetLabel = RETRO_PRESETS.find((p) => p.id === curPreset)?.label ?? curPreset;\n const cyclePreset = (): void => {\n const idx = PRESET_ORDER.indexOf(curPreset);\n kolmoActions(world).setRetroPreset(PRESET_ORDER[(idx + 1) % PRESET_ORDER.length]);\n };\n const btnSize = isTouch ? { minHeight: 44, minWidth: 44, padding: \"8px 12px\" } : { padding: \"6px 9px\" };\n\n return (\n <div\n style={{\n position: \"fixed\", inset: 0, pointerEvents: \"none\", zIndex: 30,\n fontFamily: \"ui-monospace, SFMono-Regular, Menlo, monospace\", color: \"#e6edf3\", ...style,\n }}\n >\n <div style={{\n position: \"absolute\", top: \"max(10px, env(safe-area-inset-top))\", left: \"max(12px, env(safe-area-inset-left))\",\n maxWidth: \"calc(100vw - 24px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px))\",\n display: \"flex\", flexWrap: \"wrap\", alignItems: \"center\", gap: 8, pointerEvents: \"auto\",\n }}>\n <div\n ref={telemetryRef}\n style={{\n padding: \"6px 10px\", background: \"rgba(9, 13, 22, 0.82)\", border: \"1px solid #00e5ff\",\n borderRadius: 4, fontSize: 12, fontWeight: 700, letterSpacing: \"0.02em\",\n }}\n >\n On foot · 0 km/h\n </div>\n {showPresets && (\n <button\n type=\"button\" onClick={cyclePreset}\n style={{\n ...btnSize, background: \"rgba(9, 13, 22, 0.82)\", border: \"1px solid #ffb000\",\n borderRadius: 4, color: \"#ffb000\", fontFamily: \"inherit\", fontSize: 11, fontWeight: 700, cursor: \"pointer\",\n }}\n >\n {`Shader: ${presetLabel}`}\n </button>\n )}\n <button\n type=\"button\" title=\"Flip vehicle (R)\" aria-label=\"Flip vehicle\"\n onClick={() => kolmoActions(world).resetVehicle()}\n style={{\n ...btnSize, background: \"rgba(9, 13, 22, 0.82)\", border: \"1px solid #10b981\",\n borderRadius: 4, color: \"#10b981\", fontFamily: \"inherit\", fontSize: 11, fontWeight: 700, cursor: \"pointer\",\n }}\n >\n Flip\n </button>\n </div>\n {!isTouch && (\n <div\n ref={promptRef}\n style={{\n position: \"absolute\", bottom: \"max(12px, env(safe-area-inset-bottom))\", left: \"50%\", transform: \"translateX(-50%)\",\n maxWidth: \"calc(100vw - 24px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px))\",\n padding: \"5px 12px\", background: \"rgba(9, 13, 22, 0.78)\", border: \"1px solid #1c2632\",\n borderRadius: 4, fontSize: 11, color: \"#94a3b8\", whiteSpace: \"normal\", textAlign: \"center\",\n }}\n >\n [WASD] Drive · [Shift] Boost · [Space] Brake · [G] Mode · [V] Cam · [R] Flip · [F] Exit\n </div>\n )}\n </div>\n );\n}\n\nexport function KolmoHud({ showPresets = true, style }: KolmoHudProps): React.JSX.Element | null {\n const hudId = useId();\n const inCanvas = useContext(KolmoInCanvasContext);\n const registerOverlay = useContext(KolmoOverlayContext);\n const outerWorld = useOptionalKootaWorld();\n\n useLayoutEffect(() => {\n if (!inCanvas || !registerOverlay) return;\n registerOverlay(hudId, <KolmoHudDom showPresets={showPresets} style={style} />);\n return () => registerOverlay(hudId, null);\n }, [inCanvas, registerOverlay, hudId, showPresets, style]);\n\n if (inCanvas) return null;\n const node = <KolmoHudDom showPresets={showPresets} style={style} />;\n return outerWorld ? node : <WorldProvider world={kolmoWorld}>{node}</WorldProvider>;\n}\n"],"mappings":";;;;;;;;;;;AAuBA,IAAa,sBAAsB,cAA2C,IAAI;AAclF,SAAgB,aAAa,eAAkD;CAC3E,MAAM,SAAS,oBAAoB;CACnC,MAAM,cAAc,iBAAiB;CACrC,MAAM,GAAG,QAAQ,YAAY,MAAc,IAAI,GAAG,CAAC;CAEnD,gBAAgB;EACZ,mBAAmB,WAAW;EAC9B,MAAM,iBAAiB,KAAK;EAC5B,MAAM,KAAK,YAAY,SAAS,mBAAmB,QAAQ;EAC3D,MAAM,KAAK,YAAY,MAAM,YAAY,QAAQ;EACjD,MAAM,KAAK,YAAY,SAAS,YAAY,QAAQ;EACpD,MAAM,KAAK,YAAY,MAAM,WAAW,QAAQ;EAChD,MAAM,KAAK,YAAY,SAAS,WAAW,QAAQ;EACnD,MAAM,KAAK,YAAY,SAAS,cAAc,QAAQ;EACtD,aAAa;GAAE,GAAG;GAAG,GAAG;GAAG,GAAG;GAAG,GAAG;GAAG,GAAG;GAAG,GAAG;EAAG;CACvD,GAAG,CAAC,WAAW,CAAC;CAEhB,MAAM,UAAU,YAAY,IAAI,iBAAiB,IAAI,YAAY,IAAI,iBAAiB,IAAI,KAAA;CAE1F,MAAM,eADa,YAAY,WAAW,UAAU,CAAC,EAAE,UAAU,SAAS,KAAK,YAAY,WAAW,mBAAmB,EAAA,EACzF,IAAI,YAAY;CAChD,MAAM,UAAU,aAAa,WAAW;CAExC,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A;IAEO,CAAA;GACP;EACJ,CAAA,GAAA,CAAC,WACE,oBAAC,OAAD;GACI,KAAK;GACL,OAAO;IACH,UAAU;IAAY,QAAQ;IAA0C,MAAM;IAAO,WAAW;IAChG,UAAU;IACV,SAAS;IAAY,YAAY;IAAyB,QAAQ;IAClE,cAAc;IAAG,UAAU;IAAI,OAAO;IAAW,YAAY;IAAU,WAAW;GACtF;GACH,UAAA;EAEI,CAAA,CAER;;AAEb;AAEA,SAAgB,SAAS,EAAE,cAAc,MAAM,SAAkD;CAC7F,MAAM,QAAQ,MAAM;CACpB,MAAM,WAAW,WAAW,oBAAoB;CAChD,MAAM,kBAAkB,WAAW,mBAAmB;CACtD,MAAM,aAAa,sBAAsB;CAEzC,sBAAsB;EAClB,IAAI,CAAC,YAAY,CAAC,iBAAiB;EACnC,gBAAgB,OAAO,oBAAC,aAAD;GAA0B;GAAoB;EAAQ,CAAA,CAAC;EAC9E,aAAa,gBAAgB,OAAO,IAAI;CAC5C,GAAG;EAAC;EAAU;EAAiB;EAAO;EAAa;CAAK,CAAC;CAEzD,IAAI,UAAU,OAAO;CACrB,MAAM,OAAO,oBAAC,aAAD;EAA0B;EAAoB;CAAQ,CAAA;CACnE,OAAO,aAAa,OAAO,oBAAC,eAAD;EAAe,OAAO;EAAa,UAAA;CAAoB,CAAA;AACtF"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import React, { type ReactNode } from "react";
|
|
2
2
|
import { type KeyboardControlsEntry } from "@react-three/drei";
|
|
3
|
+
import { applyKolmoTouchLook } from "./kolmoTouchState.js";
|
|
4
|
+
export { applyKolmoTouchLook };
|
|
3
5
|
export declare const KolmoKeyboardContext: React.Context<(() => Record<string, boolean>) | null>;
|
|
4
6
|
export declare function KolmoKeyboardBridge({ children }: {
|
|
5
7
|
readonly children: ReactNode;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { kolmoTouchInput } from "./kolmoTouchState.js";
|
|
1
2
|
import React, { createContext, useCallback, useContext, useRef } from "react";
|
|
2
3
|
import { useKeyboardControls } from "@react-three/drei";
|
|
3
4
|
// src/engine/kolmoInput.ts
|
|
@@ -139,22 +140,21 @@ function useKolmoInput(getInput) {
|
|
|
139
140
|
const outRef = useRef({ ...EMPTY_KOLMO_INPUT });
|
|
140
141
|
return useCallback(() => {
|
|
141
142
|
const custom = getterRef.current?.();
|
|
142
|
-
if (!getKeyboard && !custom) return EMPTY_KOLMO_INPUT;
|
|
143
143
|
const k = getKeyboard ? getKeyboard() : EMPTY_KOLMO_INPUT;
|
|
144
144
|
const out = outRef.current;
|
|
145
|
-
out.forward = Boolean(custom?.forward ?? (k.forward || k.W || k.Up));
|
|
146
|
-
out.backward = Boolean(custom?.backward ?? (k.backward || k.S || k.Down));
|
|
147
|
-
out.left = Boolean(custom?.left ?? (k.left || k.A || k.Left));
|
|
148
|
-
out.right = Boolean(custom?.right ?? (k.right || k.D || k.Right));
|
|
149
|
-
out.brake = Boolean(custom?.brake ?? (k.brake || k.Space));
|
|
150
|
-
out.boost = Boolean(custom?.boost ?? (k.boost || k.Shift));
|
|
151
|
-
out.interact = Boolean(custom?.interact ?? (k.interact || k.F));
|
|
145
|
+
out.forward = Boolean(custom?.forward ?? (k.forward || k.W || k.Up || kolmoTouchInput.forward));
|
|
146
|
+
out.backward = Boolean(custom?.backward ?? (k.backward || k.S || k.Down || kolmoTouchInput.backward));
|
|
147
|
+
out.left = Boolean(custom?.left ?? (k.left || k.A || k.Left || kolmoTouchInput.left));
|
|
148
|
+
out.right = Boolean(custom?.right ?? (k.right || k.D || k.Right || kolmoTouchInput.right));
|
|
149
|
+
out.brake = Boolean(custom?.brake ?? (k.brake || k.Space || kolmoTouchInput.brake));
|
|
150
|
+
out.boost = Boolean(custom?.boost ?? (k.boost || k.Shift || kolmoTouchInput.boost));
|
|
151
|
+
out.interact = Boolean(custom?.interact ?? (k.interact || k.F || kolmoTouchInput.interact));
|
|
152
152
|
out.turnLeft = Boolean(custom?.turnLeft ?? k.turnLeft);
|
|
153
153
|
out.turnRight = Boolean(custom?.turnRight ?? k.turnRight);
|
|
154
|
-
out.descend = Boolean(custom?.descend ?? k.descend);
|
|
155
|
-
out.cameraView = Boolean(custom?.cameraView ?? k.cameraView);
|
|
156
|
-
out.reset = Boolean(custom?.reset ?? (k.reset || k.R));
|
|
157
|
-
out.modeToggle = Boolean(custom?.modeToggle ?? k.modeToggle);
|
|
154
|
+
out.descend = Boolean(custom?.descend ?? (k.descend || kolmoTouchInput.descend));
|
|
155
|
+
out.cameraView = Boolean(custom?.cameraView ?? (k.cameraView || kolmoTouchInput.cameraView));
|
|
156
|
+
out.reset = Boolean(custom?.reset ?? (k.reset || k.R || kolmoTouchInput.reset));
|
|
157
|
+
out.modeToggle = Boolean(custom?.modeToggle ?? (k.modeToggle || kolmoTouchInput.modeToggle));
|
|
158
158
|
return out;
|
|
159
159
|
}, [getKeyboard]);
|
|
160
160
|
}
|