From e4ca1695f0fc77845619d854674b19569ddc8b63 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Fri, 15 Sep 2023 13:50:29 +0200 Subject: [PATCH] Enable waypoint hash navigation --- src/bit-systems/waypoint.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/bit-systems/waypoint.ts b/src/bit-systems/waypoint.ts index 4896a2fdcd..f86f588b29 100644 --- a/src/bit-systems/waypoint.ts +++ b/src/bit-systems/waypoint.ts @@ -91,7 +91,10 @@ function* trySpawnIntoOccupiable(world: HubsWorld, characterController: Characte if (!spawnPoints.length) return false; const waypoint = spawnPoints[Math.floor(Math.random() * spawnPoints.length)]; - if (yield* tryOccupyAndSpawn(world, characterController, waypoint)) return true; + if (yield* tryOccupyAndSpawn(world, characterController, waypoint)) { + initialSpawnHappened = true; + return true; + } } return false; @@ -104,6 +107,7 @@ function* moveToSpawnPointJob(world: HubsWorld, characterController: CharacterCo if (spawnPoints.length) { const waypoint = spawnPoints[Math.floor(Math.random() * spawnPoints.length)]; moveToWaypoint(world, waypoint, characterController, true); + initialSpawnHappened = true; } else { console.warn("Could not find any available spawn points, spawning at the origin."); characterController.enqueueWaypointTravelTo(new Matrix4().identity(), true, { @@ -112,6 +116,7 @@ function* moveToSpawnPointJob(world: HubsWorld, characterController: CharacterCo snapToNavMesh: true, willMaintainInitialOrientation: false }); + initialSpawnHappened = true; } } @@ -147,6 +152,8 @@ const hoveredRightWaypointQuery = defineQuery([Waypoint, HoveredRemoteRight]); const exitedOwnedQuery = exitQuery(defineQuery([Owned])); let preview: Object3D | null; +let initialSpawnHappened: boolean = false; +let previousWaypointHash: string | null = null; export function waypointSystem( world: HubsWorld, characterController: CharacterControllerSystem, @@ -164,6 +171,16 @@ export function waypointSystem( } } + const hashUpdated = window.location.hash !== "" && previousWaypointHash !== window.location.hash; + if (hashUpdated && initialSpawnHappened) { + const waypointName = window.location.hash.replace("#", ""); + if (eid) { + moveToWaypoint(world, eid, characterController, previousWaypointHash === null); + window.history.replaceState(null, "", window.location.href.split("#")[0]); // Reset so you can re-activate the same waypoint + } + previousWaypointHash = window.location.hash; + } + if (hasComponent(world, Interacted, eid)) { if (hasComponent(world, NetworkedWaypoint, eid)) { if (NetworkedWaypoint.occupied[eid]) {