77 lines
2.2 KiB
TypeScript
77 lines
2.2 KiB
TypeScript
import { Dataspace, Embedded, Reader, Ref, Schemas, Sturdy } from "@syndicate-lang/core";
|
|
import * as html from "@syndicate-lang/html";
|
|
import * as wsRelay from "@syndicate-lang/ws-relay";
|
|
import * as wakeDetector from './wake-detector.js';
|
|
|
|
import {
|
|
Engine,
|
|
HemisphericLight,
|
|
Mesh,
|
|
MeshBuilder,
|
|
Scene,
|
|
Vector3,
|
|
} from '@babylonjs/core/Legacy/legacy';
|
|
|
|
import { box } from './shapes.js';
|
|
import { attachTexture } from './interiors.js';
|
|
import { startEngine } from './engine.js';
|
|
import { uuid } from './uuid.js';
|
|
|
|
async function createScene(_canvas: HTMLCanvasElement, engine: Engine): Promise<{
|
|
scene: Scene,
|
|
floorMeshes: Mesh[]
|
|
}> {
|
|
const scene = new Scene(engine);
|
|
|
|
new HemisphericLight("light", new Vector3(0, 1, 0), scene);
|
|
|
|
const b = attachTexture(box('b', 1, 1, 1, 0, 2, 2), scene, 'papenweg-textures/individual/floor1.jpg');
|
|
|
|
return {
|
|
scene,
|
|
floorMeshes: [
|
|
b,
|
|
MeshBuilder.CreateGround("ground", {width:30, height:30}),
|
|
],
|
|
};
|
|
}
|
|
|
|
function wsurl(): string {
|
|
const scheme = (document.location.protocol.toLowerCase() === 'https:') ? 'wss' : 'ws';
|
|
return `${scheme}://${document.location.host}/ws`;
|
|
}
|
|
|
|
function bootApp(ds: Ref) {
|
|
spawn named 'app' {
|
|
at ds {
|
|
const url = wsurl();
|
|
const serverCap = Sturdy.asSturdyRef(new Reader<Ref>(
|
|
'<ref "syndicate" [] #[pkgN9TBmEd3Q04grVG4Zdw==]>').next());
|
|
const this_instance = uuid();
|
|
const relayAddr = wsRelay.RelayAddress(Schemas.transportAddress.WebSocket(url));
|
|
|
|
during wsRelay.Resolved({
|
|
"addr": relayAddr,
|
|
"sturdyref": serverCap,
|
|
"resolved": $remoteDs_e: Embedded,
|
|
}) => {
|
|
const remoteDs = remoteDs_e.embeddedValue;
|
|
|
|
on message wakeDetector.WakeEvent() => {
|
|
send message wsRelay.ForceRelayDisconnect(relayAddr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
window.addEventListener('load', async () => {
|
|
await startEngine(createScene);
|
|
Dataspace.boot(ds => {
|
|
html.boot(ds);
|
|
wsRelay.boot(ds, true);
|
|
wakeDetector.boot(ds);
|
|
bootApp(ds);
|
|
});
|
|
});
|