-
Notifications
You must be signed in to change notification settings - Fork 209
/
universe.js
87 lines (76 loc) · 2.29 KB
/
universe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
this file contains the multiplayer code.
*/
import * as THREE from 'three';
import * as Z from 'zjs';
import {world} from './world.js';
import physicsManager from './physics-manager.js';
import {initialPosY} from './constants.js';
import {parseQuery, parseCoord} from './util.js';
import metaversefile from 'metaversefile';
import sceneNames from './scenes/scenes.json';
let currentWorld = null;
const getWorldsHost = () => window.location.protocol + '//' + window.location.hostname + ':' +
((window.location.port ? parseInt(window.location.port, 10) : (window.location.protocol === 'https:' ? 443 : 80)) + 1) + '/worlds/';
const enterWorld = async worldSpec => {
world.disconnectRoom();
const localPlayer = metaversefile.useLocalPlayer();
/* localPlayer.teleportTo(new THREE.Vector3(0, 1.5, 0), camera.quaternion, {
relation: 'float',
}); */
localPlayer.position.set(0, initialPosY, 0);
localPlayer.resetPhysics();
localPlayer.updateMatrixWorld();
physicsManager.setPhysicsEnabled(true);
localPlayer.updatePhysics(0, 0);
physicsManager.setPhysicsEnabled(false);
const _doLoad = async () => {
// world.clear();
const promises = [];
const {src, room} = worldSpec;
if (!room) {
const state = new Z.Doc();
world.connectState(state);
if (src === undefined) {
promises.push(metaversefile.load('./scenes/' + sceneNames[0]));
} else if (src === '') {
// nothing
} else {
promises.push(metaversefile.load(src));
}
} else {
const p = (async () => {
const roomUrl = getWorldsHost() + room;
await world.connectRoom(roomUrl);
})();
promises.push(p);
}
await Promise.all(promises);
};
await _doLoad().catch(err => {
console.warn(err);
});
localPlayer.resetPhysics();
physicsManager.setPhysicsEnabled(true);
localPlayer.updatePhysics(0, 0);
currentWorld = worldSpec;
};
const reload = async () => {
await enterWorld(currentWorld);
};
const pushUrl = async u => {
history.pushState({}, '', u);
window.dispatchEvent(new MessageEvent('pushstate'));
await handleUrlUpdate();
};
const handleUrlUpdate = async () => {
const q = parseQuery(location.search);
await enterWorld(q);
};
export {
enterWorld,
reload,
getWorldsHost,
pushUrl,
handleUrlUpdate,
};