Skip to content

Commit

Permalink
Enable waypoint hash navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Sep 15, 2023
1 parent 8653fdd commit e4ca169
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/bit-systems/waypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, {
Expand All @@ -112,6 +116,7 @@ function* moveToSpawnPointJob(world: HubsWorld, characterController: CharacterCo
snapToNavMesh: true,
willMaintainInitialOrientation: false
});
initialSpawnHappened = true;
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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]) {
Expand Down

0 comments on commit e4ca169

Please sign in to comment.