Skip to content

Commit

Permalink
Move named waypoint search look to its own
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Sep 27, 2023
1 parent c60c6cb commit b179702
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/bit-systems/waypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ export function waypointSystem(
myOccupiedWaypoint = 0;
}

// When a scene is opened with a named waypoint we have to make sure that the scene default waypoint
// doesn't override it and that we correctly spawn in the named waypoint from the url.
// https://github.com/mozilla/hubs/issues/2833
const hashUpdated = window.location.hash !== "" && previousWaypointHash !== window.location.hash;
const waypointName = window.location.hash.replace("#", "");
if (hashUpdated && initialSpawnHappened) {
waypointQuery(world).forEach(eid => {
const waypointObj = world.eid2obj.get(eid)!;
if (waypointObj.name === waypointName) {
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;
}
});
}

waypointQuery(world).forEach(eid => {
if (hasComponent(world, NetworkedWaypoint, eid) && hasComponent(world, Owned, eid) && eid !== myOccupiedWaypoint) {
// Inherited this waypoint, clear its occupied state
Expand All @@ -170,20 +186,6 @@ export function waypointSystem(
}
}

// When a scene is opened with a named waypoint we have to make sure that the scene default waypoint
// doesn't override it and that we correctly spawn in the named waypoint from the url.
// https://github.com/mozilla/hubs/issues/2833
const hashUpdated = window.location.hash !== "" && previousWaypointHash !== window.location.hash;
if (hashUpdated && initialSpawnHappened) {
const waypointName = window.location.hash.replace("#", "");
const waypointObj = world.eid2obj.get(eid);
if (waypointObj && waypointObj.name === waypointName) {
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 b179702

Please sign in to comment.